Example usage for org.objectweb.asm Opcodes ACONST_NULL

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

Introduction

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

Prototype

int ACONST_NULL

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

Click Source Link

Usage

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

License:Apache License

@Deprecated
public static void genInvokeXxxWithArray(ClassWriter cw, Method method) {
    MethodVisitor mv;//from   ww  w  . j  a  va  2s.  c  o  m
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS);
    mv.visitCode();

    Class callClass = method.getDeclaringClass();
    boolean useInterface = callClass.isInterface();

    String type = BytecodeHelper.getClassInternalName(callClass.getName());
    String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes());

    // make call
    if (Modifier.isStatic(method.getModifiers())) {
        genLoadParameters(2, mv, method);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        BytecodeHelper.doCast(mv, callClass);
        genLoadParameters(2, mv, method);
        mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                method.getName(), descriptor);
    }

    BytecodeHelper.box(mv, method.getReturnType());
    if (method.getReturnType() == void.class) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

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

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

License:Apache License

private static void genInvokeWithFixedParams(ClassWriter cw, Method method) {
    MethodVisitor mv;//  www . j  a  v a2  s . co  m
    final int pc = method.getParameterTypes().length;
    if (pc <= 4) {
        StringBuilder pdescb = new StringBuilder();
        for (int i = 0; i != pc; ++i)
            pdescb.append("Ljava/lang/Object;");

        String pdesc = pdescb.toString();

        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(Ljava/lang/Object;" + pdesc + ")Ljava/lang/Object;",
                null, EXCEPTIONS);
        mv.visitCode();

        Class callClass = method.getDeclaringClass();
        boolean useInterface = callClass.isInterface();

        String type = BytecodeHelper.getClassInternalName(callClass.getName());
        String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(),
                method.getParameterTypes());

        // make call
        if (Modifier.isStatic(method.getModifiers())) {
            MethodHandleFactory.genLoadParametersDirect(2, mv, method);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            BytecodeHelper.doCast(mv, callClass);
            MethodHandleFactory.genLoadParametersDirect(2, mv, method);
            mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                    method.getName(), descriptor);
        }

        BytecodeHelper.box(mv, method.getReturnType());
        if (method.getReturnType() == void.class) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

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

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

License:Apache License

private static void genInvokeWithFixedPrimitiveParams(ClassWriter cw, Method method) {
    MethodVisitor mv;/*from w w  w . ja va  2s  .c  om*/
    final Class<?>[] pt = method.getParameterTypes();
    final int pc = pt.length;
    if (pc > 0 && pc <= 3) {
        StringBuilder pdescb = new StringBuilder();
        boolean hasPrimitive = false;
        for (int i = 0; i != pc; ++i)
            if (pt[i].isPrimitive()) {
                hasPrimitive = true;
                pdescb.append(BytecodeHelper.getTypeDescription(pt[i]));
            } else
                pdescb.append("Ljava/lang/Object;");

        if (!hasPrimitive)
            return;

        String pdesc = pdescb.toString();

        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(Ljava/lang/Object;" + pdesc + ")Ljava/lang/Object;",
                null, EXCEPTIONS);
        mv.visitCode();

        Class callClass = method.getDeclaringClass();
        boolean useInterface = callClass.isInterface();

        String type = BytecodeHelper.getClassInternalName(callClass.getName());
        String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(),
                method.getParameterTypes());

        // make call
        if (Modifier.isStatic(method.getModifiers())) {
            MethodHandleFactory.genLoadParametersPrimitiveDirect(2, mv, method);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            BytecodeHelper.doCast(mv, callClass);
            MethodHandleFactory.genLoadParametersPrimitiveDirect(2, mv, method);
            mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                    method.getName(), descriptor);
        }

        BytecodeHelper.box(mv, method.getReturnType());
        if (method.getReturnType() == void.class) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

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

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  w w w  .j av  a2 s. co  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.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createObjectHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getObjectMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "([Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);/*from   ww  w. j a  va2 s  .c om*/
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("args", "[Ljava/lang/Object;", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;

}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createIntHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getIntMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(I)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);/*  ww w  .j av  a 2s .  c o m*/
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "I", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;
}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createLongHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getLongMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(J)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);/*  w  w w. j av a  2  s  .co  m*/
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "J", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 3;
    return mn;
}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createFloatHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getFloatMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(F)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);//from   w  w w . ja  v  a2s  . c o m
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "F", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;
}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createStringHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getStringMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(Ljava/lang/String;)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);//from   ww  w  . j  ava 2 s .  co m
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "Ljava/lang/String;", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;
}

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

License:Apache License

/**
 * Box the result if needed./*  w  ww  .j av  a  2s . c o  m*/
 */
private static void prepareResult(MethodVisitor mv, Method refMethod) {

    Type type = Type.getReturnType(refMethod);
    switch (type.getSort()) {
    case Type.VOID:
        mv.visitInsn(Opcodes.ACONST_NULL); // nothing to return the original method returns void
        break;
    case Type.BOOLEAN:
        callBoxer(mv, "(Z)Ljava/lang/Object;");
        break;
    case Type.BYTE:
        callBoxer(mv, "(B)Ljava/lang/Object;");
        break;
    case Type.CHAR:
        callBoxer(mv, "(C)Ljava/lang/Object;");
        break;
    case Type.SHORT:
        callBoxer(mv, "(S)Ljava/lang/Object;");
        break;
    case Type.INT:
        callBoxer(mv, "(I)Ljava/lang/Object;");
        break;
    case Type.LONG:
        callBoxer(mv, "(J)Ljava/lang/Object;");
        break;
    case Type.FLOAT:
        callBoxer(mv, "(F)Ljava/lang/Object;");
        break;
    case Type.DOUBLE:
        callBoxer(mv, "(D)Ljava/lang/Object;");
        break;
    }
}