Package org.objectweb.asm

Examples of org.objectweb.asm.CodeVisitor


      returnType = OBJECT;

    String methodDescriptor = getMethodDescriptor( returnType, paramTypes );

    // Generate method body
    CodeVisitor cv = cw.visitMethod(
      modifiers, methodName, methodDescriptor, exceptions );

    if ( (modifiers & ACC_ABSTRACT) != 0 )
      return;

    // Generate code to push the BSHTHIS or BSHSTATIC field
    if ( isStatic )
    {
      cv.visitFieldInsn(
        GETSTATIC, fqClassName, BSHSTATIC+className, "Lbsh/This;" );
    }else
    {
      // Push 'this'
      cv.visitVarInsn( ALOAD, 0 );

      // Get the instance field
      cv.visitFieldInsn(
        GETFIELD, fqClassName, BSHTHIS+className, "Lbsh/This;" );
    }

    // Push the name of the method as a constant
      cv.visitLdcInsn( methodName );

    // Generate code to push arguments as an object array
    generateParameterReifierCode( paramTypes, isStatic, cv );

    // Push nulls for various args of invokeMethod
    cv.visitInsn(ACONST_NULL); // interpreter
    cv.visitInsn(ACONST_NULL); // callstack
    cv.visitInsn(ACONST_NULL); // callerinfo

    // Push the boolean constant 'true' (for declaredOnly)
    cv.visitInsn(ICONST_1);

    // Invoke the method This.invokeMethod( name, Class [] sig, boolean )
    cv.visitMethodInsn(
      INVOKEVIRTUAL, "bsh/This", "invokeMethod",
      Type.getMethodDescriptor(
        Type.getType(Object.class),
        new Type [] {
          Type.getType(String.class),
          Type.getType(Object [].class),
          Type.getType(Interpreter.class),
          Type.getType(CallStack.class),
          Type.getType(SimpleNode.class),
          Type.getType(Boolean.TYPE)
        }
      )
    );

    // Generate code to unwrap bsh Primitive types
    cv.visitMethodInsn(
      INVOKESTATIC, "bsh/Primitive", "unwrap",
      "(Ljava/lang/Object;)Ljava/lang/Object;" );

    // Generate code to return the value
    generateReturnCode( returnType, cv );

    // Need to calculate this... just fudging here for now.
    cv.visitMaxs( 20, 20 );
  }
View Full Code Here


    String [] exceptions = null;
    String methodDescriptor = getMethodDescriptor( "V", paramTypes );

    // Create this constructor method
    CodeVisitor cv =
      cw.visitMethod( modifiers, "<init>", methodDescriptor, exceptions );

    // Generate code to push arguments as an object array
    generateParameterReifierCode( paramTypes, false/*isStatic*/, cv );
    cv.visitVarInsn( ASTORE, argsVar );

    // Generate the code implementing the alternate constructor switch
    generateConstructorSwitch( index, argsVar, consArgsVar, cv );

    // Generate code to invoke the ClassGeneratorUtil initInstance() method

    // push 'this'
    cv.visitVarInsn( ALOAD, 0 );

    // Push the class/constructor name as a constant
      cv.visitLdcInsn( className );

    // Push arguments as an object array
    cv.visitVarInsn( ALOAD, argsVar );

    // invoke the initInstance() method
    cv.visitMethodInsn(
      INVOKESTATIC, "bsh/ClassGeneratorUtil", "initInstance",
      "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");

    cv.visitInsn( RETURN );

    // Need to calculate this... just fudging here for now.
    cv.visitMaxs( 20, 20 );
  }
View Full Code Here

      returnType = OBJECT;

    String methodDescriptor = getMethodDescriptor( returnType, paramTypes );

    // Add method body
    CodeVisitor cv = cw.visitMethod(
      modifiers, "_bshSuper"+methodName, methodDescriptor, exceptions );

    cv.visitVarInsn(ALOAD, 0);
    // Push vars
    int localVarIndex = 1;
    for (int i = 0; i < paramTypes.length; ++i)
    {
      if ( isPrimitive( paramTypes[i]) )
        cv.visitVarInsn(ILOAD, localVarIndex);
      else
        cv.visitVarInsn(ALOAD, localVarIndex);
      localVarIndex +=
        ( (paramTypes[i].equals("D") || paramTypes[i].equals("J"))
          ? 2 : 1 );
    }

    cv.visitMethodInsn( INVOKESPECIAL,
      superClassName, methodName, methodDescriptor );

    generatePlainReturnCode( returnType, cv );

    // Need to calculate this... just fudging here for now.
    cv.visitMaxs( 20, 20 );
  }
View Full Code Here

            final IndexTuple[] aroundAdvices,
            final IndexTuple[] beforeAdvices,
            final IndexTuple[] afterAdvices,
            final System system) {

        CodeVisitor cv =
                cw.visitMethod(
                        Constants.ACC_PUBLIC, INIT_METHOD_NAME, JIT_JOIN_POINT_INIT_METHOD_SIGNATURE, null, null
                );

        cv.visitVarInsn(Constants.ALOAD, 0);
        cv.visitVarInsn(Constants.ALOAD, 1);
        cv.visitVarInsn(Constants.ILOAD, 2);
        cv.visitVarInsn(Constants.ALOAD, 3);
        cv.visitVarInsn(Constants.ALOAD, 6);
        cv.visitInsn(Constants.ACONST_NULL);
        cv.visitInsn(Constants.ACONST_NULL);
        cv.visitInsn(Constants.ACONST_NULL);
        cv.visitMethodInsn(
                Constants.INVOKESPECIAL, JOIN_POINT_BASE_CLASS_NAME,
                INIT_METHOD_NAME, JOIN_POINT_BASE_INIT_METHOD_SIGNATURE
        );

        // init the stack frame field
        cv.visitVarInsn(Constants.ALOAD, 0);
        cv.visitInsn(Constants.ICONST_M1);
        cv.visitFieldInsn(Constants.PUTFIELD, className, STACKFRAME_FIELD_NAME, I);

        // init the signature field
        cv.visitVarInsn(Constants.ALOAD, 0);
        cv.visitVarInsn(Constants.ALOAD, 4);
        switch (joinPointType) {
            case JoinPointType.METHOD_EXECUTION:
            case JoinPointType.METHOD_CALL:
                cv.visitTypeInsn(Constants.CHECKCAST, METHOD_SIGNATURE_IMPL_CLASS_NAME);
                cv.visitFieldInsn(
                        Constants.PUTFIELD, className, SIGNATURE_FIELD_NAME, METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.CONSTRUCTOR_CALL:
            case JoinPointType.CONSTRUCTOR_EXECUTION:
                cv.visitTypeInsn(Constants.CHECKCAST, CONSTRUCTOR_SIGNATURE_IMPL_CLASS_NAME);
                cv.visitFieldInsn(
                        Constants.PUTFIELD, className, SIGNATURE_FIELD_NAME,
                        CONSTRUCTOR_SIGNATURE_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.FIELD_SET:
            case JoinPointType.FIELD_GET:
                cv.visitTypeInsn(Constants.CHECKCAST, FIELD_SIGNATURE_IMPL_CLASS_NAME);
                cv.visitFieldInsn(
                        Constants.PUTFIELD, className, SIGNATURE_FIELD_NAME, FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.HANDLER:
                throw new UnsupportedOperationException("handler is not support yet");

            case JoinPointType.STATIC_INITALIZATION:
                throw new UnsupportedOperationException("static initialization is not support yet");
        }

        // init the rtti field
        cv.visitVarInsn(Constants.ALOAD, 0);
        cv.visitVarInsn(Constants.ALOAD, 5);
        switch (joinPointType) {
            case JoinPointType.METHOD_EXECUTION:
            case JoinPointType.METHOD_CALL:
                cv.visitTypeInsn(Constants.CHECKCAST, METHOD_RTTI_IMPL_CLASS_NAME);
                cv.visitFieldInsn(Constants.PUTFIELD, className, RTTI_FIELD_NAME, METHOD_RTTI_IMPL_CLASS_SIGNATURE);
                break;

            case JoinPointType.CONSTRUCTOR_CALL:
            case JoinPointType.CONSTRUCTOR_EXECUTION:
                cv.visitTypeInsn(Constants.CHECKCAST, CONSTRUCTOR_RTTI_IMPL_CLASS_NAME);
                cv.visitFieldInsn(
                        Constants.PUTFIELD, className, RTTI_FIELD_NAME,
                        CONSTRUCTOR_RTTI_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.FIELD_SET:
            case JoinPointType.FIELD_GET:
                cv.visitTypeInsn(Constants.CHECKCAST, FIELD_RTTI_IMPL_CLASS_NAME);
                cv.visitFieldInsn(Constants.PUTFIELD, className, RTTI_FIELD_NAME, FIELD_RTTI_IMPL_CLASS_SIGNATURE);
                break;

            case JoinPointType.HANDLER:
                throw new UnsupportedOperationException("handler is not support yet");

            case JoinPointType.STATIC_INITALIZATION:
                throw new UnsupportedOperationException("static initialization is not support yet");
        }

        // init the system field
        cv.visitVarInsn(Constants.ALOAD, 0);
        cv.visitVarInsn(Constants.ALOAD, 1);
        cv.visitMethodInsn(
                Constants.INVOKESTATIC, SYSTEM_LOADER_CLASS_NAME, GET_SYSTEM_METHOD_NAME,
                GET_SYSTEM_METHOD_NAME_SIGNATURE
        );
        cv.visitFieldInsn(Constants.PUTFIELD, className, SYSTEM_FIELD_NAME, SYSTEM_CLASS_SIGNATURE);

        // init the aspect fields
        for (int i = 0; i < aroundAdvices.length; i++) {
            if (initAspectField(system, aroundAdvices[i], cw, AROUND_ADVICE_FIELD_PREFIX + i, cv, className)) {
                return true;
            }
        }
        for (int i = 0; i < beforeAdvices.length; i++) {
            if (initAspectField(system, beforeAdvices[i], cw, BEFORE_ADVICE_FIELD_PREFIX + i, cv, className)) {
                return true;
            }
        }
        for (int i = 0; i < afterAdvices.length; i++) {
            if (initAspectField(system, afterAdvices[i], cw, AFTER_ADVICE_FIELD_PREFIX + i, cv, className)) {
                return true;
            }
        }

        cv.visitInsn(Constants.RETURN);
        cv.visitMaxs(0, 0);

        return false;
    }
View Full Code Here

    private static void createGetSignatureMethod(
            final int joinPointType,
            final ClassWriter cw,
            final String className) {

        CodeVisitor cv =
                cw.visitMethod(
                        Constants.ACC_PUBLIC, GET_SIGNATURE_METHOD_NAME, GET_SIGNATURE_METHOD_SIGNATURE, null,
                        null
                );

        cv.visitVarInsn(Constants.ALOAD, 0);

        switch (joinPointType) {
            case JoinPointType.METHOD_EXECUTION:
            case JoinPointType.METHOD_CALL:
                cv.visitFieldInsn(
                        Constants.GETFIELD, className, SIGNATURE_FIELD_NAME,
                        METHOD_SIGNATURE_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.CONSTRUCTOR_CALL:
            case JoinPointType.CONSTRUCTOR_EXECUTION:
                cv.visitFieldInsn(
                        Constants.GETFIELD, className, SIGNATURE_FIELD_NAME,
                        CONSTRUCTOR_SIGNATURE_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.FIELD_SET:
            case JoinPointType.FIELD_GET:
                cv.visitFieldInsn(
                        Constants.GETFIELD, className, SIGNATURE_FIELD_NAME,
                        FIELD_SIGNATURE_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.HANDLER:
                throw new UnsupportedOperationException("handler is not support yet");

            case JoinPointType.STATIC_INITALIZATION:
                throw new UnsupportedOperationException("static initialization is not support yet");
        }

        cv.visitInsn(Constants.ARETURN);
        cv.visitMaxs(0, 0);
    }
View Full Code Here

    private static void createGetRttiMethod(
            final int joinPointType,
            final ClassWriter cw,
            final String className) {

        CodeVisitor cv =
                cw.visitMethod(
                        Constants.ACC_PUBLIC, GET_RTTI_METHOD_NAME, GET_RTTI_METHOD_SIGNATURE, null,
                        null
                );

        cv.visitVarInsn(Constants.ALOAD, 0);

        switch (joinPointType) {
            case JoinPointType.METHOD_EXECUTION:
            case JoinPointType.METHOD_CALL:
                cv.visitFieldInsn(
                        Constants.GETFIELD, className, RTTI_FIELD_NAME,
                        METHOD_RTTI_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.CONSTRUCTOR_CALL:
            case JoinPointType.CONSTRUCTOR_EXECUTION:
                cv.visitFieldInsn(
                        Constants.GETFIELD, className, RTTI_FIELD_NAME,
                        CONSTRUCTOR_RTTI_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.FIELD_SET:
            case JoinPointType.FIELD_GET:
                cv.visitFieldInsn(
                        Constants.GETFIELD, className, RTTI_FIELD_NAME,
                        FIELD_RTTI_IMPL_CLASS_SIGNATURE
                );
                break;

            case JoinPointType.HANDLER:
                throw new UnsupportedOperationException("handler is not support yet");

            case JoinPointType.STATIC_INITALIZATION:
                throw new UnsupportedOperationException("static initialization is not support yet");
        }

        cv.visitInsn(Constants.ARETURN);
        cv.visitMaxs(0, 0);
    }
View Full Code Here

            final RttiInfo signatureCflowExprStruct,
            final IndexTuple[] aroundAdvice,
            final IndexTuple[] beforeAdvice,
            final IndexTuple[] afterAdvice) {

        CodeVisitor cv = cw.visitMethod(
                Constants.ACC_PUBLIC | Constants.ACC_FINAL,
                PROCEED_METHOD_NAME, PROCEED_METHOD_SIGNATURE,
                new String[]{THROWABLE_CLASS_NAME}, null
        );

        incrementStackFrameCounter(cv, className);

        Labels labels = invokeAdvice(
                cv, className, aroundAdvice, beforeAdvice, afterAdvice, system, signatureCflowExprStruct
        );

        resetStackFrameCounter(cv, className);

        invokeJoinPoint(joinPointType, system, declaringClass, joinPointHash, cv, className);

        cv.visitInsn(Constants.ARETURN);

        cv.visitLabel(labels.handlerLabel);
        cv.visitVarInsn(Constants.ASTORE, 2);
        cv.visitLabel(labels.endLabel);
        cv.visitVarInsn(Constants.ALOAD, 0);
        cv.visitInsn(Constants.ICONST_M1);
        cv.visitFieldInsn(Constants.PUTFIELD, className, STACKFRAME_FIELD_NAME, I);
        cv.visitVarInsn(Constants.ALOAD, 2);
        cv.visitInsn(Constants.ATHROW);

        // handle the final try-finally clause
        cv.visitTryCatchBlock(labels.startLabel, labels.returnLabels[0], labels.handlerLabel, null);
        for (int i = 1; i < labels.switchCaseLabels.length; i++) {
            Label switchCaseLabel = labels.switchCaseLabels[i];
            Label returnLabel = labels.returnLabels[i];
            cv.visitTryCatchBlock(switchCaseLabel, returnLabel, labels.handlerLabel, null);
        }
        cv.visitTryCatchBlock(labels.handlerLabel, labels.endLabel, labels.handlerLabel, null);

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

            } else if (CLINIT_METHOD_NAME.equals(name)) {
                // skip clinit
                ;
            } else if (INIT_METHOD_NAME.equals(name)) {
                // constructors
                CodeVisitor proxyCode = m_proxyCv.visitMethod(
                        access + ACC_SYNTHETIC,
                        INIT_METHOD_NAME,
                        desc,
                        exceptions,
                        attrs
                );

                proxyCode.visitVarInsn(ALOAD, 0);
                AsmHelper.loadArgumentTypes(proxyCode, Type.getArgumentTypes(desc), false);
                proxyCode.visitMethodInsn(INVOKESPECIAL, m_className, INIT_METHOD_NAME, desc);
                proxyCode.visitInsn(RETURN);
                proxyCode.visitMaxs(0, 0);
            } else {
                // method that can be proxied
                CodeVisitor proxyCode = m_proxyCv.visitMethod(
                        access + ACC_SYNTHETIC,
                        name,
                        desc,
                        exceptions,
                        attrs
                );

                if (Modifier.isStatic(access)) {
                    AsmHelper.loadArgumentTypes(proxyCode, Type.getArgumentTypes(desc), true);
                    proxyCode.visitMethodInsn(INVOKESTATIC, m_className, name, desc);
                    proxyCode.visitInsn(RETURN);
                    proxyCode.visitMaxs(0, 0);
                } else {
                    proxyCode.visitVarInsn(ALOAD, 0);
                    AsmHelper.loadArgumentTypes(proxyCode, Type.getArgumentTypes(desc), false);
                    proxyCode.visitMethodInsn(INVOKESPECIAL, m_className, name, desc);
                    proxyCode.visitInsn(RETURN);
                    proxyCode.visitMaxs(0, 0);

                }
            }

            return AsmAnnotationHelper.NullCodeAdapter.NULL_CODE_ADAPTER;
View Full Code Here

                null,
                null
        );

        // ctor
        CodeVisitor cv = m_cw.visitMethod(
                ACC_PUBLIC,
                INIT_METHOD_NAME,
                NO_PARAM_RETURN_VOID_SIGNATURE,
                EMPTY_STRING_ARRAY,
                null
        );
        // invoke the constructor of abstract
        cv.visitVarInsn(ALOAD, 0);
        cv.visitMethodInsn(INVOKESPECIAL, ABSTRACT_CFLOW_CLASS, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
        // assign the singleton field to this
        cv.visitVarInsn(ALOAD, 0);
        cv.visitFieldInsn(PUTSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, ABSTRACT_CFLOW_SIGNATURE);
        cv.visitInsn(RETURN);
        cv.visitMaxs(0, 0);

        // static isInCflow() delegators
        cv = m_cw.visitMethod(
                ACC_PUBLIC + ACC_STATIC,
                IS_IN_CFLOW_METOD_NAME,
                IS_IN_CFLOW_METOD_SIGNATURE,
                EMPTY_STRING_ARRAY,
                null
        );
        Label isNull = new Label();
        cv.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, ABSTRACT_CFLOW_SIGNATURE);
        cv.visitJumpInsn(IFNULL, isNull);
        cv.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, ABSTRACT_CFLOW_SIGNATURE);
        cv.visitMethodInsn(INVOKEVIRTUAL, ABSTRACT_CFLOW_CLASS, IN_CFLOW_METOD_NAME, IN_CFLOW_METOD_SIGNATURE);
        cv.visitInsn(IRETURN);
        cv.visitLabel(isNull);
        cv.visitInsn(ICONST_0);
        cv.visitInsn(IRETURN);
        cv.visitMaxs(0, 0);

        // static isInCflowBelow() delegators
        cv = m_cw.visitMethod(
                ACC_PUBLIC + ACC_STATIC,
                IS_IN_CFLOWBELOW_METOD_NAME,
                IS_IN_CFLOWBELOW_METOD_SIGNATURE,
                EMPTY_STRING_ARRAY,
                null
        );
        Label isNull2 = new Label();
        cv.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, ABSTRACT_CFLOW_SIGNATURE);
        cv.visitJumpInsn(IFNULL, isNull2);
        cv.visitFieldInsn(GETSTATIC, m_className, INSTANCE_CFLOW_FIELD_NAME, ABSTRACT_CFLOW_SIGNATURE);
        cv.visitMethodInsn(INVOKEVIRTUAL, ABSTRACT_CFLOW_CLASS, IN_CFLOWBELOW_METOD_NAME, IN_CFLOWBELOW_METOD_SIGNATURE);
        cv.visitInsn(IRETURN);
        cv.visitLabel(isNull2);
        cv.visitInsn(ICONST_0);
        cv.visitInsn(IRETURN);
        cv.visitMaxs(0, 0);

        m_cw.visitEnd();

        return m_cw.toByteArray();
    }
View Full Code Here

            if (methodInfo != null) {
                // visit the method to access the parameter names as required to support Aspect with bindings
                // TODO: should we make this optional - similar to m_lazyAttributes ?
                Type[] parameterTypes = Type.getArgumentTypes(desc);
                if (parameterTypes.length > 0) {
                    CodeVisitor methodParameterNamesVisitor = new MethodParameterNamesCodeAdapter(
                            Modifier.isStatic(access),
                            parameterTypes.length, methodInfo
                    );
                    return methodParameterNamesVisitor;
                } else {
View Full Code Here

TOP

Related Classes of org.objectweb.asm.CodeVisitor

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.