Example usage for org.objectweb.asm Opcodes AALOAD

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

Introduction

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

Prototype

int AALOAD

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

Click Source Link

Usage

From source file:jerl.bcm.inj.impl.InjectCollection.java

License:Apache License

public void injectMemberFieldArraySystemOut(MethodVisitor mv, String clazz, String field, String type,
        String printType, String prefix, boolean isStatic, int index) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn(prefix);// w w w  .  j  a  va2s .  co m
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V");

    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    if (isStatic) {
        mv.visitFieldInsn(Opcodes.GETSTATIC, clazz, field, type);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 0); // this
        mv.visitFieldInsn(Opcodes.GETFIELD, clazz, field, type);
    }
    mv.visitLdcInsn(new Integer(index));

    if (type.endsWith("[Z") || type.endsWith("[B")) {
        mv.visitInsn(Opcodes.BALOAD);
    } else if (type.endsWith("[C")) {
        mv.visitInsn(Opcodes.CALOAD);
    } else if (type.endsWith("[D")) {
        mv.visitInsn(Opcodes.DALOAD);
    } else if (type.endsWith("[F")) {
        mv.visitInsn(Opcodes.FALOAD);
    } else if (type.endsWith("[I")) {
        mv.visitInsn(Opcodes.IALOAD);
    } else if (type.endsWith("[J")) {
        mv.visitInsn(Opcodes.LALOAD);
    } else if (type.endsWith("[S")) {
        mv.visitInsn(Opcodes.SALOAD);
    } else {
        mv.visitInsn(Opcodes.AALOAD);
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(" + printType + ")V");
}

From source file:lucee.transformer.bytecode.BytecodeFactory.java

License:Open Source License

@Override
public void registerKey(Context c, Expression name, boolean doUpperCase) throws TransformerException {
    BytecodeContext bc = (BytecodeContext) c;
    if (name instanceof Literal) {
        Literal l = (Literal) name;//  w w w. j  ava  2  s . c o  m

        LitString ls = name instanceof LitString ? (LitString) l
                : c.getFactory().createLitString(l.getString());
        if (doUpperCase) {
            ls = ls.duplicate();
            ls.upperCase();
        }
        String key = KeyConstants.getFieldName(ls.getString());
        if (key != null) {
            bc.getAdapter().getStatic(KEY_CONSTANTS, key, Types.COLLECTION_KEY);
            return;
        }
        int index = bc.registerKey(ls);
        bc.getAdapter().visitVarInsn(Opcodes.ALOAD, 0);
        bc.getAdapter().visitFieldInsn(Opcodes.GETFIELD, bc.getClassName(), "keys",
                Types.COLLECTION_KEY_ARRAY.toString());
        bc.getAdapter().push(index);
        bc.getAdapter().visitInsn(Opcodes.AALOAD);

        //ExpressionUtil.writeOutSilent(lit,bc, Expression.MODE_REF);
        //bc.getAdapter().invokeStatic(Page.KEY_IMPL, Page.KEY_INTERN);

        return;
    }
    name.writeOut(bc, Expression.MODE_REF);
    bc.getAdapter().invokeStatic(Page.KEY_IMPL, INIT);
    //bc.getAdapter().invokeStatic(Types.CASTER, TO_KEY);
    return;
}

From source file:lucee.transformer.bytecode.expression.var.Variable.java

License:Open Source License

public static void registerKey(BytecodeContext bc, Expression name, boolean doUpperCase)
        throws BytecodeException {

    if (name instanceof Literal) {
        Literal l = (Literal) name;//  w ww .  j  a v  a2 s .c o  m

        LitString ls = name instanceof LitString ? (LitString) l : LitString.toLitString(l.getString());
        if (doUpperCase) {
            ls = ls.duplicate();
            ls.upperCase();
        }
        String key = KeyConstants.getFieldName(ls.getString());
        if (key != null) {
            bc.getAdapter().getStatic(KEY_CONSTANTS, key, Types.COLLECTION_KEY);
            return;
        }
        int index = bc.registerKey(ls);
        bc.getAdapter().visitVarInsn(Opcodes.ALOAD, 0);
        bc.getAdapter().visitFieldInsn(Opcodes.GETFIELD, bc.getClassName(), "keys",
                Types.COLLECTION_KEY_ARRAY.toString());
        bc.getAdapter().push(index);
        bc.getAdapter().visitInsn(Opcodes.AALOAD);

        return;
    }
    name.writeOut(bc, MODE_REF);
    bc.getAdapter().invokeStatic(Page.KEY_IMPL, INIT);
    //bc.getAdapter().invokeStatic(Types.CASTER, TO_KEY);
    return;
}

From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java

License:Open Source License

private static byte[] _createMethod(Type type, Class clazz, Method method, Resource classRoot, String className)
        throws IOException {
    Class<?> rtn = method.getReturnType();
    Type rtnType = Type.getType(rtn);

    className = className.replace('.', File.separatorChar);
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, ASM_METHOD.getInternalName(), null);

    // CONSTRUCTOR

    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, null, null, cw);

    Label begin = new Label();
    adapter.visitLabel(begin);/*from   w w w .  j  a v  a2s .c om*/
    adapter.loadThis();

    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitVarInsn(Opcodes.ALOAD, 2);

    adapter.invokeConstructor(ASM_METHOD, CONSTRUCTOR);
    adapter.visitInsn(Opcodes.RETURN);

    Label end = new Label();
    adapter.visitLabel(end);

    adapter.endMethod();

    /*
             
        GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,CONSTRUCTOR,null,null,cw);
                
        Label begin = new Label();
         adapter.visitLabel(begin);
       adapter.loadThis();
                 
        // clazz
         adapter.visitVarInsn(Opcodes.ALOAD, 2);
                 
         // parameterTypes 
         Class<?>[] params = method.getParameterTypes();
         Type[] paramTypes = new Type[params.length];
        ArrayVisitor av=new ArrayVisitor();
        av.visitBegin(adapter, Types.CLASS, params.length);
        for(int i=0;i<params.length;i++){
           paramTypes[i]=Type.getType(params[i]);
           av.visitBeginItem(adapter, i);
     loadClass(adapter,params[i]);
           av.visitEndItem(adapter);
        }
        av.visitEnd();
                
       adapter.invokeConstructor(ASM_METHOD, ASM_METHOD_CONSTRUCTOR);
       adapter.visitInsn(Opcodes.RETURN);
               
       Label end = new Label();
       adapter.visitLabel(end);
               
       adapter.endMethod();
     */

    // METHOD getName();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_NAME, null, null, cw);
    adapter.push(method.getName());
    adapter.visitInsn(Opcodes.ARETURN);
    adapter.endMethod();

    // METHOD getModifiers();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_MODIFIERS, null, null, cw);
    adapter.push(method.getModifiers());
    adapter.visitInsn(Opcodes.IRETURN);
    adapter.endMethod();

    // METHOD getReturnType();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_RETURN_TYPE_AS_STRING, null, null, cw);

    adapter.push(method.getReturnType().getName());
    adapter.visitInsn(Opcodes.ARETURN);

    adapter.endMethod();

    // METHOD INVOKE
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, INVOKE, null, null, cw);
    Label start = adapter.newLabel();
    adapter.visitLabel(start);

    // load Object
    if (!isStatic) {
        adapter.visitVarInsn(Opcodes.ALOAD, 1);
        adapter.checkCast(type);
    }

    // load params
    Class<?>[] params = method.getParameterTypes();

    Type[] paramTypes = new Type[params.length];
    for (int i = 0; i < params.length; i++) {
        paramTypes[i] = Type.getType(params[i]);
    }

    for (int i = 0; i < params.length; i++) {
        adapter.visitVarInsn(Opcodes.ALOAD, 2);
        adapter.push(i);
        //adapter.visitInsn(Opcodes.ICONST_0);
        adapter.visitInsn(Opcodes.AALOAD);

        adapter.checkCast(toReferenceType(params[i], paramTypes[i]));

        // cast
        if (params[i] == boolean.class)
            adapter.invokeVirtual(Types.BOOLEAN, BOOL_VALUE);
        else if (params[i] == short.class)
            adapter.invokeVirtual(Types.SHORT, SHORT_VALUE);
        else if (params[i] == int.class)
            adapter.invokeVirtual(Types.INTEGER, INT_VALUE);
        else if (params[i] == float.class)
            adapter.invokeVirtual(Types.FLOAT, FLT_VALUE);
        else if (params[i] == long.class)
            adapter.invokeVirtual(Types.LONG, LONG_VALUE);
        else if (params[i] == double.class)
            adapter.invokeVirtual(Types.DOUBLE, DBL_VALUE);
        else if (params[i] == char.class)
            adapter.invokeVirtual(Types.CHARACTER, CHR_VALUE);
        else if (params[i] == byte.class)
            adapter.invokeVirtual(Types.BYTE, BYT_VALUE);
        //else adapter.checkCast(paramTypes[i]);

    }

    // call method
    final org.objectweb.asm.commons.Method m = new org.objectweb.asm.commons.Method(method.getName(), rtnType,
            paramTypes);
    if (isStatic)
        adapter.invokeStatic(type, m);
    else
        adapter.invokeVirtual(type, m);

    // return
    if (rtn == void.class)
        ASMConstants.NULL(adapter);

    // cast result to object
    if (rtn == boolean.class)
        adapter.invokeStatic(Types.BOOLEAN, BOOL_VALUE_OF);
    else if (rtn == short.class)
        adapter.invokeStatic(Types.SHORT, SHORT_VALUE_OF);
    else if (rtn == int.class)
        adapter.invokeStatic(Types.INTEGER, INT_VALUE_OF);
    else if (rtn == long.class)
        adapter.invokeStatic(Types.LONG, LONG_VALUE_OF);
    else if (rtn == float.class)
        adapter.invokeStatic(Types.FLOAT, FLT_VALUE_OF);
    else if (rtn == double.class)
        adapter.invokeStatic(Types.DOUBLE, DBL_VALUE_OF);
    else if (rtn == char.class)
        adapter.invokeStatic(Types.CHARACTER, CHR_VALUE_OF);
    else if (rtn == byte.class)
        adapter.invokeStatic(Types.BYTE, BYT_VALUE_OF);

    adapter.visitInsn(Opcodes.ARETURN);

    adapter.endMethod();

    if (classRoot != null) {
        Resource classFile = classRoot.getRealResource(className + ".class");
        return store(cw.toByteArray(), classFile);
    }
    return cw.toByteArray();
}

From source file:org.actorsguildframework.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Create or get a MessageCaller implementation for the given method.
 * @param ownerClass the class that owns the message
 * @param method the method to invoke// ww  w.ja v a2s. c o m
 * @return the message caller
 * @throws NoSuchMethodException 
 * @throws SecurityException 
 */
@SuppressWarnings("unchecked")
public static Class<MessageCaller<?>> createMessageCaller(Class<?> ownerClass, Method method)
        throws SecurityException, NoSuchMethodException {

    String className = String.format("%s_%s_%d__MESSAGECALLER", ownerClass.getName(), method.getName(),
            getMethodNumber(method));
    String classNameInternal = className.replace('.', '/');
    java.lang.reflect.Type fullReturnType = method.getGenericReturnType();
    if ((!(fullReturnType instanceof ParameterizedType))
            && AsyncResult.class.isAssignableFrom(((Class) ((ParameterizedType) fullReturnType).getRawType())))
        throw new RuntimeException("Something's wrong here: should not be called for such a method");
    String returnSignature = GenericTypeHelper
            .getSignature(((ParameterizedType) fullReturnType).getActualTypeArguments()[0]);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;

    cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC,
            classNameInternal, "L" + classNameInternal + "<" + returnSignature + ">;",
            "org/actorsguildframework/internal/MessageCaller", null);
    cw.visitSource(null, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "org/actorsguildframework/internal/MessageCaller", "<init>",
                "()V");
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke",
                "(Lorg/actorsguildframework/Actor;[Ljava/lang/Object;)Lorg/actorsguildframework/AsyncResult;",
                "(Lorg/actorsguildframework/Actor;[Ljava/lang/Object;)Lorg/actorsguildframework/AsyncResult<"
                        + returnSignature + ">;",
                null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);

        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(method.getDeclaringClass()) + "__ACTORPROXY");

        int idx = 0;
        for (Class<?> t : method.getParameterTypes()) {
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitIntInsn(Opcodes.BIPUSH, idx);
            mv.visitInsn(Opcodes.AALOAD);
            if (t.isPrimitive()) {
                String wrapperDescr = GenerationUtils.getWrapperInternalName(t);
                mv.visitTypeInsn(Opcodes.CHECKCAST, wrapperDescr);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, wrapperDescr, t.getName() + "Value",
                        "()" + Type.getDescriptor(t));
            } else {
                if (isArgumentFreezingRequired(method, idx, t)) {
                    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(SerializableFreezer.class));
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(SerializableFreezer.class),
                            "get", Type.getMethodDescriptor(SerializableFreezer.class.getMethod("get")));
                }
                if (!t.equals(Object.class))
                    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(t));
            }
            idx++;
        }
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                Type.getInternalName(method.getDeclaringClass()) + "__ACTORPROXY",
                String.format(SUPER_CALLER_NAME_FORMAT, method.getName()), Type.getMethodDescriptor(method));

        mv.visitInsn(Opcodes.ARETURN);

        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l2, 0);
        mv.visitLocalVariable("instance", "Lorg/actorsguildframework/Actor;", null, l0, l2, 1);
        mv.visitLocalVariable("arguments", "[Ljava/lang/Object;", null, l0, l2, 2);

        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getMessageName", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLdcInsn(method.getName());
        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return (Class<MessageCaller<?>>) GenerationUtils.loadClass(className, cw.toByteArray());
}

From source file:org.adjective.stout.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;//from  www.  j  a  v  a2s.c om
    case Opcodes.ACONST_NULL:
        push("null", Object.class);
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        push(Integer.toString(opcode - Opcodes.ICONST_0), Type.INT_TYPE);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(Integer.toString(opcode - Opcodes.LCONST_0), Type.LONG_TYPE);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(Integer.toString(opcode - Opcodes.FCONST_0), Type.FLOAT_TYPE);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(Integer.toString(opcode - Opcodes.DCONST_0), Type.DOUBLE_TYPE);
        break;
    case Opcodes.IALOAD:
    case Opcodes.LALOAD:
    case Opcodes.FALOAD:
    case Opcodes.DALOAD:
    case Opcodes.AALOAD:
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.SALOAD: {
        Type opType = getType(Opcodes.IALOAD, opcode);
        StackValue idx = pop(Type.INT_TYPE);
        StackValue arr = popArray(opType);
        push(arr.description + "[" + idx.description + "]", opType);
    }
        break;
    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE: {
        Type opType = getType(Opcodes.IASTORE, opcode);
        pop(opType);
        pop(Type.INT_TYPE);
        popArray(opType);
    }
        break;
    case Opcodes.POP:
        pop();
        break;
    case Opcodes.POP2:
        pop(2);
        break;
    case Opcodes.DUP:
        push(peek());
        break;
    case Opcodes.DUP2:
        push(peek(2));
        push(peek(1));
        break;
    case Opcodes.DUP_X1: {
        StackValue a = pop();
        StackValue b = pop();
        push(a);
        push(b);
        push(a);
    }
        break;
    case Opcodes.DUP_X2: {
        StackValue a = pop();
        StackValue b = pop();
        StackValue c = pop();
        push(a);
        push(c);
        push(b);
        push(a);
    }
        break;
    case Opcodes.DUP2_X1: {
        StackValue a = popValue(false);
        StackValue b = pop();
        StackValue c = pop();
        push(b);
        push(a);
        push(c);
        push(b);
        push(a);
    }
    case Opcodes.DUP2_X2: {
        StackValue a = popValue(false);
        StackValue b = pop();
        StackValue c = popValue(false);
        StackValue d = pop();
        push(b);
        push(a);
        push(d);
        push(c);
        push(b);
        push(a);
    }
        break;
    case Opcodes.SWAP: {
        StackValue a = pop();
        StackValue b = pop();
        push(a);
        push(b);
    }
        break;
    case Opcodes.IADD:
    case Opcodes.LADD:
    case Opcodes.FADD:
    case Opcodes.DADD:
        math(Opcodes.IADD, opcode, "+");
        break;
    case Opcodes.ISUB:
    case Opcodes.LSUB:
    case Opcodes.FSUB:
    case Opcodes.DSUB:
        math(Opcodes.ISUB, opcode, "-");
        break;
    case Opcodes.IMUL:
    case Opcodes.LMUL:
    case Opcodes.FMUL:
    case Opcodes.DMUL:
        math(Opcodes.IMUL, opcode, "*");
        break;
    case Opcodes.IDIV:
    case Opcodes.LDIV:
    case Opcodes.FDIV:
    case Opcodes.DDIV:
        math(Opcodes.IDIV, opcode, "/");
        break;
    case Opcodes.IREM:
    case Opcodes.LREM:
    case Opcodes.FREM:
    case Opcodes.DREM:
        math(Opcodes.IREM, opcode, "%");
        break;
    case Opcodes.IAND:
    case Opcodes.LAND:
        math(Opcodes.IAND, opcode, "&");
        break;
    case Opcodes.IOR:
    case Opcodes.LOR:
        math(Opcodes.IOR, opcode, "|");
        break;
    case Opcodes.IXOR:
    case Opcodes.LXOR:
        math(Opcodes.IXOR, opcode, "^");
        break;
    case Opcodes.INEG:
    case Opcodes.LNEG:
    case Opcodes.FNEG:
    case Opcodes.DNEG: {
        Type type = getType(Opcodes.INEG, opcode);
        StackValue a = pop(type);
        push("-" + a.description, type);
    }
        break;
    case Opcodes.ISHL:
    case Opcodes.LSHL: {
        Type type = getType(Opcodes.ISHL, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + "<<" + n.description, type);
    }
        break;
    case Opcodes.ISHR:
    case Opcodes.LSHR: {
        Type type = getType(Opcodes.ISHR, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + ">>" + n.description, type);
    }
        break;
    case Opcodes.IUSHR:
    case Opcodes.LUSHR: {
        Type type = getType(Opcodes.IUSHR, opcode);
        StackValue n = pop(Type.INT_TYPE);
        StackValue a = pop(type);
        push(a.description + ">>>" + n.description, type);
    }
    case Opcodes.LCMP: {
        StackValue a = pop(Type.LONG_TYPE);
        StackValue b = pop(Type.LONG_TYPE);
        push(a.description + " cmp " + b.description + " {-1|0|1}", Type.LONG_TYPE);
    }
        break;
    case Opcodes.I2L:
    case Opcodes.I2F:
    case Opcodes.I2D:
    case Opcodes.L2I:
    case Opcodes.L2F:
    case Opcodes.L2D:
    case Opcodes.F2I:
    case Opcodes.F2L:
    case Opcodes.F2D:
    case Opcodes.D2I:
    case Opcodes.D2L:
    case Opcodes.D2F:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
        cast(opcode);
        break;
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
        popObject();
        break;
    case Opcodes.RETURN:
        break;
    default:
        throw new IllegalArgumentException("Unsupported opcode " + opcode + " - " + OPCODES[opcode]);
    }
    print(opcode, "");
    /* 
        *        FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN,
        *        FRETURN, DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW,
        *        MONITORENTER, or MONITOREXIT
      */

}

From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java

License:Apache License

@Deprecated
public static void genLoadParameters(int argumentIndex, MethodVisitor mv, Method method) {
    Class<?>[] parameters = method.getParameterTypes();
    int size = parameters.length;
    for (int i = 0; i < size; i++) {
        // unpack argument from Object[]
        mv.visitVarInsn(Opcodes.ALOAD, argumentIndex);
        BytecodeHelper.pushConstant(mv, i);
        mv.visitInsn(Opcodes.AALOAD);

        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        BytecodeHelper.doCast(mv, parameters[i]);
    }/*from   w  ww.  jav a  2  s . c  o m*/
}

From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java

License:Apache License

private static MethodVisitor writeMethod(ClassWriter cw, String name, int argumentCount,
        final String superClass, CachedMethod cachedMethod, String receiverType, String parameterDescription,
        boolean useArray) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "call" + name,
            "(L" + receiverType + ";" + parameterDescription + ")Ljava/lang/Object;", null, null);
    mv.visitCode();/*from   ww  w .j ava2  s  . c o m*/

    final Label tryStart = new Label();
    mv.visitLabel(tryStart);

    // call for checking if method is still valid
    for (int i = 0; i < argumentCount; ++i)
        mv.visitVarInsn(Opcodes.ALOAD, i);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClass, "checkCall",
            "(Ljava/lang/Object;" + parameterDescription + ")Z", false);
    Label l0 = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, l0);

    // valid method branch

    Class callClass = cachedMethod.getDeclaringClass().getTheClass();
    boolean useInterface = callClass.isInterface();

    String type = BytecodeHelper.getClassInternalName(callClass.getName());
    String descriptor = BytecodeHelper.getMethodDescriptor(cachedMethod.getReturnType(),
            cachedMethod.getNativeParameterTypes());

    // prepare call
    int invokeMethodCode = Opcodes.INVOKEVIRTUAL;
    if (cachedMethod.isStatic()) {
        invokeMethodCode = Opcodes.INVOKESTATIC;
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        BytecodeHelper.doCast(mv, callClass);
        if (useInterface)
            invokeMethodCode = Opcodes.INVOKEINTERFACE;
    }

    Class<?>[] parameters = cachedMethod.getPT();
    int size = parameters.length;
    for (int i = 0; i < size; i++) {
        if (useArray) {
            // unpack argument from Object[]
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            BytecodeHelper.pushConstant(mv, i);
            mv.visitInsn(Opcodes.AALOAD);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
        }

        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        BytecodeHelper.doCast(mv, parameters[i]);
    }

    // make call
    mv.visitMethodInsn(invokeMethodCode, type, cachedMethod.getName(), descriptor, useInterface);

    // produce result
    BytecodeHelper.box(mv, cachedMethod.getReturnType());
    if (cachedMethod.getReturnType() == void.class) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

    // return
    mv.visitInsn(Opcodes.ARETURN);

    // fall back after method change
    mv.visitLabel(l0);
    for (int i = 0; i < argumentCount; ++i)
        mv.visitVarInsn(Opcodes.ALOAD, i);
    if (!useArray) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ArrayUtil", "createArray",
                "(" + parameterDescription + ")[Ljava/lang/Object;", false);
    }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/callsite/CallSiteArray",
            "defaultCall" + name, "(Lorg/codehaus/groovy/runtime/callsite/CallSite;L" + receiverType
                    + ";[Ljava/lang/Object;)Ljava/lang/Object;",
            false);
    mv.visitInsn(Opcodes.ARETURN);

    // exception unwrapping for stackless exceptions
    final Label tryEnd = new Label();
    mv.visitLabel(tryEnd);
    final Label catchStart = new Label();
    mv.visitLabel(catchStart);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "unwrap",
            "(Lgroovy/lang/GroovyRuntimeException;)Ljava/lang/Throwable;", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "groovy/lang/GroovyRuntimeException");

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

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Creates the method invoking wrapper method.
 *///from   www. jav a2s .co m
private static void createMethod(Class clazz, String name, Method refMethod, ClassWriter cw, String methodName,
        String desc, boolean argsParams, boolean returnValue, Class... parameterTypes) {

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS, methodName, desc, null, null);

    boolean isStatic = Modifier.isStatic(refMethod.getModifiers());
    boolean isInteface = Modifier.isInterface(refMethod.getDeclaringClass().getModifiers());

    final int invokeCode;
    if (isStatic) {
        invokeCode = Opcodes.INVOKESTATIC;
    } else {
        invokeCode = isInteface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(clazz));
    }

    if (argsParams) {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitIntInsn(Opcodes.BIPUSH, i);
            mv.visitInsn(Opcodes.AALOAD);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    } else {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    }

    mv.visitMethodInsn(invokeCode, Type.getInternalName(clazz), name, Type.getMethodDescriptor(refMethod));

    if (returnValue) {
        prepareResult(mv, refMethod);
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitMaxs(1, 1); // ignored since ClassWriter set as ClassWriter.COMPUTE_MAXS
    mv.visitEnd();
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitInsn(final int opcode) {
    T t = null;//from   www .j  a va 2s .  co  m
    int iValue = Integer.MIN_VALUE;
    Object oValue = null;

    switch (opcode) {
    case Opcodes.NOP:
        // nothing to do, ignore
        break;
    /*******
     * ADD *
     *******/
    case Opcodes.DADD:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FADD:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IADD:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LADD:
        if (t == null) {
            t = T.LONG;
        }
        add(new ADD(this.ops.size(), opcode, this.line, t));
        break;
    /*********
     * ALOAD *
     *********/
    case Opcodes.AALOAD:
        t = T.REF;
        // fall through
    case Opcodes.BALOAD:
        if (t == null) {
            t = T.SMALL;
        }
        // fall through
    case Opcodes.CALOAD:
        if (t == null) {
            t = T.CHAR;
        }
        // fall through
    case Opcodes.DALOAD:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FALOAD:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IALOAD:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LALOAD:
        if (t == null) {
            t = T.LONG;
        }
        // fall through
    case Opcodes.SALOAD:
        if (t == null) {
            t = T.SHORT;
        }
        add(new ALOAD(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * AND *
     *******/
    case Opcodes.IAND:
        t = T.AINT;
        // fall through
    case Opcodes.LAND:
        if (t == null) {
            t = T.LONG;
        }
        add(new AND(this.ops.size(), opcode, this.line, t));
        break;
    /***************
     * ARRAYLENGTH *
     ***************/
    case Opcodes.ARRAYLENGTH:
        add(new ARRAYLENGTH(this.ops.size(), opcode, this.line));
        break;
    /**********
     * ASTORE *
     **********/
    case Opcodes.AASTORE:
        t = T.REF;
        // fall through
    case Opcodes.BASTORE:
        if (t == null) {
            t = T.SMALL;
        }
        // fall through
    case Opcodes.CASTORE:
        if (t == null) {
            t = T.CHAR;
        }
        // fall through
    case Opcodes.DASTORE:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FASTORE:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IASTORE:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LASTORE:
        if (t == null) {
            t = T.LONG;
        }
        // fall through
    case Opcodes.SASTORE:
        if (t == null) {
            t = T.SHORT;
        }
        add(new ASTORE(this.ops.size(), opcode, this.line, t));
        break;
    /********
     * CAST *
     ********/
    case Opcodes.D2F:
        t = T.DOUBLE;
        oValue = T.FLOAT;
        // fall through
    case Opcodes.D2I:
        if (t == null) {
            t = T.DOUBLE;
            oValue = T.INT;
        }
        // fall through
    case Opcodes.D2L:
        if (t == null) {
            t = T.DOUBLE;
            oValue = T.LONG;
        }
        // fall through
    case Opcodes.F2D:
        if (t == null) {
            t = T.FLOAT;
            oValue = T.DOUBLE;
        }
        // fall through
    case Opcodes.F2I:
        if (t == null) {
            t = T.FLOAT;
            oValue = T.INT;
        }
        // fall through
    case Opcodes.F2L:
        if (t == null) {
            t = T.FLOAT;
            oValue = T.LONG;
        }
        // fall through
    case Opcodes.I2B:
        if (t == null) {
            t = T.INT;
            oValue = T.BYTE;
        }
        // fall through
    case Opcodes.I2C:
        if (t == null) {
            t = T.INT;
            oValue = T.CHAR;
        }
        // fall through
    case Opcodes.I2D:
        if (t == null) {
            t = T.INT;
            oValue = T.DOUBLE;
        }
        // fall through
    case Opcodes.I2F:
        if (t == null) {
            t = T.INT;
            oValue = T.FLOAT;
        }
        // fall through
    case Opcodes.I2L:
        if (t == null) {
            t = T.INT;
            oValue = T.LONG;
        }
        // fall through
    case Opcodes.I2S:
        if (t == null) {
            t = T.INT;
            oValue = T.SHORT;
        }
        // fall through
    case Opcodes.L2D:
        if (t == null) {
            t = T.LONG;
            oValue = T.DOUBLE;
        }
        // fall through
    case Opcodes.L2F:
        if (t == null) {
            t = T.LONG;
            oValue = T.FLOAT;
        }
        // fall through
    case Opcodes.L2I:
        if (t == null) {
            t = T.LONG;
            oValue = T.INT;
        }
        assert oValue instanceof T;
        add(new CAST(this.ops.size(), opcode, this.line, t, (T) oValue));
        break;
    /*******
     * CMP *
     *******/
    case Opcodes.DCMPG:
        t = T.DOUBLE;
        iValue = CMP.T_G;
        // fall through
    case Opcodes.DCMPL:
        if (t == null) {
            t = T.DOUBLE;
            iValue = CMP.T_L;
        }
        // fall through
    case Opcodes.FCMPG:
        if (t == null) {
            t = T.FLOAT;
            iValue = CMP.T_G;
        }
        // fall through
    case Opcodes.FCMPL:
        if (t == null) {
            t = T.FLOAT;
            iValue = CMP.T_L;
        }
        // fall through
    case Opcodes.LCMP:
        if (t == null) {
            t = T.LONG;
            iValue = CMP.T_0;
        }
        add(new CMP(this.ops.size(), opcode, this.line, t, iValue));
        break;
    /*******
     * DIV *
     *******/
    case Opcodes.DDIV:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FDIV:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IDIV:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LDIV:
        if (t == null) {
            t = T.LONG;
        }
        add(new DIV(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * DUP *
     *******/
    case Opcodes.DUP:
        oValue = DUP.Kind.DUP;
        // fall through
    case Opcodes.DUP_X1:
        if (oValue == null) {
            oValue = DUP.Kind.DUP_X1;
        }
        // fall through
    case Opcodes.DUP_X2:
        if (oValue == null) {
            oValue = DUP.Kind.DUP_X2;
        }
        // fall through
    case Opcodes.DUP2:
        if (oValue == null) {
            oValue = DUP.Kind.DUP2;
        }
        // fall through
    case Opcodes.DUP2_X1:
        if (oValue == null) {
            oValue = DUP.Kind.DUP2_X1;
        }
        // fall through
    case Opcodes.DUP2_X2:
        if (oValue == null) {
            oValue = DUP.Kind.DUP2_X2;
        }
        add(new DUP(this.ops.size(), opcode, this.line, (DUP.Kind) oValue));
        break;
    /***********
     * MONITOR *
     ***********/
    case Opcodes.MONITORENTER:
        oValue = MONITOR.Kind.ENTER;
        // fall through
    case Opcodes.MONITOREXIT:
        if (oValue == null) {
            oValue = MONITOR.Kind.EXIT;
        }
        add(new MONITOR(this.ops.size(), opcode, this.line, (MONITOR.Kind) oValue));
        break;
    /*******
     * MUL *
     *******/
    case Opcodes.DMUL:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FMUL:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IMUL:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LMUL:
        if (t == null) {
            t = T.LONG;
        }
        add(new MUL(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * NEG *
     *******/
    case Opcodes.DNEG:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FNEG:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.INEG:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LNEG:
        if (t == null) {
            t = T.LONG;
        }
        add(new NEG(this.ops.size(), opcode, this.line, t));
        break;
    /******
     * OR *
     ******/
    case Opcodes.IOR:
        t = T.AINT;
        // fall through
    case Opcodes.LOR:
        if (t == null) {
            t = T.LONG;
        }
        add(new OR(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * POP *
     *******/
    case Opcodes.POP:
        oValue = POP.Kind.POP;
        // fall through
    case Opcodes.POP2:
        if (oValue == null) {
            oValue = POP.Kind.POP2;
        }
        add(new POP(this.ops.size(), opcode, this.line, (POP.Kind) oValue));
        break;
    /********
     * PUSH *
     ********/
    case Opcodes.ACONST_NULL:
        t = T.REF;
        // fall through
    case Opcodes.DCONST_0:
        if (t == null) {
            oValue = 0D;
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FCONST_0:
        if (t == null) {
            oValue = 0F;
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ICONST_0:
        if (t == null) {
            oValue = 0;
            t = T.getJvmIntT(0);
        }
        // fall through
    case Opcodes.LCONST_0:
        if (t == null) {
            oValue = 0L;
            t = T.LONG;
        }
        // fall through
    case Opcodes.DCONST_1:
        if (t == null) {
            oValue = 1D;
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FCONST_1:
        if (t == null) {
            oValue = 1F;
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ICONST_1:
        if (t == null) {
            oValue = 1;
            t = T.getJvmIntT(1);
        }
        // fall through
    case Opcodes.LCONST_1:
        if (t == null) {
            oValue = 1L;
            t = T.LONG;
        }
        // fall through
    case Opcodes.FCONST_2:
        if (t == null) {
            oValue = 2F;
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ICONST_2:
        if (t == null) {
            oValue = 2;
            t = T.getJvmIntT(2);
        }
        // fall through
    case Opcodes.ICONST_3:
        if (t == null) {
            oValue = 3;
            t = T.getJvmIntT(3);
        }
        // fall through
    case Opcodes.ICONST_4:
        if (t == null) {
            oValue = 4;
            t = T.getJvmIntT(4);
        }
        // fall through
    case Opcodes.ICONST_5:
        if (t == null) {
            oValue = 5;
            t = T.getJvmIntT(5);
        }
        // fall through
    case Opcodes.ICONST_M1:
        if (t == null) {
            oValue = -1;
            t = T.getJvmIntT(-1);
        }
        add(new PUSH(this.ops.size(), opcode, this.line, t, oValue));
        break;
    /*******
     * REM *
     *******/
    case Opcodes.DREM:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FREM:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IREM:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LREM:
        if (t == null) {
            t = T.LONG;
        }
        add(new REM(this.ops.size(), opcode, this.line, t));
        break;
    /**********
     * RETURN *
     **********/
    case Opcodes.ARETURN:
        t = T.REF;
        // fall through
    case Opcodes.DRETURN:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FRETURN:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IRETURN:
        if (t == null) {
            t = T.AINT;
        }
        // fall through
    case Opcodes.LRETURN:
        if (t == null) {
            t = T.LONG;
        }
        // fall through
    case Opcodes.RETURN:
        if (t == null) {
            t = T.VOID;
        }
        add(new RETURN(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * SHL *
     *******/
    case Opcodes.ISHL:
        t = T.INT;
        // fall through
    case Opcodes.LSHL:
        if (t == null) {
            t = T.LONG;
        }
        add(new SHL(this.ops.size(), opcode, this.line, t, T.INT));
        break;
    /*******
     * SHR *
     *******/
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
        t = T.INT;
        // fall through
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        if (t == null) {
            t = T.LONG;
        }
        add(new SHR(this.ops.size(), opcode, this.line, t, T.INT,
                opcode == Opcodes.IUSHR || opcode == Opcodes.LUSHR));
        break;
    /*******
     * SUB *
     *******/
    case Opcodes.DSUB:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FSUB:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ISUB:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LSUB:
        if (t == null) {
            t = T.LONG;
        }
        add(new SUB(this.ops.size(), opcode, this.line, t));
        break;
    /********
     * SWAP *
     ********/
    case Opcodes.SWAP:
        add(new SWAP(this.ops.size(), opcode, this.line));
        break;
    /*********
     * THROW *
     *********/
    case Opcodes.ATHROW:
        add(new THROW(this.ops.size(), opcode, this.line));
        break;
    /*******
     * XOR *
     *******/
    case Opcodes.IXOR:
        t = T.AINT;
        // fall through
    case Opcodes.LXOR: {
        if (t == null) {
            t = T.LONG;
        }
        add(new XOR(this.ops.size(), opcode, this.line, t));
        break;
    }
    default:
        log.warn(getM() + ": Unknown insn opcode '" + opcode + "'!");
    }
}