Search in sources :

Example 1 with FieldVisitor

use of org.apache.xbean.asm5.FieldVisitor in project tomee by apache.

the class Cmp1Generator method generate.

/**
     * Generate the class for implementing CMP 1 level of
     * persistence.
     *
     * @return The generated byte-array containing the class data.
     */
public byte[] generate() {
    // We're creating a superclass for the implementation.  We force this to implement
    // EntityBean to allow POJOs to be used as the bean class. 
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, implClassName, null, beanClassName, new String[] { "javax/ejb/EntityBean" });
    // if we have an unknown pk, we need to add a field for the pk
    if (unknownPk) {
        // public Long OpenEJB_pk;
        final FieldVisitor fv = cw.visitField(ACC_PUBLIC, "OpenEJB_pk", "Ljava/lang/Long;", null, null);
        fv.visitEnd();
    }
    // there's not much to generate here.  We create a default constructor, then generate the 
    // post create methods.  A lot of the work is done by having mapped superclass information that 
    // we pass to the JPA engine. 
    createConstructor();
    postCreateGenerator.generate();
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : FieldVisitor(org.apache.xbean.asm5.FieldVisitor)

Example 2 with FieldVisitor

use of org.apache.xbean.asm5.FieldVisitor in project jodd by oblac.

the class ProxettaWrapperClassBuilder method visit.

/**
	 * {@inheritDoc}
	 */
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
    wd.init(name, superName, this.suffix, this.reqProxyClassName);
    // no superclass
    wd.superName = AsmUtil.SIGNATURE_JAVA_LANG_OBJECT;
    // change access of destination
    access &= ~AsmUtil.ACC_ABSTRACT;
    access &= ~AsmUtil.ACC_INTERFACE;
    // write destination class
    if (targetClassOrInterface.isInterface()) {
        // target is interface
        wd.wrapInterface = true;
        interfaces = new String[] { targetClassOrInterface.getName().replace('.', '/') };
    } else {
        // target is class
        wd.wrapInterface = false;
        if (targetInterface != null) {
            // interface provided
            interfaces = new String[] { targetInterface.getName().replace('.', '/') };
        } else {
        // no interface provided, use all
        //interfaces = null;
        }
    }
    wd.dest.visit(version, access, wd.thisReference, signature, wd.superName, interfaces);
    wd.proxyAspects = new ProxyAspectData[aspects.length];
    for (int i = 0; i < aspects.length; i++) {
        wd.proxyAspects[i] = new ProxyAspectData(wd, aspects[i], i);
    }
    // create new field wrapper field and store it's reference into work-data
    wd.wrapperRef = targetFieldName;
    wd.wrapperType = 'L' + name + ';';
    FieldVisitor fv = wd.dest.visitField(AsmUtil.ACC_PUBLIC, wd.wrapperRef, wd.wrapperType, null, null);
    fv.visitEnd();
    createEmptyCtor();
}
Also used : FieldVisitor(jodd.asm5.FieldVisitor)

Example 3 with FieldVisitor

use of org.apache.xbean.asm5.FieldVisitor in project jodd by oblac.

the class ProxyAspectData method readAdviceData.

// ---------------------------------------------------------------- read
/**
	 * Parse advice class to gather some advice data. Should be called before any advice use.
	 * Must be called only *once* per advice.
	 */
private void readAdviceData() {
    if (ready) {
        return;
    }
    adviceClassReader.accept(new EmptyClassVisitor() {

        /**
			 * Stores advice reference.
			 */
        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            adviceReference = name;
            super.visit(version, access, name, signature, superName, interfaces);
        }

        /**
			 * Prevents advice to have inner classes.
			 */
        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            if (outerName.equals(adviceReference)) {
                throw new ProxettaException("Proxetta doesn't allow inner classes in/for advice: " + advice.getName());
            }
            super.visitInnerClass(name, outerName, innerName, access);
        }

        /**
			 * Clones advices fields to destination.
			 */
        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            // [A5]
            wd.dest.visitField(access, adviceFieldName(name, aspectIndex), desc, signature, value);
            return super.visitField(access, name, desc, signature, value);
        }

        /**
			 * Copies advices methods to destination.
			 */
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            if (name.equals(CLINIT)) {
                // [A6]
                if (!desc.equals(DESC_VOID)) {
                    throw new ProxettaException("Invalid static initialization block description for advice: " + advice.getName());
                }
                name = clinitMethodName + methodDivider + aspectIndex;
                access |= AsmUtil.ACC_PRIVATE | AsmUtil.ACC_FINAL;
                wd.addAdviceClinitMethod(name);
                return new MethodAdapter(wd.dest.visitMethod(access, name, desc, signature, exceptions)) {

                    @Override
                    public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
                    }

                    @Override
                    public void visitLineNumber(int line, Label start) {
                    }

                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
                        if (opcode == INVOKESTATIC) {
                            if (owner.equals(adviceReference)) {
                                owner = wd.thisReference;
                                name = adviceMethodName(name, aspectIndex);
                            }
                        }
                        super.visitMethodInsn(opcode, owner, name, desc, isInterface);
                    }

                    @Override
                    public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                        // [F6]
                        if (owner.equals(adviceReference)) {
                            // [F5]
                            owner = wd.thisReference;
                            name = adviceFieldName(name, aspectIndex);
                        }
                        super.visitFieldInsn(opcode, owner, name, desc);
                    }
                };
            } else if (name.equals(INIT)) {
                // [A7]
                if (!desc.equals(DESC_VOID)) {
                    throw new ProxettaException("Advices can have only default constructors. Invalid advice: " + advice.getName());
                }
                name = initMethodName + methodDivider + aspectIndex;
                access = ProxettaAsmUtil.makePrivateFinalAccess(access);
                wd.addAdviceInitMethod(name);
                return new MethodAdapter(wd.dest.visitMethod(access, name, desc, signature, exceptions)) {

                    @Override
                    public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
                    }

                    @Override
                    public void visitLineNumber(int line, Label start) {
                    }

                    // used to detect and to ignore the first super call()
                    int state;

                    @Override
                    public void visitVarInsn(int opcode, int var) {
                        // [F7]
                        if ((state == 0) && (opcode == ALOAD) && (var == 0)) {
                            state++;
                            return;
                        }
                        super.visitVarInsn(opcode, var);
                    }

                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
                        if ((state == 1) && (opcode == INVOKESPECIAL)) {
                            state++;
                            return;
                        }
                        if ((opcode == INVOKEVIRTUAL) || (opcode == INVOKEINTERFACE)) {
                            if (owner.equals(adviceReference)) {
                                owner = wd.thisReference;
                                name = adviceMethodName(name, aspectIndex);
                            }
                        } else if (opcode == INVOKESTATIC) {
                            if (owner.equals(adviceReference)) {
                                owner = wd.thisReference;
                                name = adviceMethodName(name, aspectIndex);
                            }
                        }
                        super.visitMethodInsn(opcode, owner, name, desc, isInterface);
                    }

                    @Override
                    public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                        // [F7]
                        if (owner.equals(adviceReference)) {
                            // [F5]
                            owner = wd.thisReference;
                            name = adviceFieldName(name, aspectIndex);
                        }
                        super.visitFieldInsn(opcode, owner, name, desc);
                    }
                };
            } else // other methods
            if (!name.equals(executeMethodName)) {
                name = adviceMethodName(name, aspectIndex);
                return new MethodAdapter(wd.dest.visitMethod(access, name, desc, signature, exceptions)) {

                    @Override
                    public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
                    }

                    @Override
                    public void visitLineNumber(int line, Label start) {
                    }

                    @Override
                    public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) {
                        if ((opcode == INVOKEVIRTUAL) || (opcode == INVOKEINTERFACE)) {
                            if (owner.equals(adviceReference)) {
                                owner = wd.thisReference;
                                name = adviceMethodName(name, aspectIndex);
                            }
                        } else if (opcode == INVOKESTATIC || opcode == INVOKESPECIAL) {
                            if (owner.equals(adviceReference)) {
                                owner = wd.thisReference;
                                name = adviceMethodName(name, aspectIndex);
                            }
                        }
                        super.visitMethodInsn(opcode, owner, name, desc, isInterface);
                    }

                    @Override
                    public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                        // replace field references
                        if (owner.equals(adviceReference)) {
                            owner = wd.thisReference;
                            name = adviceFieldName(name, aspectIndex);
                        }
                        super.visitFieldInsn(opcode, owner, name, desc);
                    }
                };
            }
            //return new MethodAdapter(new EmptyMethodVisitor()) {		// toask may we replace this with the following code?
            return new EmptyMethodVisitor() {

                @Override
                public void visitVarInsn(int opcode, int var) {
                    if (isStoreOpcode(opcode)) {
                        if (var > maxLocalVarOffset) {
                            // find max local var offset
                            maxLocalVarOffset = var;
                        }
                    }
                    super.visitVarInsn(opcode, var);
                }
            };
        //					return super.visitMethod(access, name, desc, signature, exceptions);
        }
    }, 0);
    // increment offset by 2 because var on last index may be a dword value
    maxLocalVarOffset += 2;
    ready = true;
}
Also used : ProxettaException(jodd.proxetta.ProxettaException) Label(jodd.asm5.Label) FieldVisitor(jodd.asm5.FieldVisitor) EmptyClassVisitor(jodd.asm.EmptyClassVisitor) EmptyMethodVisitor(jodd.asm.EmptyMethodVisitor) MethodVisitor(jodd.asm5.MethodVisitor) MethodAdapter(jodd.asm.MethodAdapter) EmptyMethodVisitor(jodd.asm.EmptyMethodVisitor)

Example 4 with FieldVisitor

use of org.apache.xbean.asm5.FieldVisitor in project tomee by apache.

the class Cmp2Generator method generate.

/**
     * Perform the generation step for a CMP Entity Bean.
     * This uses the accumulated meta data and the
     * base bean class to generate a subclass with
     * the automatically generated bits of OpenEJB infrastructure
     * hooks.
     *
     * @return The class file byte array to be used for defining this
     * class.
     */
public byte[] generate() {
    // generate the class as super class of the base bean class.  This class will also implment 
    // EntityBean and Cmp2Entity. 
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, implClassName, null, beanClassName, new String[] { "org/apache/openejb/core/cmp/cmp2/Cmp2Entity", "javax/ejb/EntityBean" });
    // public static Object deploymentInfo;
    {
        final FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "deploymentInfo", "Ljava/lang/Object;", null, null);
        fv.visitEnd();
    }
    // private transient boolean deleted;
    {
        final FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_TRANSIENT + ACC_VOLATILE, DELETED, "Z", null, null);
        fv.visitEnd();
    }
    if (Object.class.equals(primKeyClass)) {
        final FieldVisitor fv = cw.visitField(ACC_PRIVATE, UNKNOWN_PK_NAME, UNKNOWN_PK_TYPE.getDescriptor(), null, null);
        fv.visitEnd();
    }
    // private ${cmpField.type} ${cmpField.name};
    for (final CmpField cmpField : cmpFields.values()) {
        createField(cmpField);
    }
    // and create the corresponding CMR fields as well. 
    for (final CmrField cmrField : cmrFields) {
        createCmrFields(cmrField);
    }
    createConstructor();
    // from the abstract methods the bean author should have provided. 
    for (final CmpField cmpField : cmpFields.values()) {
        // public ${cmpField.type} get${cmpField.name}() {
        //     return this.${cmpField.name};
        // }
        createGetter(cmpField);
        // public void setId(${cmpField.type} ${cmpField.name}) {
        //    this.${cmpField.name} = ${cmpField.name};
        // }
        createSetter(cmpField);
    }
    // and repeat this for the cmr fields. 
    for (final CmrField cmrField : cmrFields) {
        createCmrGetter(cmrField);
        createCmrSetter(cmrField);
    }
    createSimplePrimaryKeyGetter();
    // add the set of OpenEJB container management methods. 
    createOpenEJB_isDeleted();
    createOpenEJB_deleted();
    createOpenEJB_addCmr();
    createOpenEJB_removeCmr();
    // generate the select methods 
    for (final Method selectMethod : selectMethods) {
        createSelectMethod(selectMethod);
    }
    // empty ones in the generated superclass. 
    if (!hasMethod(beanClass, "ejbActivate")) {
        createEjbActivate();
    }
    if (!hasMethod(beanClass, "ejbPassivate")) {
        createEjbPassivate();
    }
    if (!hasMethod(beanClass, "ejbLoad")) {
        createEjbLoad();
    }
    if (!hasMethod(beanClass, "ejbStore")) {
        createEjbStore();
    }
    if (!hasMethod(beanClass, "ejbRemove")) {
        createEjbRemove();
    }
    if (!hasMethod(beanClass, "setEntityContext", EntityContext.class)) {
        createSetEntityContext();
    }
    if (!hasMethod(beanClass, "unsetEntityContext")) {
        createUnsetEntityContext();
    }
    // add on any post-create methods that might be required. 
    postCreateGenerator.generate();
    cw.visitEnd();
    // be defined in the appropriate classloader instance. 
    return cw.toByteArray();
}
Also used : EntityContext(javax.ejb.EntityContext) Method(java.lang.reflect.Method) FieldVisitor(org.apache.xbean.asm5.FieldVisitor)

Example 5 with FieldVisitor

use of org.apache.xbean.asm5.FieldVisitor in project tomee by apache.

the class Cmp2Generator method createCmrFields.

/**
     * Create the CMR fields defined for this object.  This
     * creates a pair of fields for each CMR field.  The
     * first field is the real field containing the object
     * data.  The second field will be an accessor object
     * that's instantiated when the fields are first
     * initialized.  The accessor field gets created with
     * the same name and "Cmr" concatenated to the end
     * of the field name.
     *
     * @param cmrField The CMR field descriptor.
     */
private void createCmrFields(final CmrField cmrField) {
    FieldVisitor fv = cw.visitField(ACC_PRIVATE, cmrField.getName(), cmrField.getDescriptor(), cmrField.getGenericSignature(), null);
    fv.visitEnd();
    fv = cw.visitField(ACC_PRIVATE + ACC_TRANSIENT, cmrField.getName() + "Cmr", cmrField.getAccessorDescriptor(), cmrField.getAccessorGenericSignature(), null);
    fv.visitEnd();
}
Also used : FieldVisitor(org.apache.xbean.asm5.FieldVisitor)

Aggregations

FieldVisitor (org.apache.xbean.asm5.FieldVisitor)4 FieldVisitor (jodd.asm5.FieldVisitor)2 Method (java.lang.reflect.Method)1 EntityContext (javax.ejb.EntityContext)1 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)1 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)1 MethodAdapter (jodd.asm.MethodAdapter)1 Label (jodd.asm5.Label)1 MethodVisitor (jodd.asm5.MethodVisitor)1 ProxettaException (jodd.proxetta.ProxettaException)1