Package org.ow2.easybeans.asm

Examples of org.ow2.easybeans.asm.MethodVisitor


     */
    private void generateCallSuperEncodedMethod(final EasyBeansEjbJarMethodMetadata method) {

        String generatedMethodName = MethodRenamer.encode(method.getMethodName());
        JMethod jMethod = method.getJMethod();
        MethodVisitor mv = this.cv.visitMethod(jMethod.getAccess(), generatedMethodName,
                jMethod.getDescriptor(), jMethod.getSignature(), jMethod.getExceptions());

        // Add some flags on the generated method
        CommonClassGenerator.addAnnotationsOnGeneratedMethod(mv);

        mv.visitCode();

        // Add bean (as first argument)
        mv.visitVarInsn(ALOAD, 0);

        // for each argument
        Type[] args = Type.getArgumentTypes(jMethod.getDescriptor());
        int methodArg = 1;
        for (Type type : args) {
            int opCode = CommonClassGenerator.putFieldLoadOpCode(type.getSort());
            mv.visitVarInsn(opCode, methodArg);
            // Double and Long are special parameters
            if (opCode == LLOAD || opCode == DLOAD) {
                methodArg++;
            }
            methodArg++;
        }

        // call super class method()
        mv.visitMethodInsn(INVOKESPECIAL, method.getClassMetadata().getSuperName(),
                jMethod.getName(), jMethod.getDescriptor());

        Type returnType = Type.getReturnType(jMethod.getDescriptor());
        CommonClassGenerator.addReturnType(returnType, mv);


        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here


                    throw new RuntimeException("No generated method name found for interceptorType '" + interceptorType + "'");
        }


        // Generates the body of this method.
        MethodVisitor mv = this.cv.visitMethod(ACC_PUBLIC, generatedMethodName, "()V", null, null);

        // Add some flags on the generated method
        CommonClassGenerator.addAnnotationsOnGeneratedMethod(mv);

        mv.visitCode();
        // Call methods in their order (if any)
        if (existingLifecycleMethods != null) {
            for (EasyBeansEjbJarMethodMetadata method : existingLifecycleMethods) {
                // Inherited or not ?
                String clName = method.getClassMetadata().getClassName();
                mv.visitVarInsn(ALOAD, 0);
                int opcode = INVOKEVIRTUAL;
                if (method.isInherited()) {
                    clName = method.getOriginalClassMetadata().getClassName();
                    opcode = INVOKESPECIAL;
                }
                mv.visitMethodInsn(opcode, clName, method.getMethodName(), method.getJMethod().getDescriptor());
            }
        }


        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

        // add method in the class metadata
        JMethod method = new JMethod(ACC_PUBLIC, generatedMethodName, "()V", null, null);
        EasyBeansEjbJarMethodMetadata generatedMetadata = new EasyBeansEjbJarMethodMetadata(method, classMetaData);
View Full Code Here

        // public CtxImpl(Bean bean, <args of the method>)
        // it is a void type for return type
        this.constructorDesc = "(" + this.beanClassDesc + argsMethodDesc + ")V";

        // Generate constructor
        MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "<init>", this.constructorDesc, null, null);
        mv.visitCode();

        // Call super constructor
        int arg = 1;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

        // Now, set the attributes of the class
        // this.bean = bean
        int argBean = arg++;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, argBean);
        mv
                .visitFieldInsn(PUTFIELD, this.generatedClassName, "bean", encodeClassDesc(this.classAnnotationMetadata
                        .getClassName()));


        // this.factory = bean.getEasyBeansFactory();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, argBean);
        mv.visitMethodInsn(INVOKEVIRTUAL, this.classAnnotationMetadata.getClassName(), "getEasyBeansFactory",
                "()Lorg/ow2/easybeans/api/Factory;");
        mv.visitFieldInsn(PUTFIELD, this.generatedClassName, "factory", "Lorg/ow2/easybeans/api/Factory;");


        // this.interceptorManager = bean.getEasyBeansInterceptorManager();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, argBean);
        mv.visitMethodInsn(INVOKEVIRTUAL, this.classAnnotationMetadata.getClassName(), "getEasyBeansInterceptorManager",
                "()" + encodeClassDesc(this.interceptorManagerClassName));
        mv.visitFieldInsn(PUTFIELD, this.generatedClassName, "interceptorManager",
                encodeClassDesc(this.interceptorManagerClassName));

        // And now, the attributes corresponding to the arguments of the method
        // it will do : this.ARG0 = xxx;
        int methodArg = 0;
        for (Type type : this.methodArgsType) {
            mv.visitVarInsn(ALOAD, 0);
            int opCode = putFieldLoadOpCode(type.getSort());
            mv.visitVarInsn(opCode, arg++);
            mv.visitFieldInsn(PUTFIELD, this.generatedClassName, ARG + (methodArg++), type.getDescriptor());
            // Double and Long are special parameters
            if (opCode == LLOAD || opCode == DLOAD) {
                arg++;
            }
        }


        // this.interceptorXX = interceptorManager.getXXXInterceptor();
        int index = 0;
        for (IJClassInterceptor interceptor : this.allInterceptors) {
            // Only if interceptor is not in the bean class
            if (!interceptor.getClassName().equals(this.beanClassName)) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, this.generatedClassName, "interceptorManager",
                        encodeClassDesc(this.interceptorManagerClassName));
                String getterName = "get" + interceptor.getClassName().replace("/", "");
                mv.visitMethodInsn(INVOKEVIRTUAL, this.interceptorManagerClassName, getterName, "()"
                        + encodeClassDesc(interceptor.getClassName()));
                mv.visitFieldInsn(PUTFIELD, this.generatedClassName, INTERCEPTOR + (index++), encodeClassDesc(interceptor
                        .getClassName()));
            }
        }



        // need to add return instruction
        mv.visitInsn(RETURN);

        // visit max compute automatically
        mv.visitMaxs(0, 0);
        mv.visitEnd();

    }
View Full Code Here

    @Override
    public void visitEnd() {
        super.visitEnd();

        // Inject call to all postConstructs annotations
        MethodVisitor mv = this.cv
                .visitMethod(ACC_PUBLIC + ACC_STATIC, "easyBeansLifeCyclePostConstruct", "()V", null, null);
        mv.visitCode();

        // call super method (if any)
        String superNameClass = this.classAnnotationMetadata.getSuperName();
        if (superNameClass != null && !superNameClass.equals(JAVA_LANG_OBJECT)) {
            EasyBeansEjbJarClassMetadata superMetadata = this.classAnnotationMetadata.getLinkedClassMetadata(superNameClass);
            if (superMetadata != null) {
                mv.visitMethodInsn(INVOKESTATIC, superMetadata.getClassName(), "easyBeansLifeCyclePostConstruct", "()V");
            }
        }

        // call each method annotated
        for (EasyBeansEjbJarMethodMetadata method : this.classAnnotationMetadata.getPostConstructMethodsMetadata()) {
            String clName = method.getClassMetadata().getClassName();
            mv.visitMethodInsn(INVOKESTATIC, clName, method.getMethodName(), method.getJMethod().getDescriptor());
        }

        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here

     *   return bean;
     * }
     * </pre>
     */
    private void addInvocationContextGetTarget() {
        MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "getTarget", "()Ljava/lang/Object;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, this.generatedClassName, "bean", this.beanClassDesc);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here

     *     return this.factory;
     * }
     * </pre>
     */
    private void addEasyBeansInvocationContextGetFactory() {
        MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "getFactory", "()" + EASYBEANS_FACTORY, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, this.generatedClassName, "factory", EASYBEANS_FACTORY);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

    }
View Full Code Here

     *   return method;
     * }
     * </pre>
     */
    private void addInvocationContextGetMethod() {
        MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "getMethod", "()" + JAVA_LANG_REFLECT_METHOD, null, null);
        mv.visitCode();

        // only for around invoke type, lifecycle interceptor should return null
        if (this.interceptorType == AROUND_INVOKE) {

            // if (method == null) {
            mv.visitFieldInsn(GETSTATIC, this.generatedClassName, "method", JAVA_LANG_REFLECT_METHOD);
            // go to this label if not null
            Label notNullParametersLabel = new Label();
            mv.visitJumpInsn(IFNONNULL, notNullParametersLabel);


            // Start of the try block
            Label tryLabel = new Label();
            mv.visitLabel(tryLabel);

            // call a method on the bean class
            mv.visitLdcInsn(this.beanClassType);
            // name of the method which is searched
            mv.visitLdcInsn(this.jMethod.getName());


            // build an array of java.lang.Class with the size of args of the method
            mv.visitIntInsn(BIPUSH, this.methodArgsType.length);
            mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
            int argCount = 0;
            for (Type type : this.methodArgsType) {
                mv.visitInsn(DUP);
                mv.visitIntInsn(BIPUSH, argCount);
                visitClassType(type, mv);
                mv.visitInsn(AASTORE);
                argCount++;
            }

            // signature of the getMethod() method on java.lang.Class class
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class",
                    "getMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");

            // set the result : method = ...
            mv.visitFieldInsn(PUTSTATIC, this.generatedClassName, "method", "Ljava/lang/reflect/Method;");


            // go to the return label
            mv.visitJumpInsn(GOTO, notNullParametersLabel);

            // start of the catch label which throw a runtime exception
            // } catch (SecurityException e) {
            //   throw new RuntimeException("Cannot...", e);
            // }
            Label firstCatchLabel = new Label();
            mv.visitLabel(firstCatchLabel);
            mv.visitVarInsn(ASTORE, 1);
            mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
            mv.visitInsn(DUP);
            mv.visitLdcInsn("Cannot find method due to a security exception");
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                    "(Ljava/lang/String;Ljava/lang/Throwable;)V");
            mv.visitInsn(ATHROW);


            // } catch (NoSuchMethodException e) {
            //   throw new RuntimeException("Cannot...", e);
            // }
            Label secondCatchLabel = new Label();
            mv.visitLabel(secondCatchLabel);
            mv.visitVarInsn(ASTORE, 1);
            mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
            mv.visitInsn(DUP);
            mv.visitLdcInsn("Cannot find the method");
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                    "(Ljava/lang/String;Ljava/lang/Throwable;)V");
            mv.visitInsn(ATHROW);


            // if method is not null, return it
            mv.visitLabel(notNullParametersLabel);
            mv.visitFieldInsn(GETSTATIC, this.generatedClassName, "method", JAVA_LANG_REFLECT_METHOD);
            mv.visitInsn(ARETURN);

            // add try/cacth
            mv.visitTryCatchBlock(tryLabel, firstCatchLabel, firstCatchLabel, "java/lang/SecurityException");
            mv.visitTryCatchBlock(tryLabel, firstCatchLabel, secondCatchLabel, "java/lang/NoSuchMethodException");
        } else {
            // for lifecycle method
            mv.visitInsn(ACONST_NULL);
            mv.visitInsn(ARETURN);
        }


        // finish
        mv.visitMaxs(0, 0);
        mv.visitEnd();


    }
View Full Code Here

     * ie : private static Method method = null
     *      private static InterceptorClass interceptor0 = new MyInterceptor();
     *      private static InterceptorClass2 interceptor1 = ....
     */
    private void addStaticClassInitialization() {
        MethodVisitor mv = getCW().visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();

        // private static Method method = null
        mv.visitInsn(ACONST_NULL);
        mv.visitFieldInsn(PUTSTATIC, this.generatedClassName, "method", JAVA_LANG_REFLECT_METHOD);


        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

    }
View Full Code Here

     *    }
     }
     * </pre>
     */
    private void addInvocationContextProceed() {
        MethodVisitor mv = getCW()
                .visitMethod(ACC_PUBLIC, "proceed", "()" + JAVA_LANG_OBJECT, null, PROCEED_EXCEPTIONS);
        mv.visitCode();


        // interceptor++ or in fact : interceptor = interceptor + 1;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(DUP);
        mv.visitFieldInsn(GETFIELD, this.generatedClassName, "interceptor", "I");
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IADD); // + 1
        mv.visitFieldInsn(PUTFIELD, this.generatedClassName, "interceptor", "I");



        // load interceptor constant to do the switch
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, this.generatedClassName, "interceptor", "I");


        // Size
        int sizeInterceptors = this.allInterceptors.size();

        // need to add call to the original method
        int switchSize = sizeInterceptors + 1;

        // Build array of labels corresponding to swtich entries
        Label[] switchLabels = new Label[switchSize];
        for (int s = 0; s < switchSize; s++) {
            switchLabels[s] = new Label();
        }

        // default label
        Label defaultCaseLabel = new Label();

        // switch
        mv.visitTableSwitchInsn(1, switchSize, defaultCaseLabel, switchLabels);

        // add each interceptor switch entry with a return block at the end
        // ie : case 1 :
        //        return myInterceptor.intercept(this); // interceptor class
        // or case 1 :
        //        return bean.intercept(this) // bean class
        int index = 0;
        int interceptorIndex = 0;
        for (IJClassInterceptor interceptor : this.allInterceptors) {
            mv.visitLabel(switchLabels[index]);

            Type returnType = Type.getReturnType(interceptor.getJMethod().getDescriptor());

            // interceptor on the bean
            if (interceptor.getClassName().equals(this.beanClassName)) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, this.generatedClassName, "bean", this.beanClassDesc);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, this.beanClassName,
                        interceptor.getJMethod().getName(), interceptor.getJMethod().getDescriptor());

                // return object or null if the return type is void
                returnsObject(returnType, mv);
            } else { // interceptor in another class
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, this.generatedClassName, INTERCEPTOR + interceptorIndex ,
                        encodeClassDesc(interceptor.getClassName()));
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, interceptor.getClassName(),
                interceptor.getJMethod().getName(), interceptor.getJMethod().getDescriptor());
                // return object or null if the return type is void
                returnsObject(returnType, mv);
                interceptorIndex++;
            }
            index++;
        }

        // then, add call to original method, ie bean.businessMethod(i,j,...);
        mv.visitLabel(switchLabels[index++]);
        // get bean object
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, this.generatedClassName, "bean", this.beanClassDesc);

        // arguments of the method
        int indexArg = 0;
        for (Type argType : this.methodArgsType) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, this.generatedClassName, ARG + (indexArg++), argType.getDescriptor());
        }

        // Call to the renamed method only for AroundInvoke
        // LifeCycle interceptors call the original method
        String interceptedMethod = null;
        if (this.interceptorType.equals(AROUND_INVOKE)) {
            interceptedMethod = MethodRenamer.encode(this.jMethod.getName());
        } else {
            interceptedMethod = this.jMethod.getName();
        }

        mv.visitMethodInsn(INVOKEVIRTUAL, this.beanClassName, interceptedMethod, this.jMethod.getDescriptor());
        Type returnType = Type.getReturnType(this.jMethod.getDescriptor());
        // return object or null if the return type is void
        returnsObject(returnType, mv);


        // default case
        mv.visitLabel(defaultCaseLabel);
        mv.visitTypeInsn(NEW, "java/lang/IllegalStateException");
        mv.visitInsn(DUP);
        mv.visitLdcInsn("Problem in interceptors. Shouldn't go in the default case.");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V");
        mv.visitInsn(ATHROW);

        // end
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here

     * }
     * </pre>
     *
     */
    public void addInvocationContextGetContextData() {
        MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "getContextData", "()Ljava/util/Map;", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, this.generatedClassName, "contextData", "Ljava/util/Map;");

            Label elseLabel = new Label();
            mv.visitJumpInsn(IFNONNULL, elseLabel);

            // if
            mv.visitVarInsn(ALOAD, 0);
            mv.visitTypeInsn(NEW, "java/util/HashMap");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V");
            mv.visitFieldInsn(PUTFIELD, this.generatedClassName, "contextData", "Ljava/util/Map;");

            // else
            mv.visitLabel(elseLabel);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, this.generatedClassName, "contextData", "Ljava/util/Map;");

            // return
            mv.visitInsn(ARETURN);

            mv.visitMaxs(0, 0);
            mv.visitEnd();


    }
View Full Code Here

TOP

Related Classes of org.ow2.easybeans.asm.MethodVisitor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.