Example usage for org.objectweb.asm Opcodes RETURN

List of usage examples for org.objectweb.asm Opcodes RETURN

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes RETURN.

Prototype

int RETURN

To view the source code for org.objectweb.asm Opcodes RETURN.

Click Source Link

Usage

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Implementing "static reflection" for the method putRaw so the
 * interpreter uses a switch instruction for ***PutRaw
 * based on the hash values of String names in this namespace.
 *//*from  w w w  .  j  a v a2s  . c  om*/
private static void writeMethodPutRaw(ClassWriter cw, String className, String methodName,
        EnvironmentClass environmentClass, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            "(" + STRING_DESCRIPTOR + environmentClass.descriptor() + ")V", null, null);
    mv.visitCode();

    Label beginFunction = new Label();
    mv.visitLabel(beginFunction);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_INTERNALNAME, "hashCode", "()I");
    mv.visitVarInsn(Opcodes.ISTORE, 3);
    Label beginLoop = new Label();
    mv.visitLabel(beginLoop);

    Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass);
    ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet());
    Collections.sort(sortedCodes);
    Label notFound = new Label();
    putRawHelper(mv, className, environmentClass, hashCodeRelation, sortedCodes, notFound);
    mv.visitLabel(notFound);
    mv.visitInsn(Opcodes.RETURN);
    Label endFunction = new Label();
    mv.visitLabel(endFunction);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0);
    mv.visitLocalVariable("queryString", STRING_DESCRIPTOR, null, beginFunction, endFunction, 1);
    mv.visitLocalVariable("value", environmentClass.descriptor(), null, beginFunction, endFunction, 2);
    mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 3);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 4);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void putRawBaseCase(MethodVisitor mv, String className,
        Relation<String, Integer> hashCodeRelation, EnvironmentClass environmentClass, int code,
        Label notFound) {/*from w  w w .  j  ava2  s .com*/

    PredicateSet<String> strings = hashCodeRelation.matchSecond(code);
    for (String testString : strings) {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(testString);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
        Label afterSetValue = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, afterSetValue);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        String idString = testString + environmentClass.namespace();
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, Naming.mangleIdentifier(idString),
                environmentClass.descriptor());
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(afterSetValue);
    }
    mv.visitJumpInsn(Opcodes.GOTO, notFound);
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void writeNullSetter(ClassWriter cw, String className, String methodName, String signature) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, signature, null, null);
    mv.visitCode();// w w w . j  av a 2  s  . c  o m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l1, 0);
    mv.visitLocalVariable("str", "Ljava/lang/String;", null, l0, l1, 1);
    mv.visitLocalVariable("f2", "Ljava/lang/Number;", null, l0, l1, 2);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(0, 3);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void writeRemoveMethod(ClassWriter cw, String className, String methodName, String invokeMethod,
        EnvironmentClass environmentClass) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(Ljava/lang/String;)V", null, null);
    mv.visitCode();//from w  w w .  j a v  a 2s  .  c o  m
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, invokeMethod,
            "(Ljava/lang/String;" + environmentClass.descriptor() + ")V");
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitInsn(Opcodes.RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, l0, l2, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l2, 1);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(3, 2);
    mv.visitEnd();
}

From source file:com.sun.fortress.runtimeSystem.Instantiater.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    // necessary?
    name = oprs.getMethodName(name);/*from  w ww .  j ava 2  s. c om*/
    //System.out.println("old desc=" + desc);
    //desc = types.getMethodDesc(desc);
    //System.out.println("new desc=" + desc);
    String newDesc = types.getMethodDesc(desc);
    MethodVisitor mv = cv.visitMethod(access, name, newDesc, signature, exceptions);

    if (!desc.equals(newDesc)) { // catch flattened tuples
        String params = desc.substring(desc.indexOf("(") + 1, //TODO: wrong if nested parens
                desc.indexOf(")"));
        String newParams = newDesc.substring(newDesc.indexOf("(") + 1, newDesc.indexOf(")"));
        if (params.split(";").length == 1 && //single generic parameter 
                newParams.startsWith("LTuple")) { //tuple substituted in
            //System.out.println(access + " " + name + " " + signature + " " +this.instanceName);
            if ((this.access_flags & Opcodes.ACC_INTERFACE) == 0 && //not in an interface
                    (access & Opcodes.ACC_STATIC) == 0) { //and not a static method, so generate a body  

                //extract the parameters and create strings for the types 
                List<String> paramList = InstantiationMap.extractStringParameters(newParams,
                        newParams.indexOf(Naming.LEFT_OXFORD),
                        InstantiationMap.templateClosingRightOxford(newParams), new ArrayList<String>());
                String rawParams = "";
                for (String p : paramList)
                    rawParams = rawParams + Naming.internalToDesc(p);
                final String altDesc = newDesc.substring(0, newDesc.indexOf("(") + 1) + rawParams
                        + newDesc.substring(newDesc.indexOf(")"), newDesc.length());
                String tuple_params = InstantiatingClassloader.stringListToTuple(paramList);
                String make_sig = InstantiatingClassloader.toJvmSig(paramList,
                        Naming.javaDescForTaggedFortressType(tuple_params));

                MethodVisitor altMv = cv.visitMethod(access, name, altDesc, signature, exceptions);

                altMv.visitVarInsn(Opcodes.ALOAD, 0); //load this

                final int n = paramList.size(); //load the parameters
                for (int i = 1; i <= n; i++) {
                    altMv.visitVarInsn(Opcodes.ALOAD, i);
                }
                altMv.visitMethodInsn(Opcodes.INVOKESTATIC, InstantiatingClassloader.CONCRETE_ + tuple_params,
                        "make", make_sig); //create a tuple from the parameters

                if (name.equals("<init>")) {
                    altMv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instanceName, name, newDesc); //call original method
                    altMv.visitInsn(Opcodes.RETURN); //return
                } else {
                    altMv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.instanceName, name, newDesc); //call original method
                    altMv.visitInsn(Opcodes.ARETURN); //return
                }
                altMv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
                altMv.visitEnd();
            }
        }
    }
    return new MethodInstantiater(mv, types, icl);
}

From source file:com.sun.fortress.runtimeSystem.InstantiatingClassloader.java

License:Open Source License

private byte[] instantiateConcreteTuple(String dename, List<String> parameters) {
    /*/*from  w  w w.jav  a2  s  .com*/
     * extends AnyConcreteTuple[\ N \]
     * 
     * implements Tuple[\ parameters \]
     * 
     * defines f1 ... fN
     * defines e1 ... eN
     * defines o1 ... oN
     */

    ManglingClassWriter cw = new ManglingClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);

    final int n = parameters.size();
    final String any_tuple_n = ANY_TUPLE + Naming.LEFT_OXFORD + n + Naming.RIGHT_OXFORD;
    final String any_concrete_tuple_n = ANY_CONCRETE_TUPLE + Naming.LEFT_OXFORD + n + Naming.RIGHT_OXFORD;
    final String tuple_params = stringListToTuple(parameters);

    String[] superInterfaces = { tuple_params };

    cw.visit(JVM_BYTECODE_VERSION, ACC_PUBLIC + ACC_SUPER, dename, null, any_concrete_tuple_n, superInterfaces);

    /* Outline of what must be generated:
            
    // fields
            
    // init method
            
    // factory method
              
    // getRTTI method
            
    // is instance method -- takes an Object
            
    // is instance method
              
    // cast method
            
    // typed getters
            
    // untyped getters
             
    */

    // fields
    {
        for (int i = 0; i < n; i++) {
            String f = TUPLE_FIELD_PFX + (i + Naming.TUPLE_ORIGIN);
            String sig = Naming.internalToDesc(parameters.get(i));
            cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, f, sig, null /* for non-generic */,
                    null /* instance has no value */);
        }
    }
    // init method
    {
        String init_sig = tupleParamsToJvmInitSig(parameters);
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", init_sig, null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, any_concrete_tuple_n, "<init>", Naming.voidToVoid);

        for (int i = 0; i < n; i++) {
            String f = TUPLE_FIELD_PFX + (i + Naming.TUPLE_ORIGIN);
            String sig = Naming.internalToDesc(parameters.get(i));

            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, i + 1);
            mv.visitFieldInsn(Opcodes.PUTFIELD, dename, f, sig);
        }
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // factory method -- same args as init, returns a new one.
    {
        String init_sig = tupleParamsToJvmInitSig(parameters);
        String make_sig = toJvmSig(parameters, Naming.javaDescForTaggedFortressType(tuple_params));
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "make", make_sig, null,
                null);

        mv.visitCode();
        // eep(mv, "before new");
        mv.visitTypeInsn(NEW, dename);
        mv.visitInsn(DUP);
        // push params for init
        for (int i = 0; i < n; i++) {
            mv.visitVarInsn(Opcodes.ALOAD, i);
        }
        // eep(mv, "before init");
        mv.visitMethodInsn(INVOKESPECIAL, dename, "<init>", init_sig);

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // getRTTI method/field and static initialization
    {
        final String classname = dename;
        MethodVisitor mv = cw.visitNoMangleMethod(Opcodes.ACC_PUBLIC, // acccess
                Naming.RTTI_GETTER, // name
                Naming.STATIC_PARAMETER_GETTER_SIG, // sig
                null, // generics sig?
                null); // exceptions
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, classname, Naming.RTTI_FIELD, Naming.RTTI_CONTAINER_DESC);

        areturnEpilogue(mv);

        MethodVisitor imv = cw.visitMethod(ACC_STATIC, "<clinit>", Naming.voidToVoid, null, null);
        //taken from codegen.emitRttiField   
        InitializedStaticField isf = new InitializedStaticField.StaticForRttiFieldOfTuple(classname, this);
        isf.forClinit(imv);
        cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, isf.asmName(), isf.asmSignature(),
                null /* for non-generic */, null /* instance has no value */);

        imv.visitInsn(RETURN);
        imv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        imv.visitEnd();

    }

    // is instance method -- takes an Object
    {
        String sig = "(Ljava/lang/Object;)Z";
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, IS_A, sig, null, null);

        Label fail = new Label();

        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, any_tuple_n);
        mv.visitJumpInsn(Opcodes.IFEQ, fail);

        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.CHECKCAST, any_tuple_n);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, dename, IS_A, "(" + Naming.internalToDesc(any_tuple_n) + ")Z");
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitLabel(fail);
        mv.visitIntInsn(BIPUSH, 0);
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // is instance method -- takes an AnyTuple[\N\]
    {
        String sig = "(" + Naming.internalToDesc(any_tuple_n) + ")Z";
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, IS_A, sig, null, null);

        Label fail = new Label();

        for (int i = 0; i < n; i++) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(INVOKEINTERFACE, any_tuple_n, TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + i),
                    UNTYPED_GETTER_SIG);

            String cast_to = parameters.get(i);

            generalizedInstanceOf(mv, cast_to);

            mv.visitJumpInsn(Opcodes.IFEQ, fail);

        }

        mv.visitIntInsn(BIPUSH, 1);
        mv.visitInsn(Opcodes.IRETURN);

        mv.visitLabel(fail);
        mv.visitIntInsn(BIPUSH, 0);
        mv.visitInsn(Opcodes.IRETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // cast method
    {
        String sig = "(" + Naming.internalToDesc(any_tuple_n) + ")" + Naming.internalToDesc(tuple_params);
        String make_sig = toJvmSig(parameters, Naming.javaDescForTaggedFortressType(tuple_params));

        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, CAST_TO, sig, null, null);

        // Get the parameters to make, and cast them.
        for (int i = 0; i < n; i++) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(INVOKEINTERFACE, any_tuple_n, TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + i),
                    UNTYPED_GETTER_SIG);
            String cast_to = parameters.get(i);
            generalizedCastTo(mv, cast_to);
        }

        mv.visitMethodInsn(INVOKESTATIC, dename, "make", make_sig);

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
        mv.visitEnd();
    }

    // typed getters
    // untyped getters
    for (int i = 0; i < n; i++) {
        String untyped = TUPLE_OBJECT_ELT_PFX + (Naming.TUPLE_ORIGIN + i);
        String typed = TUPLE_TYPED_ELT_PFX + (Naming.TUPLE_ORIGIN + i);
        String field = TUPLE_FIELD_PFX + (Naming.TUPLE_ORIGIN + i);
        String param_type = parameters.get(i);
        String param_desc = Naming.internalToDesc(param_type);
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, untyped, UNTYPED_GETTER_SIG, null, null);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, dename, field, param_desc);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
            mv.visitEnd();
        }
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, typed, "()" + param_desc, null, null);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, dename, field, param_desc);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(Naming.ignoredMaxsParameter, Naming.ignoredMaxsParameter);
            mv.visitEnd();
        }
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.sun.tdk.jcov.instrument.StaticInvokeMethodAdapter.java

License:Open Source License

@Override
public void visitInsn(int opcode) {

    switch (opcode) {
    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.ARETURN:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
    case Opcodes.RETURN:
        if (params.isInnerInvacationsOff()
                && Utils.isAdvanceStaticInstrAllowed(className, methName/*"<init>"*/)) {
            if (!methName.equals("<clinit>")) {
                int id = 0;
                super.visitLdcInsn(id);
                super.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/CollectDetect", "setExpected",
                        "(I)V");
            } else {
                super.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/CollectDetect", "leaveClinit",
                        "()V");
            }/*from w w  w  .  j av  a 2s.  co m*/
        }
        break;
    default:
        break;
    }

    super.visitInsn(opcode);
}

From source file:com.taobao.profile.instrument.ProfMethodAdapter.java

License:Open Source License

public void visitInsn(int inst) {
    switch (inst) {
    case Opcodes.ARETURN:
    case Opcodes.DRETURN:
    case Opcodes.FRETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.RETURN:
    case Opcodes.ATHROW:
        this.visitLdcInsn(mMethodId);
        this.visitMethodInsn(INVOKESTATIC, "com/taobao/profile/Profiler", "End", "(I)V");
        break;/*  w w  w  . ja  v a2  s .  co m*/
    default:
        break;
    }

    super.visitInsn(inst);
}

From source file:com.tencent.tinker.build.auxiliaryclass.AuxiliaryClassGenerator.java

License:Open Source License

private static void generateClass(String dotClassName, File fileOut) throws IOException {
    final String classDesc = dotClassName.replace('.', '/');
    ClassWriter cw = new ClassWriter(0);
    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classDesc, null, "java/lang/Object", null);
    cw.visitSource(fileOut.getName(), null);
    {//from   ww  w . j  av a2s  . c o  m
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    byte[] classBytes = cw.toByteArray();

    OutputStream os = null;
    try {
        os = new BufferedOutputStream(new FileOutputStream(fileOut));
        os.write(classBytes);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception e) {
                // Ignored.
            }
        }
    }
}

From source file:com.tencent.tinker.build.auxiliaryclass.AuxiliaryClassInjectAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    // If method <clinit> and <init> are not found, we should generate a <clinit>.
    if (!this.isClInitExists && !this.isInitExists) {
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();/*ww w .  ja v a 2s  . c  o  m*/
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "lineSeparator", "()Ljava/lang/String;",
                false);
        Label lblSkipInvalidInsn = new Label();
        mv.visitJumpInsn(Opcodes.IFNONNULL, lblSkipInvalidInsn);
        mv.visitLdcInsn(Type.getType(this.auxiliaryClassDesc));
        mv.visitVarInsn(Opcodes.ASTORE, 0);
        mv.visitLabel(lblSkipInvalidInsn);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    super.visitEnd();
}