use of com.sun.xml.ws.org.objectweb.asm.AnnotationVisitor in project metro-jax-ws by eclipse-ee4j.
the class WrapperBeanGenerator method createBeanImage.
// Creates class's bytes
private static byte[] createBeanImage(String className, String rootName, String rootNS, String typeName, String typeNS, Collection<Field> fields) throws Exception {
ClassWriter cw = new ClassWriter(0);
// org.objectweb.asm.util.TraceClassVisitor cw = new org.objectweb.asm.util.TraceClassVisitor(actual, new java.io.PrintWriter(System.out));
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, replaceDotWithSlash(className), null, "java/lang/Object", null);
AnnotationVisitor root = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlRootElement;", true);
root.visit("name", rootName);
root.visit("namespace", rootNS);
root.visitEnd();
AnnotationVisitor type = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlType;", true);
type.visit("name", typeName);
type.visit("namespace", typeNS);
if (fields.size() > 1) {
AnnotationVisitor propVisitor = type.visitArray("propOrder");
for (Field field : fields) {
propVisitor.visit("propOrder", field.fieldName);
}
propVisitor.visitEnd();
}
type.visitEnd();
for (Field field : fields) {
FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, field.fieldName, field.asmType.getDescriptor(), field.getSignature(), null);
for (Annotation ann : field.jaxbAnnotations) {
if (ann instanceof XmlMimeType) {
AnnotationVisitor mime = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlMimeType;", true);
mime.visit("value", ((XmlMimeType) ann).value());
mime.visitEnd();
} else if (ann instanceof XmlJavaTypeAdapter) {
AnnotationVisitor ada = fv.visitAnnotation("Ljakarta/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
ada.visit("value", getASMType(((XmlJavaTypeAdapter) ann).value()));
// XmlJavaTypeAdapter.type() is for package only. No need to copy.
// ada.visit("type", ((XmlJavaTypeAdapter)ann).type());
ada.visitEnd();
} else if (ann instanceof XmlAttachmentRef) {
AnnotationVisitor att = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlAttachmentRef;", true);
att.visitEnd();
} else if (ann instanceof XmlList) {
AnnotationVisitor list = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlList;", true);
list.visitEnd();
} else if (ann instanceof XmlElement) {
AnnotationVisitor elem = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlElement;", true);
XmlElement xmlElem = (XmlElement) ann;
elem.visit("name", xmlElem.name());
elem.visit("namespace", xmlElem.namespace());
if (xmlElem.nillable()) {
elem.visit("nillable", true);
}
if (xmlElem.required()) {
elem.visit("required", true);
}
elem.visitEnd();
} else {
throw new WebServiceException("Unknown JAXB annotation " + ann);
}
}
fv.visitEnd();
}
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
cw.visitEnd();
if (LOGGER.isLoggable(Level.FINE)) {
// Class's @XmlRootElement
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append("@XmlRootElement(name=").append(rootName).append(", namespace=").append(rootNS).append(")");
// Class's @XmlType
sb.append("\n");
sb.append("@XmlType(name=").append(typeName).append(", namespace=").append(typeNS);
if (fields.size() > 1) {
sb.append(", propOrder={");
for (Field field : fields) {
sb.append(" ");
sb.append(field.fieldName);
}
sb.append(" }");
}
sb.append(")");
// class declaration
sb.append("\n");
sb.append("public class ").append(className).append(" {");
// fields declaration
for (Field field : fields) {
sb.append("\n");
// Field's other JAXB annotations
for (Annotation ann : field.jaxbAnnotations) {
sb.append("\n ");
if (ann instanceof XmlMimeType) {
sb.append("@XmlMimeType(value=").append(((XmlMimeType) ann).value()).append(")");
} else if (ann instanceof XmlJavaTypeAdapter) {
sb.append("@XmlJavaTypeAdapter(value=").append(getASMType(((XmlJavaTypeAdapter) ann).value())).append(")");
} else if (ann instanceof XmlAttachmentRef) {
sb.append("@XmlAttachmentRef");
} else if (ann instanceof XmlList) {
sb.append("@XmlList");
} else if (ann instanceof XmlElement) {
XmlElement xmlElem = (XmlElement) ann;
sb.append("\n ");
sb.append("@XmlElement(name=").append(xmlElem.name()).append(", namespace=").append(xmlElem.namespace());
if (xmlElem.nillable()) {
sb.append(", nillable=true");
}
if (xmlElem.required()) {
sb.append(", required=true");
}
sb.append(")");
} else {
throw new WebServiceException("Unknown JAXB annotation " + ann);
}
}
// Field declaration
sb.append("\n ");
sb.append("public ");
if (field.getSignature() == null) {
sb.append(field.asmType.getDescriptor());
} else {
sb.append(field.getSignature());
}
sb.append(" ");
sb.append(field.fieldName);
}
sb.append("\n\n}");
LOGGER.fine(sb.toString());
}
return cw.toByteArray();
}
Aggregations