use of org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method generateManyValueClass.
private void generateManyValueClass(EclipseLinkASMClassWriter cw, TypeMappingInfo typeMappingInfo, String namespace, Class<?> superType, String classNameSeparatedBySlash, JavaClass componentType, JavaClass containerType) {
String componentClassNameSeparatedBySlash = getObjectType(componentType).getQualifiedName().replace(DOT_CHR, SLASH_CHR);
String containerClassNameSeperatedBySlash = containerType.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
if ("[B".equals(componentClassNameSeparatedBySlash)) {
cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classNameSeparatedBySlash, "L" + Type.getInternalName(superType) + "<" + componentClassNameSeparatedBySlash + ">;", Type.getInternalName(superType), null);
} else {
cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classNameSeparatedBySlash, "L" + Type.getInternalName(superType) + "<L" + componentClassNameSeparatedBySlash + ";>;", Type.getInternalName(superType), null);
}
// Write @XmlType(namespace)
AnnotationVisitor av = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlType;", true);
if (null != namespace) {
av.visit("namespace", namespace);
}
if (classNameSeparatedBySlash.startsWith(ARRAY_PACKAGE_NAME.replace('.', '/')) && classNameSeparatedBySlash.contains("QName")) {
av.visit("name", classNameSeparatedBySlash.substring(classNameSeparatedBySlash.lastIndexOf('/') + 1));
}
av.visitEnd();
// Public No-Arg Constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superType), "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
if (!componentType.isPrimitive() && ArrayValue.class.isAssignableFrom(superType)) {
// @Override
// public Object getItem() {
// if(null == adaptedValue) {
// return null;
// }
// int len = adaptedValue.size();
// Float[] array = new Float[len];
// adaptedValue.toArray(array);
// return array;
// }
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getItem", "()Ljava/lang/Object;", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, classNameSeparatedBySlash, "adaptedValue", "Ljava/util/Collection;");
Label l0 = new Label();
mv.visitJumpInsn(Opcodes.IFNONNULL, l0);
mv.visitInsn(Opcodes.ACONST_NULL);
mv.visitInsn(Opcodes.ARETURN);
mv.visitLabel(l0);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, classNameSeparatedBySlash, "adaptedValue", "Ljava/util/Collection;");
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size", "()I", true);
mv.visitVarInsn(Opcodes.ISTORE, 1);
mv.visitVarInsn(Opcodes.ILOAD, 1);
mv.visitTypeInsn(Opcodes.ANEWARRAY, componentClassNameSeparatedBySlash);
mv.visitVarInsn(Opcodes.ASTORE, 2);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, classNameSeparatedBySlash, "adaptedValue", "Ljava/util/Collection;");
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", true);
mv.visitInsn(Opcodes.POP);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(2, 3);
mv.visitEnd();
// @Override
// public void setItem(Object array) {
// Float[] floatArray = (Float[])array;
// adaptedValue = (Collection<T>) Arrays.asList(floatArray);
// }
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setItem", "(Ljava/lang/Object;)V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitTypeInsn(Opcodes.CHECKCAST, "[L" + componentClassNameSeparatedBySlash + ";");
mv.visitVarInsn(Opcodes.ASTORE, 2);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", false);
mv.visitFieldInsn(Opcodes.PUTFIELD, classNameSeparatedBySlash, "adaptedValue", "Ljava/util/Collection;");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 3);
mv.visitEnd();
}
// }
if ("[B".equals(componentClassNameSeparatedBySlash)) {
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getAdaptedValue", "()Ljava/util/Collection;", "()Ljava/util/Collection<" + componentClassNameSeparatedBySlash + ">;", null);
} else {
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getAdaptedValue", "()Ljava/util/Collection;", "()Ljava/util/Collection<L" + componentClassNameSeparatedBySlash + ";>;", null);
}
// Copy annotations
boolean hasXmlList = false;
Annotation[] annotations;
if (typeMappingInfo != null && ((annotations = getAnnotations(typeMappingInfo)) != null)) {
for (Annotation annotation : annotations) {
if (!(annotation instanceof XmlElement || annotation instanceof XmlJavaTypeAdapter)) {
Class<? extends Annotation> annotationType = annotation.annotationType();
// if(annotationType.equals(XmlList.class)) {
if (annotation instanceof XmlList) {
hasXmlList = true;
}
av = mv.visitAnnotation(L + annotationType.getName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON, true);
for (Method next : annotation.annotationType().getDeclaredMethods()) {
try {
Object nextValue = next.invoke(annotation);
if (nextValue instanceof Class) {
nextValue = Type.getType(L + ((Class) nextValue).getName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
}
av.visit(next.getName(), nextValue);
} catch (InvocationTargetException ex) {
} catch (IllegalAccessException ex) {
}
}
av.visitEnd();
}
}
}
if (hasXmlList) {
// @XmlValue
av = mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlValue;", true);
av = mv.visitAnnotation("Lorg/eclipse/persistence/oxm/annotations/XmlValueExtension;", true);
av.visitEnd();
} else {
// @XmlElement(name="item", nillable=true)
av = mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlElement;", true);
av.visit("name", ITEM);
av.visit("nillable", true);
av.visitEnd();
}
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superType), "getAdaptedValue", "()Ljava/util/Collection;", false);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
// public void setAdaptedValue(Collection<COMPONENT_TYPE> adaptedValue) {
// super.setAdaptedValue(adaptedValue);
// }
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setAdaptedValue", "(Ljava/util/Collection;)V", "(Ljava/util/Collection<L" + componentClassNameSeparatedBySlash + ";>;)V", null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superType), "setAdaptedValue", "(Ljava/util/Collection;)V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
// public Class<?> containerClass() {
// return CONTAINER_TYPE.class;
// }
mv = cw.visitMethod(Opcodes.ACC_PROTECTED, "containerClass", "()Ljava/lang/Class;", "()Ljava/lang/Class<*>;", null);
mv.visitCode();
if (componentType.isPrimitive()) {
mv.visitFieldInsn(Opcodes.GETSTATIC, getObjectType(componentType).getQualifiedName().replace(DOT_CHR, SLASH_CHR), "TYPE", "Ljava/lang/Class;");
} else {
if (containerClassNameSeperatedBySlash.contains(";")) {
mv.visitLdcInsn(Type.getType(containerClassNameSeperatedBySlash));
} else {
mv.visitLdcInsn(Type.getType("L" + containerClassNameSeperatedBySlash + ";"));
}
}
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
use of org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method generateWrapperForMapClass.
private Class<?> generateWrapperForMapClass(JavaClass mapClass, JavaClass keyClass, JavaClass valueClass, TypeMappingInfo typeMappingInfo) {
String packageName = JAXB_DEV;
NamespaceResolver combinedNamespaceResolver = new NamespaceResolver();
if (!helper.isBuiltInJavaType(keyClass)) {
String keyPackageName = keyClass.getPackageName();
packageName = packageName + DOT_CHR + keyPackageName;
NamespaceInfo keyNamespaceInfo = getPackageInfoForPackage(keyClass).getNamespaceInfo();
if (keyNamespaceInfo != null) {
java.util.Vector<Namespace> namespaces = keyNamespaceInfo.getNamespaceResolver().getNamespaces();
for (Namespace n : namespaces) {
combinedNamespaceResolver.put(n.getPrefix(), n.getNamespaceURI());
}
}
}
if (!helper.isBuiltInJavaType(valueClass)) {
String valuePackageName = valueClass.getPackageName();
packageName = packageName + DOT_CHR + valuePackageName;
NamespaceInfo valueNamespaceInfo = getPackageInfoForPackage(valueClass).getNamespaceInfo();
if (valueNamespaceInfo != null) {
java.util.Vector<Namespace> namespaces = valueNamespaceInfo.getNamespaceResolver().getNamespaces();
for (Namespace n : namespaces) {
combinedNamespaceResolver.put(n.getPrefix(), n.getNamespaceURI());
}
}
}
String namespace = this.defaultTargetNamespace;
if (namespace == null) {
namespace = EMPTY_STRING;
}
PackageInfo packageInfo = packageToPackageInfoMappings.get(mapClass.getPackageName());
if (packageInfo == null) {
packageInfo = getPackageToPackageInfoMappings().get(packageName);
} else {
if (packageInfo.getNamespace() != null) {
namespace = packageInfo.getNamespace();
}
getPackageToPackageInfoMappings().put(packageName, packageInfo);
}
if (packageInfo == null) {
packageInfo = new PackageInfo();
packageInfo.setNamespaceInfo(new NamespaceInfo());
packageInfo.setNamespace(namespace);
packageInfo.setNamespaceResolver(combinedNamespaceResolver);
getPackageToPackageInfoMappings().put(packageName, packageInfo);
}
int beginIndex = keyClass.getName().lastIndexOf(DOT_CHR) + 1;
String keyName = keyClass.getName().substring(beginIndex);
int dollarIndex = keyName.indexOf(DOLLAR_SIGN_CHR);
if (dollarIndex > -1) {
keyName = keyName.substring(dollarIndex + 1);
}
beginIndex = valueClass.getName().lastIndexOf(DOT_CHR) + 1;
String valueName = valueClass.getName().substring(beginIndex);
dollarIndex = valueName.indexOf(DOLLAR_SIGN_CHR);
if (dollarIndex > -1) {
valueName = valueName.substring(dollarIndex + 1);
}
String collectionClassShortName = mapClass.getRawName().substring(mapClass.getRawName().lastIndexOf(DOT_CHR) + 1);
String suggestedClassName = keyName + valueName + collectionClassShortName;
String qualifiedClassName = packageName + DOT_CHR + suggestedClassName;
qualifiedClassName = getNextAvailableClassName(qualifiedClassName);
String qualifiedInternalClassName = qualifiedClassName.replace(DOT_CHR, SLASH_CHR);
String internalKeyName = keyClass.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
String internalValueName = valueClass.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
Type mapType = Type.getType(L + mapClass.getRawName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
EclipseLinkASMClassWriter cw = new EclipseLinkASMClassWriter();
String sig = "Lorg/eclipse/persistence/internal/jaxb/many/MapValue<L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;>;";
cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, qualifiedInternalClassName, sig, "org/eclipse/persistence/internal/jaxb/many/MapValue", null);
// Write Field: @... public Map entry
String fieldSig = L + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;";
FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, "entry", L + mapType.getInternalName() + SEMI_COLON, fieldSig, null);
fv.visitAnnotation(Type.getDescriptor(XmlElement.class), true);
if (typeMappingInfo != null) {
Annotation[] annotations = typeMappingInfo.getAnnotations();
if (annotations != null) {
for (Annotation nextAnnotation : annotations) {
if (nextAnnotation != null && !(nextAnnotation instanceof XmlElement) && !(nextAnnotation instanceof XmlJavaTypeAdapter)) {
String annotationClassName = nextAnnotation.annotationType().getName();
AnnotationVisitor av = fv.visitAnnotation(L + annotationClassName.replace(DOT_CHR, SLASH_CHR) + SEMI_COLON, true);
for (Method next : nextAnnotation.annotationType().getDeclaredMethods()) {
try {
Object nextValue = next.invoke(nextAnnotation);
if (nextValue instanceof Class) {
Type nextType = Type.getType(L + ((Class) nextValue).getName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
nextValue = nextType;
}
av.visit(next.getName(), nextValue);
} catch (InvocationTargetException ignored) {
// ignore the invocation target exception here.
} catch (IllegalAccessException ignored) {
// ignore the illegal access exception here.
}
}
av.visitEnd();
}
}
}
}
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "org/eclipse/persistence/internal/jaxb/many/MapValue", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
// Write: @XmlTransient public void setItem(???)
String methodSig = "(L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;)V";
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setItem", "(L" + mapType.getInternalName() + ";)V", methodSig, null);
// TODO: Verify that we really want to put @XmlTranient on setItem
// method
mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlTransient;", true);
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, qualifiedInternalClassName, "entry", L + mapType.getInternalName() + SEMI_COLON);
mv.visitInsn(Opcodes.RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
// Replacement?:LocalVariableTypeTableAttribute cvAttr = new
// LocalVariableTypeTableAttribute();
// mv.visitAttribute(cvAttr);
mv.visitMaxs(2, 2);
mv.visitEnd();
// Write @XmlTransient public ??? getItem()
methodSig = "()L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;";
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getItem", "()L" + mapType.getInternalName() + SEMI_COLON, methodSig, null);
mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlTransient;", true);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, qualifiedInternalClassName, "entry", L + mapType.getInternalName() + SEMI_COLON);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, "getItem", "()Ljava/lang/Object;", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, qualifiedInternalClassName, "getItem", "()L" + mapType.getInternalName() + SEMI_COLON, false);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, "setItem", "(Ljava/lang/Object;)V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitTypeInsn(Opcodes.CHECKCAST, mapType.getInternalName());
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, qualifiedInternalClassName, "setItem", "(L" + mapType.getInternalName() + ";)V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
// Write @XmlType(namespace)
AnnotationVisitor av = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlType;", true);
av.visit("namespace", namespace);
cw.visitEnd();
byte[] classBytes = cw.toByteArray();
return generateClassFromBytes(qualifiedClassName, classBytes);
}
use of org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor in project eclipselink by eclipse-ee4j.
the class ClassLoaderTestCases method setUp.
@Override
protected void setUp() throws Exception {
EclipseLinkASMClassWriter cw = new EclipseLinkASMClassWriter();
cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, CLASS_NAME, null, ClassLoaderRoot.class.getName().replace('.', '/'), null);
AnnotationVisitor xmlTypeAV = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlRootElement;", true);
xmlTypeAV.visit("name", "root");
xmlTypeAV.visitEnd();
// Write Constructor:
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ClassLoaderRoot.class.getName().replace('.', '/'), "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
JaxbClassLoader classLoader = new JaxbClassLoader(ClassLoaderRoot.class.getClassLoader());
classLoaderChildClass = classLoader.generateClass(CLASS_NAME, cw.toByteArray());
jaxbContext = JAXBContextFactory.createContext(new Class<?>[] { classLoaderChildClass }, null);
}
Aggregations