Example usage for org.objectweb.asm Opcodes INVOKESTATIC

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

Introduction

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

Prototype

int INVOKESTATIC

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

Click Source Link

Usage

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

License:Open Source License

private byte[] instantiateConcreteTuple(String dename, List<String> parameters) {
    /*/*from  ww  w.ja  v a  2  s  .c o  m*/
     * 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.fortress.runtimeSystem.InstantiatingClassloader.java

License:Open Source License

/**
 * @param mv/*  ww w.  j a  va2s. c  o  m*/
 * @param cast_to
 */
public static void generalizedInstanceOf(MethodVisitor mv, String cast_to) {
    if (cast_to.startsWith(Naming.UNION_OX)) {
        List<String> cast_to_parameters = RTHelpers.extractStringParameters(cast_to);
        Label done = new Label();
        for (int i = 0; i < cast_to_parameters.size(); i++) {
            mv.visitInsn(DUP); // object to test
            generalizedInstanceOf(mv, cast_to_parameters.get(i)); // replaces obj w/ 1/0
            mv.visitInsn(DUP); // copy for branch test. leave one on TOS
            // eepI(mv,"union instanceof subtest " + cast_to_parameters.get(i));
            mv.visitJumpInsn(IFNE, done);
            mv.visitInsn(POP); // discard unnecessary zero.
        }
        mv.visitLdcInsn(0); // failure
        mv.visitLabel(done);
        mv.visitInsn(SWAP); // put tested obj on TOS
        mv.visitInsn(POP); // discard
    } else if (cast_to.startsWith(Naming.TUPLE_OX)) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONCRETE_ + cast_to, IS_A, "(Ljava/lang/Object;)Z");
    } else if (cast_to.startsWith(Naming.ARROW_OX)) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ABSTRACT_ + cast_to, IS_A, "(Ljava/lang/Object;)Z");
    } else {
        String type = cast_to.equals(Naming.INTERNAL_SNOWMAN)
                ? Naming.specialFortressTypes.get(Naming.INTERNAL_SNOWMAN)
                : cast_to;
        mv.visitTypeInsn(Opcodes.INSTANCEOF, type);
    }
}

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

License:Open Source License

public static void generalizedCastTo(MethodVisitor mv, String cast_to, boolean from_object) {
    if (cast_to.startsWith(Naming.UNION_OX)) {
        // do nothing, it will be erased!
    } else if (cast_to.startsWith(Naming.TUPLE_OX)) {
        List<String> cast_to_parameters = RTHelpers.extractStringParameters(cast_to);
        String any_tuple_n = ANY_TUPLE + Naming.LEFT_OXFORD + cast_to_parameters.size() + Naming.RIGHT_OXFORD;
        String sig = "(" + Naming.internalToDesc(any_tuple_n) + ")L" + cast_to + ";";
        mv.visitTypeInsn(Opcodes.CHECKCAST, any_tuple_n);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, CONCRETE_ + cast_to, CAST_TO, sig);
    } else if (cast_to.startsWith(Naming.ARROW_OX)) {
        List<String> cast_to_parameters = RTHelpers.extractStringParameters(cast_to);
        // mv.visitTypeInsn(Opcodes.CHECKCAST, cast_to);

        Triple<List<String>, List<String>, String> stuff = normalizeArrowParameters(cast_to_parameters);

        List<String> unwrapped_parameters = stuff.getA();
        List<String> tupled_parameters = stuff.getB();
        String tupleType = stuff.getC();

        List<String> objectified_parameters = Useful.applyToAll(unwrapped_parameters, toJLO);
        objectified_parameters = normalizeArrowParametersAndReturn(objectified_parameters);
        String obj_sig = stringListToGeneric(Naming.ARROW_TAG, objectified_parameters);
        //            if (from_object) {
        //                mv.visitTypeInsn(Opcodes.CHECKCAST, obj_sig);
        //            }
        // Experiment with boring signature
        String sig = "(L" + "java/lang/Object" + // obj_sig +
                ";)L" + cast_to + ";";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ABSTRACT_ + cast_to, CAST_TO, sig);

    } else {/*from w  ww . jav a 2s  .c om*/
        String type = cast_to.equals(Naming.INTERNAL_SNOWMAN)
                ? Naming.specialFortressTypes.get(Naming.INTERNAL_SNOWMAN)
                : cast_to;
        mv.visitTypeInsn(Opcodes.CHECKCAST, type);
    }
}

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

License:Open Source License

/**
 * @param rttiClassName//from   w  w  w  .j  a  v  a 2 s .  c  o m
 * @param sparams_size
 */
static public void emitDictionaryAndFactoryForGenericRTTIclass(ManglingClassWriter cw, String rttiClassName,
        int sparams_size, final Naming.XlationData xldata) {

    // Push nulls for opr parameters in the factory call.
    List<Boolean> spks;
    int type_sparams_size = sparams_size;
    if (xldata != null) {
        spks = xldata.isOprKind();
        sparams_size = spks.size();
    } else {
        spks = new InfiniteList<Boolean>(false);
    }

    // FIELD
    // static, initialized to Map-like thing
    cw.visitField(ACC_PRIVATE + ACC_STATIC + ACC_FINAL, "DICTIONARY", Naming.RTTI_MAP_DESC, null, null);

    // CLINIT
    // factory, consulting map, optionally invoking constructor.
    MethodVisitor mv = cw.visitNoMangleMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    // new
    mv.visitTypeInsn(NEW, Naming.RTTI_MAP_TYPE);
    // init
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, Naming.RTTI_MAP_TYPE, "<init>", "()V");
    // store
    mv.visitFieldInsn(PUTSTATIC, rttiClassName, "DICTIONARY", Naming.RTTI_MAP_DESC);

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

    // FACTORY

    boolean useSparamsArray = sparams_size > 6;
    int sparamsArrayIndex = sparams_size;

    String fact_sig = Naming.rttiFactorySig(type_sparams_size);
    String init_sig = InstantiatingClassloader.jvmSignatureForOnePlusNTypes("java/lang/Class",
            type_sparams_size, Naming.RTTI_CONTAINER_TYPE, "V");
    String get_sig;
    String put_sig;
    String getClass_sig;
    if (useSparamsArray) {
        get_sig = Naming.makeMethodDesc(Naming.RTTI_CONTAINER_ARRAY_DESC, Naming.RTTI_CONTAINER_DESC);
        put_sig = Naming.makeMethodDesc(Naming.RTTI_CONTAINER_ARRAY_DESC + Naming.RTTI_CONTAINER_DESC,
                Naming.RTTI_CONTAINER_DESC);
        getClass_sig = Naming.makeMethodDesc(NamingCzar.descString + Naming.RTTI_CONTAINER_ARRAY_DESC,
                NamingCzar.descClass);
    } else {
        get_sig = InstantiatingClassloader.jvmSignatureForNTypes(sparams_size, Naming.RTTI_CONTAINER_TYPE,
                Naming.RTTI_CONTAINER_DESC);
        put_sig = InstantiatingClassloader.jvmSignatureForNTypes(sparams_size + 1, Naming.RTTI_CONTAINER_TYPE,
                Naming.RTTI_CONTAINER_DESC);
        getClass_sig = InstantiatingClassloader.jvmSignatureForOnePlusNTypes(NamingCzar.internalString,
                sparams_size, Naming.RTTI_CONTAINER_TYPE, NamingCzar.descClass);
    }

    mv = cw.visitNoMangleMethod(ACC_PUBLIC + ACC_STATIC, Naming.RTTI_FACTORY, fact_sig, null, null);
    mv.visitCode();
    /* 
     * First arg is java class, necessary for creation of type.
     * 
     * rCN x = DICTIONARY.get(args)
     * if  x == null then
     *   x = new rCN(args)
     *   x = DICTIONARY.put(args, x)
     * end
     * return x
     */

    // object
    mv.visitFieldInsn(GETSTATIC, rttiClassName, "DICTIONARY", Naming.RTTI_MAP_DESC);
    // push args
    int l = sparams_size;
    if (useSparamsArray) {
        mv.visitLdcInsn(sparams_size);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, Naming.RTTI_CONTAINER_TYPE);
        mv.visitVarInsn(Opcodes.ASTORE, sparamsArrayIndex);
        InstantiatingClassloader.pushArgsIntoArray(mv, 0, l, sparamsArrayIndex, spks);
    } else {
        InstantiatingClassloader.pushArgs(mv, 0, l, spks);
    }
    // invoke Dictionary.get
    mv.visitMethodInsn(INVOKEVIRTUAL, Naming.RTTI_MAP_TYPE, "get", get_sig);
    Label not_null = new Label();
    mv.visitInsn(DUP);
    mv.visitJumpInsn(IFNONNULL, not_null);
    mv.visitInsn(POP); // discard dup'd null
    // doing it all on the stack -- (unless too many static params, then use an array for human coded stuff)
    // 1) first push the dictionary and args (array if used) 
    // 2) create new RTTI object
    // 3) push args again (array if used) and create the class for this object
    // 4) push the args again (never array) to init RTTI object
    // 5) add to dictionary

    //1)
    mv.visitFieldInsn(GETSTATIC, rttiClassName, "DICTIONARY", Naming.RTTI_MAP_DESC);

    if (useSparamsArray) {
        mv.visitVarInsn(ALOAD, sparamsArrayIndex);
    } else {
        InstantiatingClassloader.pushArgs(mv, 0, l, spks);
    }

    // 2) invoke constructor
    mv.visitTypeInsn(NEW, rttiClassName);
    mv.visitInsn(DUP);

    // 3) create class for this object
    String stem = Naming.rttiClassToBaseClass(rttiClassName);
    if (true || xldata == null) {
        // NOT symbolic (and a problem if we pretend that it is)
        mv.visitLdcInsn(stem);
    } else {
        RTHelpers.symbolicLdc(mv, stem);
    }
    if (useSparamsArray) {
        mv.visitVarInsn(ALOAD, sparamsArrayIndex);
    } else {
        InstantiatingClassloader.pushArgs(mv, 0, l, spks);
    }

    //(mv, "before getRTTIclass");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Naming.RT_HELPERS, "getRTTIclass", getClass_sig);
    //eep(mv, "after getRTTIclass");

    // 4) init RTTI object (do not use array)
    // NOTE only pushing type_sparams here.
    InstantiatingClassloader.pushArgs(mv, 0, type_sparams_size);
    mv.visitMethodInsn(INVOKESPECIAL, rttiClassName, "<init>", init_sig);
    // 5) add to dictionary
    mv.visitMethodInsn(INVOKEVIRTUAL, Naming.RTTI_MAP_TYPE, "putIfNew", put_sig);

    mv.visitLabel(not_null);
    mv.visitInsn(ARETURN);

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

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

License:Open Source License

/**
 * @param owner//from   ww  w  . j a  v a 2s .  c  o m
 */
public void rttiReference(String owner) {
    int lox_index = owner.indexOf(Naming.LEFT_OXFORD);
    if (lox_index != -1) {
        int rox_index = owner.lastIndexOf(Naming.RIGHT_OXFORD);
        String stem = owner.substring(0, lox_index);
        List<String> parameters = RTHelpers.extractStringParameters(owner, lox_index, rox_index);

        // special case hack for tuples, and arrows
        if (stem.equals(Naming.TUPLE_TAG) || stem.equals("ConcreteTuple")) {
            stem = Naming.tupleRTTIclass(parameters.size());
        } else if (stem.equals(Naming.ARROW_TAG)
                || stem.equals(InstantiatingClassloader.ABSTRACT_ + Naming.ARROW_TAG)) {
            stem = Naming.arrowRTTIclass(parameters.size());
        } else if (stem.contains(Naming.ENVELOPE) && stem.endsWith(Naming.ARROW_TAG)) {
            stem = Naming.arrowRTTIclass(parameters.size());
        } else if (stem.equals(Naming.UNION)) {

        } else {
            // a real class that might have opr parameters.
            // Obtain the xldata for the original type
            Naming.XlationData xldata = icl.xlationForGeneric(owner.substring(0, rox_index + 1));
            List<Boolean> filter = xldata.isOprKind();
            List<String> opr_params = Useful.retain(parameters, filter);
            parameters = Useful.exclude(parameters, filter);
            stem = Naming.oprArgAnnotatedRTTI(stem, opr_params);
        }

        //recursive call
        for (String parameter : parameters) {
            rttiReference(parameter);
        }

        //call the factory
        String stem_rtti = Naming.stemClassToRTTIclass(stem);
        String fact_sig = Naming.rttiFactorySig(parameters.size());
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, stem_rtti, Naming.RTTI_FACTORY, fact_sig);
    } else {
        //just get the field
        String ownerRTTIc = Naming.stemClassToRTTIclass(owner);
        if (ownerRTTIc.startsWith(Naming.SNOWMAN)) {
            ownerRTTIc = ownerRTTIc.replaceFirst(Naming.SNOWMAN, Naming.RT_VALUES_PKG + "FVoid");
        }
        mv.visitFieldInsn(Opcodes.GETSTATIC, ownerRTTIc, Naming.RTTI_SINGLETON, Naming.RTTI_CONTAINER_DESC);
    }
}

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

License:Open Source License

public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    String oname = name;/*from   w ww.j a v a2  s  .c  o  m*/
    String descSplice = ""; // used to transform calls to union/intersection
    if (owner.equals(Naming.magicInterpClass)) {
        name = xlation.getTypeName(name);
        String op = Naming.encodedOp(name);
        String s = Naming.encodedConst(name);
        if (op.equals(Naming.hashMethod)) {
            long hash_sargs = MagicNumbers.hashStringLong(s);
            mv.visitLdcInsn(Long.valueOf(hash_sargs));
        } else if (op.equals(Naming.stringMethod)) {
            mv.visitLdcInsn(s);
        } else {
            throw new Error("Invocation of magic class Method '" + oname + "' ('" + name
                    + "') seen, but op is not recognized.");
        }
    } else {
        String new_owner = xlation.getTypeName(owner); // demangled.
        if (opcode == Opcodes.INVOKEINTERFACE && !new_owner.equals(owner)) {
            if (new_owner.contains(Naming.LEFT_OXFORD)) {
                if (new_owner.startsWith(Naming.UNION_OX)) {
                    // replace invokeinterface with invokestatic, modify desc
                    opcode = Opcodes.INVOKESTATIC;
                    descSplice = Naming.ERASED_UNION_DESC;
                } else if (!new_owner.startsWith(Naming.ARROW_OX) && !new_owner.startsWith(Naming.TUPLE_OX)) {
                    Naming.XlationData xldata = icl.xlationForGeneric(new_owner);
                    String stem_sort = xldata.first();
                    if (stem_sort.equals(Naming.OBJECT_GENERIC_TAG)
                    // || stem_sort.equals(Naming.FUNCTION_GENERIC_TAG)
                    )
                        opcode = Opcodes.INVOKEVIRTUAL;
                    else {
                        // do nothing
                    }
                } else {
                    // do nothing
                }

            } else {
                String new_owner_class_name = Naming.mangleFortressIdentifier(new_owner);
                new_owner_class_name = new_owner_class_name.replaceAll("[/]", ".");
                try {
                    Class cl = Class.forName(new_owner_class_name, true, icl);
                    if (cl.isInterface()) {
                        // Do nothing
                    } else {
                        opcode = Opcodes.INVOKEVIRTUAL;
                    }
                } catch (ClassNotFoundException e) {
                    // Do nothing, not our problem
                }
            }
        }
        name = xlation.getMethodName(name);
        desc = xlation.getMethodDesc(desc);
        if (descSplice != null)
            desc = "(" + descSplice + desc.substring(1);
        mv.visitMethodInsn(opcode, new_owner, name, desc);
    }
}

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();/* w  ww .java  2 s. 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();
}

From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java

License:MIT License

/**
 * Populate a forwarding method of the form
 * "T name() { return Class.forward(this); }".
 *
 * @param method Method to generate code for
 * @param forwardname Name of method to call
 * @param rettype Return type of method//from w w w . ja  v  a 2  s . co m
 * @param thistype Type of object method is being generated on
 * @param forwardtype Type to forward method to
 */
public static void populateForwardingToStaticMethod(MethodNode method, String forwardname, Type rettype,
        Type thistype, Type forwardtype) {
    InsnList code = method.instructions;

    code.add(new VarInsnNode(thistype.getOpcode(Opcodes.ILOAD), 0));
    code.add(new MethodInsnNode(Opcodes.INVOKESTATIC, forwardtype.getInternalName(), forwardname,
            Type.getMethodDescriptor(rettype, thistype), false));
    code.add(new InsnNode(rettype.getOpcode(Opcodes.IRETURN)));
}

From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java

public void testReplacingThisWithOtherVariable() throws Exception {

    final String FIELDPROXYWORKED = "FIELDPROXYWORKED";

    //set up the proxy object. this is the object that will receive
    //the proxied calls
    TestSubClass tcc = new TestSubClass();
    tcc.setBaseString(FIELDPROXYWORKED);

    TestBaseClassHolder.setTestBase(tcc);

    //get the dynamic source that has the donor body in it
    ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF);
    MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout");

    System.out.println("Donor");
    printMethodNode(donorMethod);/*from   w w  w  .j a  v  a 2s  . com*/

    //alright here's the strategy:  (1) inject a new local variable that points 
    //   to our remote instance,
    //  (2) inject code that sets this local to the value of a method call,
    //  (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1

    InsnList instructionsToInject = donorMethod.instructions;

    //make a new local variable
    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    instructionsToInject.insertBefore(instructionsToInject.getFirst(), begin);
    instructionsToInject.add(end);

    Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass");
    int variableIndex = donorMethod.maxLocals;
    donorMethod.maxLocals += type.getSize();
    donorMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(),
            variableIndex);

    //set the value of the local variable with a new instruction at the top
    //fetch a reference to our proxy object
    MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase",
            "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;");

    //insert after begin label
    instructionsToInject.insert(begin, getTestBase);

    //store reference
    VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex);

    //insert store after fetch
    instructionsToInject.insert(getTestBase, setRef);

    //replace all references to 'this'  with the new variable

    for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) {
        AbstractInsnNode node = instructionsToInject.get(currentIndex);
        if (node.getOpcode() == Opcodes.ALOAD) {
            VarInsnNode vin = (VarInsnNode) node;

            //'this' is var index 0. ours is var index varindex
            if (vin.var == 0) {
                vin.var = variableIndex;
            }
        }
    }

    System.out.println(">>>>>>>>>Finished Modifying<<<<<<<<");
    printMethodNode(donorMethod);
    String NEWCLASSNAME = "ScriptTestClass";

    //write a class 
    Class c = createClassFromClassNode(donorSource, NEWCLASSNAME);

    Object o = c.newInstance();
    Method m = o.getClass().getDeclaredMethod("before_whatDoIThinkAbout", String.class);

    //should return HAHAHA not baseStringValue 
    String result = (String) m.invoke(o, new Object[] { "AAAA" });
    System.out.println("TestDonorClass.whatDoIThinkAbout Result: " + result);
    assertTrue(result.equals(FIELDPROXYWORKED + "AAAA"));
}

From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java

@Test
public void testInjectingIntoMethodWithLotsOfParameters() throws Exception {
    final String FIELDPROXYWORKED = "FIELDPROXYWORKED";

    //set up the proxy object. this is the object that will receive
    //the proxied calls
    TestSubClass tcc = new TestSubClass();
    tcc.setBaseString(FIELDPROXYWORKED);

    TestBaseClassHolder.setTestBase(tcc);

    //get the dynamic source that has the donor body in it
    ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF2);
    MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout");

    System.out.println("Donor Method Before Modifications:");
    printMethodNode(donorMethod);/*from   w  w  w. j  a v a2s . c  o m*/

    String TARGETCLASSNAME = "com.triage.bytecodemaster.fortesting.Concatenator";
    ClassNode targetSource = loadLocalClass(TARGETCLASSNAME);

    ClassNode exampleSource = loadLocalClass("com.triage.bytecodemaster.fortesting.JustLikeGroovyClass");
    MethodNode exampleMethod = findMethod(exampleSource, "before_whatDoIThinkAbout");

    System.out.println("Example Method-- Should be just like the Donor Source");
    printMethodNode(exampleMethod);
    MethodNode targetMethod = findMethod(targetSource, "concat");
    System.out.println("Target Method <Before Mods>");
    printMethodNode(targetMethod);

    //alright here's the strategy:  (1) inject a new local variable that points 
    //   to our remote instance,
    //  (2) inject code that sets this local to the value of a method call,
    //  (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1

    InsnList instructionsToInject = donorMethod.instructions;
    InsnList targetInstructions = targetMethod.instructions;

    //make a new local variable in the donor method.
    //this variable needs to have a slot high enough that it doesnt
    //conflict with either the target or the source method
    //it will hold references to the objects we replace with 'this'
    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    targetInstructions.insertBefore(targetInstructions.getFirst(), begin);
    targetInstructions.add(end);

    Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass");
    int variableIndex = targetMethod.maxLocals;
    targetMethod.maxLocals += type.getSize();
    targetMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(),
            variableIndex);

    //set the value of the local variable with a new instruction at the top
    //fetch a reference to our proxy object
    MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase",
            "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;");

    //insert after begin label
    targetInstructions.insert(begin, getTestBase);

    //store reference
    VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex);

    //insert store after fetch
    targetInstructions.insert(getTestBase, setRef);

    //replace all references to 'this' in the DONOR method with the new variable
    //in the TARGET code

    for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) {
        AbstractInsnNode node = instructionsToInject.get(currentIndex);
        if (node.getOpcode() == Opcodes.ALOAD) {
            VarInsnNode vin = (VarInsnNode) node;

            //'this' is var index 0. ours is var index varindex
            if (vin.var == 0) {
                vin.var = variableIndex;
            }
        }

        //remove return methods. this will prevent a return. it should cause the donor 
        //method to have parameters that overlap with the target, which has more parameters
        if (node.getOpcode() == Opcodes.RETURN || node.getOpcode() == Opcodes.ARETURN) {
            instructionsToInject.remove(node);
        }
    }

    System.out.println(">>>>>>>>>Finished Modifying Donor Method <<<<<<<<");
    printMethodNode(donorMethod);
    String NEWCLASSNAME = "ScriptTestClass";

    //stash instructions at the beginning of the original method, 
    //but after populating the new variable
    targetInstructions.insert(setRef, instructionsToInject);

    System.out.println("Modified Target:");
    printMethodNode(targetMethod);

    //write a class 
    Class c = createClassFromClassNode(targetSource, TARGETCLASSNAME);

    Object o = c.newInstance();
    Method m = o.getClass().getDeclaredMethod("concat", String.class, String.class, String.class, String.class);

    //should return HAHAHA not baseStringValue 
    String result = (String) m.invoke(o, new Object[] { "A", "B", "C", "D" });
    System.out.println("Concatenator.concat Result: " + result);
    assertTrue(result.equals("ABCD"));

}