Search in sources :

Example 1 with Label

use of org.eclipse.persistence.internal.libraries.asm.Label in project eclipselink by eclipse-ee4j.

the class ClassWeaver method addPropertyChange.

/**
 * Add a method to track property changes. The method will look as follows:
 *
 * public void _toplink_propertyChange(String s, Object obj, Object obj1){
 * if(_persistence_listener != null {@literal &&} obj != obj1){
 * _persistence_listener.propertyChange(new PropertyChangeEvent(this, s,
 * obj, obj1)); } }
 */
public void addPropertyChange(ClassDetails classDetails) {
    // create the _toplink_propertyChange() method
    MethodVisitor cv_addPC = cv.visitMethod(ACC_PUBLIC, "_persistence_propertyChange", "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V", null, null);
    // if (_toplink_Listener != null)
    cv_addPC.visitVarInsn(ALOAD, 0);
    cv_addPC.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_listener", PCL_SIGNATURE);
    Label l0 = new Label();
    cv_addPC.visitJumpInsn(IFNULL, l0);
    // if (obj != obj1)
    cv_addPC.visitVarInsn(ALOAD, 2);
    cv_addPC.visitVarInsn(ALOAD, 3);
    cv_addPC.visitJumpInsn(IF_ACMPEQ, l0);
    // _toplink_listener.propertyChange(...);
    cv_addPC.visitVarInsn(ALOAD, 0);
    cv_addPC.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_listener", PCL_SIGNATURE);
    cv_addPC.visitTypeInsn(NEW, PCE_SHORT_SIGNATURE);
    cv_addPC.visitInsn(DUP);
    // new PropertyChangeEvent(this, s, obj, obj1)
    cv_addPC.visitVarInsn(ALOAD, 0);
    cv_addPC.visitVarInsn(ALOAD, 1);
    cv_addPC.visitVarInsn(ALOAD, 2);
    cv_addPC.visitVarInsn(ALOAD, 3);
    cv_addPC.visitMethodInsn(INVOKESPECIAL, PCE_SHORT_SIGNATURE, "<init>", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V", false);
    cv_addPC.visitMethodInsn(INVOKEINTERFACE, PCL_SHORT_SIGNATURE, "propertyChange", "(" + PCE_SIGNATURE + ")V", true);
    // }
    cv_addPC.visitLabel(l0);
    cv_addPC.visitInsn(RETURN);
    cv_addPC.visitMaxs(0, 0);
}
Also used : Label(org.eclipse.persistence.internal.libraries.asm.Label) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor)

Example 2 with Label

use of org.eclipse.persistence.internal.libraries.asm.Label in project eclipselink by eclipse-ee4j.

the class ClassWeaver method addFetchGroupMethods.

/**
 * Adds get/set method for FetchGroupTracker interface. This adds the
 * following methods:
 *
 * public Session _persistence_getSession() { return _persistence_session; }
 * public void _persistence_setSession(Session session) {
 * this._persistence_session = session; }
 *
 * public FetchGroup _persistence_getFetchGroup() { return
 * _persistence_fetchGroup; } public void
 * _persistence_setFetchGroup(FetchGroup fetchGroup) {
 * this._persistence_fetchGroup = fetchGroup; }
 *
 * public boolean _persistence_shouldRefreshFetchGroup() { return
 * _persistence_shouldRefreshFetchGroup; } public void
 * _persistence_setShouldRefreshFetchGroup(boolean shouldRefreshFetchGroup)
 * { this._persistence_shouldRefreshFetchGroup = shouldRefreshFetchGroup; }
 *
 * public void _persistence_resetFetchGroup() { }
 *
 * public void _persistence_isAttributeFetched(String attribute) { return
 * this._persistence_fetchGroup == null ||
 * _persistence_fetchGroup.containsAttribute(attribute); }
 *
 * public void _persistence_checkFetched(String attribute) { if
 * (this._persistence_fetchGroup != null) {
 * EntityManagerImpl.processUnfetchedAttribute(this, attribute); } }
 *
 * public void _persistence_checkSetFetched(String attribute) { if
 * (this._persistence_fetchGroup != null) {
 * EntityManagerImpl.processUnfetchedAttributeForSet(this, attribute); } }
 */
public void addFetchGroupMethods(ClassDetails classDetails) {
    MethodVisitor cv_getSession = cv.visitMethod(ACC_PUBLIC, "_persistence_getSession", "()" + SESSION_SIGNATURE, null, null);
    cv_getSession.visitVarInsn(ALOAD, 0);
    cv_getSession.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_session", SESSION_SIGNATURE);
    cv_getSession.visitInsn(ARETURN);
    cv_getSession.visitMaxs(0, 0);
    MethodVisitor cv_setSession = cv.visitMethod(ACC_PUBLIC, "_persistence_setSession", "(" + SESSION_SIGNATURE + ")V", null, null);
    cv_setSession.visitVarInsn(ALOAD, 0);
    cv_setSession.visitVarInsn(ALOAD, 1);
    cv_setSession.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_session", SESSION_SIGNATURE);
    cv_setSession.visitInsn(RETURN);
    cv_setSession.visitMaxs(0, 0);
    MethodVisitor cv_getFetchGroup = cv.visitMethod(ACC_PUBLIC, "_persistence_getFetchGroup", "()" + FETCHGROUP_SIGNATURE, null, null);
    cv_getFetchGroup.visitVarInsn(ALOAD, 0);
    cv_getFetchGroup.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_fetchGroup", FETCHGROUP_SIGNATURE);
    cv_getFetchGroup.visitInsn(ARETURN);
    cv_getFetchGroup.visitMaxs(0, 0);
    MethodVisitor cv_setFetchGroup = cv.visitMethod(ACC_PUBLIC, "_persistence_setFetchGroup", "(" + FETCHGROUP_SIGNATURE + ")V", null, null);
    cv_setFetchGroup.visitVarInsn(ALOAD, 0);
    cv_setFetchGroup.visitVarInsn(ALOAD, 1);
    cv_setFetchGroup.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_fetchGroup", FETCHGROUP_SIGNATURE);
    cv_setFetchGroup.visitInsn(RETURN);
    cv_setFetchGroup.visitMaxs(0, 0);
    MethodVisitor cv_shouldRefreshFetchGroup = cv.visitMethod(ACC_PUBLIC, "_persistence_shouldRefreshFetchGroup", "()" + PBOOLEAN_SIGNATURE, null, null);
    cv_shouldRefreshFetchGroup.visitVarInsn(ALOAD, 0);
    cv_shouldRefreshFetchGroup.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_shouldRefreshFetchGroup", PBOOLEAN_SIGNATURE);
    cv_shouldRefreshFetchGroup.visitInsn(IRETURN);
    cv_shouldRefreshFetchGroup.visitMaxs(0, 0);
    MethodVisitor cv_setShouldRefreshFetchGroup = cv.visitMethod(ACC_PUBLIC, "_persistence_setShouldRefreshFetchGroup", "(" + PBOOLEAN_SIGNATURE + ")V", null, null);
    cv_setShouldRefreshFetchGroup.visitVarInsn(ALOAD, 0);
    cv_setShouldRefreshFetchGroup.visitVarInsn(ILOAD, 1);
    cv_setShouldRefreshFetchGroup.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_shouldRefreshFetchGroup", PBOOLEAN_SIGNATURE);
    cv_setShouldRefreshFetchGroup.visitInsn(RETURN);
    cv_setShouldRefreshFetchGroup.visitMaxs(0, 0);
    MethodVisitor cv_resetFetchGroup = cv.visitMethod(ACC_PUBLIC, "_persistence_resetFetchGroup", "()V", null, null);
    cv_resetFetchGroup.visitInsn(RETURN);
    cv_resetFetchGroup.visitMaxs(0, 0);
    MethodVisitor cv_isAttributeFetched = cv.visitMethod(ACC_PUBLIC, "_persistence_isAttributeFetched", "(Ljava/lang/String;)Z", null, null);
    cv_isAttributeFetched.visitVarInsn(ALOAD, 0);
    cv_isAttributeFetched.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_fetchGroup", FETCHGROUP_SIGNATURE);
    Label gotoTrue = new Label();
    cv_isAttributeFetched.visitJumpInsn(IFNULL, gotoTrue);
    cv_isAttributeFetched.visitVarInsn(ALOAD, 0);
    cv_isAttributeFetched.visitFieldInsn(GETFIELD, classDetails.getClassName(), "_persistence_fetchGroup", FETCHGROUP_SIGNATURE);
    cv_isAttributeFetched.visitVarInsn(ALOAD, 1);
    cv_isAttributeFetched.visitMethodInsn(INVOKEVIRTUAL, FETCHGROUP_SHORT_SIGNATURE, "containsAttributeInternal", "(Ljava/lang/String;)Z", false);
    Label gotoFalse = new Label();
    cv_isAttributeFetched.visitJumpInsn(IFEQ, gotoFalse);
    cv_isAttributeFetched.visitLabel(gotoTrue);
    cv_isAttributeFetched.visitInsn(ICONST_1);
    Label gotoReturn = new Label();
    cv_isAttributeFetched.visitJumpInsn(GOTO, gotoReturn);
    cv_isAttributeFetched.visitLabel(gotoFalse);
    cv_isAttributeFetched.visitInsn(ICONST_0);
    cv_isAttributeFetched.visitLabel(gotoReturn);
    cv_isAttributeFetched.visitInsn(IRETURN);
    cv_isAttributeFetched.visitMaxs(0, 0);
    MethodVisitor cv_checkFetched = cv.visitMethod(ACC_PUBLIC, "_persistence_checkFetched", "(Ljava/lang/String;)V", null, null);
    cv_checkFetched.visitVarInsn(ALOAD, 0);
    cv_checkFetched.visitVarInsn(ALOAD, 1);
    cv_checkFetched.visitMethodInsn(INVOKEVIRTUAL, classDetails.getClassName(), "_persistence_isAttributeFetched", "(Ljava/lang/String;)Z", false);
    gotoReturn = new Label();
    cv_checkFetched.visitJumpInsn(IFNE, gotoReturn);
    cv_checkFetched.visitVarInsn(ALOAD, 0);
    cv_checkFetched.visitTypeInsn(CHECKCAST, FETCHGROUP_TRACKER_SHORT_SIGNATURE);
    cv_checkFetched.visitVarInsn(ALOAD, 1);
    cv_checkFetched.visitMethodInsn(INVOKESTATIC, ENTITY_MANAGER_IMPL_SHORT_SIGNATURE, "processUnfetchedAttribute", "(" + FETCHGROUP_TRACKER_SIGNATURE + "Ljava/lang/String;)V", false);
    cv_checkFetched.visitLabel(gotoReturn);
    cv_checkFetched.visitInsn(RETURN);
    cv_checkFetched.visitMaxs(0, 0);
    MethodVisitor cv_checkFetchedForSet = cv.visitMethod(ACC_PUBLIC, "_persistence_checkFetchedForSet", "(Ljava/lang/String;)V", null, null);
    cv_checkFetchedForSet.visitVarInsn(ALOAD, 0);
    cv_checkFetchedForSet.visitVarInsn(ALOAD, 1);
    cv_checkFetchedForSet.visitMethodInsn(INVOKEVIRTUAL, classDetails.getClassName(), "_persistence_isAttributeFetched", "(Ljava/lang/String;)Z", false);
    gotoReturn = new Label();
    cv_checkFetchedForSet.visitJumpInsn(IFNE, gotoReturn);
    cv_checkFetchedForSet.visitVarInsn(ALOAD, 0);
    cv_checkFetchedForSet.visitTypeInsn(CHECKCAST, FETCHGROUP_TRACKER_SHORT_SIGNATURE);
    cv_checkFetchedForSet.visitVarInsn(ALOAD, 1);
    cv_checkFetchedForSet.visitMethodInsn(INVOKESTATIC, ENTITY_MANAGER_IMPL_SHORT_SIGNATURE, "processUnfetchedAttributeForSet", "(" + FETCHGROUP_TRACKER_SIGNATURE + "Ljava/lang/String;)V", false);
    cv_checkFetchedForSet.visitLabel(gotoReturn);
    cv_checkFetchedForSet.visitInsn(RETURN);
    cv_checkFetchedForSet.visitMaxs(0, 0);
}
Also used : Label(org.eclipse.persistence.internal.libraries.asm.Label) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor)

Example 3 with Label

use of org.eclipse.persistence.internal.libraries.asm.Label in project eclipselink by eclipse-ee4j.

the class ClassWeaver method addPersistencePostClone.

/**
 * Add an internal post clone method. This will clone value holders to avoid
 * change original/clone to effect the other.
 *
 * public Object _persistence_post_clone() { this._attribute_vh =
 * this._attribute_vh.clone(); ... this._persistence_listener = null; return
 * this; }
 */
public void addPersistencePostClone(ClassDetails classDetails) {
    // create the _persistence_post_clone() method
    MethodVisitor cv_clone = cv.visitMethod(ACC_PUBLIC, "_persistence_post_clone", "()Ljava/lang/Object;", null, null);
    // super._persistence_post_clone()
    if (classDetails.getSuperClassDetails() != null && classDetails.getSuperClassDetails().shouldWeaveInternal()) {
        cv_clone.visitVarInsn(ALOAD, 0);
        cv_clone.visitMethodInsn(INVOKESPECIAL, classDetails.getSuperClassName(), "_persistence_post_clone", "()Ljava/lang/Object;", false);
    }
    if (classDetails.shouldWeaveValueHolders()) {
        for (Iterator<AttributeDetails> iterator = classDetails.getAttributesMap().values().iterator(); iterator.hasNext(); ) {
            AttributeDetails attributeDetails = iterator.next();
            if (attributeDetails.weaveValueHolders()) {
                // &&
                // !attributeDetails.isAttributeOnSuperClass())
                // {
                // clone._attribute_vh = this._attribute_vh.clone();
                cv_clone.visitVarInsn(ALOAD, 0);
                cv_clone.visitFieldInsn(GETFIELD, classDetails.getClassName(), PERSISTENCE_FIELDNAME_PREFIX + attributeDetails.getAttributeName() + PERSISTENCE_FIELDNAME_POSTFIX, ClassWeaver.VHI_SIGNATURE);
                Label label = new Label();
                cv_clone.visitJumpInsn(IFNULL, label);
                cv_clone.visitVarInsn(ALOAD, 0);
                cv_clone.visitVarInsn(ALOAD, 0);
                cv_clone.visitFieldInsn(GETFIELD, classDetails.getClassName(), PERSISTENCE_FIELDNAME_PREFIX + attributeDetails.getAttributeName() + PERSISTENCE_FIELDNAME_POSTFIX, ClassWeaver.VHI_SIGNATURE);
                cv_clone.visitMethodInsn(INVOKEINTERFACE, ClassWeaver.VHI_SHORT_SIGNATURE, "clone", "()Ljava/lang/Object;", true);
                cv_clone.visitTypeInsn(CHECKCAST, ClassWeaver.VHI_SHORT_SIGNATURE);
                cv_clone.visitFieldInsn(PUTFIELD, classDetails.getClassName(), PERSISTENCE_FIELDNAME_PREFIX + attributeDetails.getAttributeName() + PERSISTENCE_FIELDNAME_POSTFIX, ClassWeaver.VHI_SIGNATURE);
                cv_clone.visitLabel(label);
            }
        }
    }
    if (classDetails.shouldWeaveChangeTracking()) {
        // clone._persistence_listener = null;
        cv_clone.visitVarInsn(ALOAD, 0);
        cv_clone.visitInsn(ACONST_NULL);
        cv_clone.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_listener", PCL_SIGNATURE);
    }
    if (classDetails.shouldWeaveFetchGroups()) {
        // clone._persistence_fetchGroup = null;
        // clone._persistence_session = null;
        cv_clone.visitVarInsn(ALOAD, 0);
        cv_clone.visitInsn(ACONST_NULL);
        cv_clone.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_fetchGroup", FETCHGROUP_SIGNATURE);
        cv_clone.visitVarInsn(ALOAD, 0);
        cv_clone.visitInsn(ACONST_NULL);
        cv_clone.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_session", SESSION_SIGNATURE);
    }
    if (!classDetails.isEmbedable()) {
        // clone._persistence_primaryKey = null;
        cv_clone.visitVarInsn(ALOAD, 0);
        cv_clone.visitInsn(ACONST_NULL);
        cv_clone.visitFieldInsn(PUTFIELD, classDetails.getClassName(), "_persistence_primaryKey", OBJECT_SIGNATURE);
    }
    // return clone;
    cv_clone.visitVarInsn(ALOAD, 0);
    cv_clone.visitInsn(ARETURN);
    cv_clone.visitMaxs(0, 0);
}
Also used : Label(org.eclipse.persistence.internal.libraries.asm.Label) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor)

Example 4 with Label

use of org.eclipse.persistence.internal.libraries.asm.Label 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();
}
Also used : XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) Label(org.eclipse.persistence.internal.libraries.asm.Label) Method(java.lang.reflect.Method) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor) XmlList(jakarta.xml.bind.annotation.XmlList) AnnotationVisitor(org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor) XmlElement(jakarta.xml.bind.annotation.XmlElement) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) ArrayValue(org.eclipse.persistence.internal.jaxb.many.ArrayValue) MultiDimensionalArrayValue(org.eclipse.persistence.internal.jaxb.many.MultiDimensionalArrayValue)

Example 5 with Label

use of org.eclipse.persistence.internal.libraries.asm.Label 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);
}
Also used : XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) Label(org.eclipse.persistence.internal.libraries.asm.Label) EclipseLinkASMClassWriter(org.eclipse.persistence.internal.libraries.asm.EclipseLinkASMClassWriter) Method(java.lang.reflect.Method) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) FieldVisitor(org.eclipse.persistence.internal.libraries.asm.FieldVisitor) Namespace(org.eclipse.persistence.internal.oxm.Namespace) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor) Type(org.eclipse.persistence.internal.libraries.asm.Type) XmlType(jakarta.xml.bind.annotation.XmlType) XmlAccessorType(jakarta.xml.bind.annotation.XmlAccessorType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) XmlAccessType(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessType) XmlSchemaType(jakarta.xml.bind.annotation.XmlSchemaType) AnnotationVisitor(org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XmlElement(jakarta.xml.bind.annotation.XmlElement) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass)

Aggregations

Label (org.eclipse.persistence.internal.libraries.asm.Label)11 MethodVisitor (org.eclipse.persistence.internal.libraries.asm.MethodVisitor)10 XmlElement (jakarta.xml.bind.annotation.XmlElement)2 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)2 Annotation (java.lang.annotation.Annotation)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 AnnotationVisitor (org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor)2 EclipseLinkASMClassWriter (org.eclipse.persistence.internal.libraries.asm.EclipseLinkASMClassWriter)2 FieldVisitor (org.eclipse.persistence.internal.libraries.asm.FieldVisitor)2 Type (org.eclipse.persistence.internal.libraries.asm.Type)2 JavaAnnotation (org.eclipse.persistence.jaxb.javamodel.JavaAnnotation)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)2 XmlAccessorType (jakarta.xml.bind.annotation.XmlAccessorType)1 XmlList (jakarta.xml.bind.annotation.XmlList)1 XmlMimeType (jakarta.xml.bind.annotation.XmlMimeType)1 XmlSchemaType (jakarta.xml.bind.annotation.XmlSchemaType)1 XmlType (jakarta.xml.bind.annotation.XmlType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1