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:net.sf.cglib.proxy.TestEnhancer.java

License:Apache License

public void testBridgeParameterCheckcast() throws Exception {

    // If the compiler used for Z omits the bridge method, and X is compiled with javac,
    // javac will generate an invokespecial bridge in X.

    // public interface I<A, B> {
    //   public A f(B b);
    // }//from   ww  w.  j  a va 2s  .  c o m
    // public abstract class Z<U extends Number> implements I<U, Long> {
    //   public U f(Long id) {
    //     return null;
    //   }
    // }
    // public class X extends Z<Integer> {}

    final Map<String, byte[]> classes = new HashMap<String, byte[]>();

    {
        ClassWriter cw = new ClassWriter(0);
        cw.visit(49, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, "I",
                "<A:Ljava/lang/Object;B:Ljava/lang/Object;>Ljava/lang/Object;", "java/lang/Object", null);
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "f",
                    "(Ljava/lang/Object;)Ljava/lang/Object;", "(TB;)TA;", null);
            mv.visitEnd();
        }
        cw.visitEnd();
        classes.put("I.class", cw.toByteArray());
    }
    {
        ClassWriter cw = new ClassWriter(0);
        cw.visit(49, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_ABSTRACT, "Z",
                "<U:Ljava/lang/Number;>Ljava/lang/Object;LI<TU;Ljava/lang/String;>;", "java/lang/Object",
                new String[] { "I" });
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();
        }
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "f", "(Ljava/lang/String;)Ljava/lang/Number;",
                    "(Ljava/lang/String;)TU;", null);
            mv.visitCode();
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(1, 2);
            mv.visitEnd();
        }
        cw.visitEnd();
        classes.put("Z.class", cw.toByteArray());
    }
    {
        ClassWriter cw = new ClassWriter(0);
        cw.visit(49, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "X", "LZ<Ljava/lang/Integer;>;", "Z", null);
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "Z", "<init>", "()V", false);
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();
        }
        {
            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_BRIDGE | Opcodes.ACC_SYNTHETIC,
                    "f", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
            mv.visitCode();
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "Z", "f", "(Ljava/lang/String;)Ljava/lang/Number;",
                    false);
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }

        cw.visitEnd();

        classes.put("X.class", cw.toByteArray());
    }

    ClassLoader classLoader = new ClassLoader(getClass().getClassLoader()) {
        @Override
        public InputStream getResourceAsStream(String name) {
            InputStream is = super.getResourceAsStream(name);
            if (is != null) {
                return is;
            }
            if (classes.containsKey(name)) {
                return new ByteArrayInputStream(classes.get(name));
            }
            return null;
        }

        public Class findClass(String name) throws ClassNotFoundException {
            byte[] ba = classes.get(name.replace('.', '/') + ".class");
            if (ba != null) {
                return defineClass(name, ba, 0, ba.length);
            }
            throw new ClassNotFoundException(name);
        }
    };

    List<Class> retTypes = new ArrayList<Class>();
    List<Class> paramTypes = new ArrayList<Class>();
    Interceptor interceptor = new Interceptor(retTypes, paramTypes);

    Enhancer e = new Enhancer();
    e.setClassLoader(classLoader);
    e.setSuperclass(classLoader.loadClass("X"));
    e.setCallback(interceptor);

    Object c = e.create();

    for (Method m : c.getClass().getDeclaredMethods()) {
        if (m.getName().equals("f") && m.getReturnType().equals(Object.class)) {
            m.invoke(c, new Object[] { null });
        }
    }

    // f(Object)Object should bridge to f(Number)String
    assertEquals(Arrays.asList(Object.class, Number.class), retTypes);
    assertEquals(Arrays.asList(Object.class, String.class), paramTypes);
}

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

License:Apache License

/**
 * Writes an instructions to the given MethodVisitor which will load the default
 * value for the given class. For primitives, it will load 0 or false in the
 * correct type onto the stack. For references, the generated code will put a null.
 * @param mv the MethodVisitor to write to
 * @param clazz the class to write the default for.
 *///from w  ww  .  j  a v  a  2  s  .  c  o m
public static void generateLoadDefault(MethodVisitor mv, Class<?> clazz) {
    if (!clazz.isPrimitive())
        mv.visitInsn(Opcodes.ACONST_NULL);
    else if (clazz.equals(Integer.TYPE) || clazz.equals(Character.TYPE) || clazz.equals(Short.TYPE)
            || clazz.equals(Byte.TYPE) || clazz.equals(Boolean.TYPE))
        mv.visitInsn(Opcodes.ICONST_0);
    else if (clazz.equals(Long.TYPE))
        mv.visitInsn(Opcodes.LCONST_0);
    else if (clazz.equals(Double.TYPE))
        mv.visitInsn(Opcodes.DCONST_0);
    else if (clazz.equals(Float.TYPE))
        mv.visitInsn(Opcodes.FCONST_0);
    else
        throw new RuntimeException("Oops, forgot the primitive " + clazz + "?");
}

From source file:org.adjective.stout.operation.NullExpression.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    addInstruction(collector, new GenericInstruction(Opcodes.ACONST_NULL));
}

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

License:Apache License

public void visitInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;//w  w  w.  jav  a2 s.c o m
    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.apache.aries.proxy.impl.gen.ProxySubclassAdapter.java

License:Apache License

public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/* ww  w  . java 2  s  .  c  o m*/
    LOGGER.debug(Constants.LOG_ENTRY, "visit",
            new Object[] { version, access, name, signature, superName, interfaces });

    // store the superclass binary name
    this.superclassBinaryName = name.replaceAll("/", "\\.");

    try {
        this.superclassClass = Class.forName(superclassBinaryName, false, loader);
    } catch (ClassNotFoundException cnfe) {
        throw new TypeNotPresentException(superclassBinaryName, cnfe);
    }

    // keep the same access and signature as the superclass (unless it's abstract)
    // remove all the superclass interfaces because they will be inherited
    // from the superclass anyway
    if ((access & ACC_ABSTRACT) != 0) {
        //If the super was abstract the subclass should not be!
        access &= ~ACC_ABSTRACT;
    }
    cv.visit(ProxyUtils.getWeavingJavaVersion(), access, newClassName, signature, name, null);

    // add a private field for the invocation handler
    // this isn't static in case we have multiple instances of the same
    // proxy
    cv.visitField(ACC_PRIVATE, IH_FIELD, Type.getDescriptor(InvocationHandler.class), null, null);

    // create a static adapter for generating a static initialiser method in
    // the generated subclass
    staticAdapter = new GeneratorAdapter(ACC_STATIC, new Method("<clinit>", Type.VOID_TYPE, NO_ARGS), null,
            null, cv);

    // add a zero args constructor method
    Method m = new Method("<init>", Type.VOID_TYPE, NO_ARGS);
    GeneratorAdapter methodAdapter = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cv);
    // loadthis
    methodAdapter.loadThis();
    // List the constructors in the superclass.
    Constructor<?>[] constructors = superclassClass.getDeclaredConstructors();
    // Check that we've got at least one constructor, and get the 1st one in the list.
    if (constructors.length > 0) {
        // We now need to construct the proxy class as though it is going to invoke the superclasses constructor.
        // We do this because we can no longer call the java.lang.Object() zero arg constructor as the JVM now throws a VerifyError.
        // So what we do is build up the calling of the superclasses constructor using nulls and default values. This means that the 
        // class bytes can be verified by the JVM, and then in the ProxySubclassGenerator, we load the class without invoking the 
        // constructor. 
        Method constructor = Method.getMethod(constructors[0]);
        Type[] argTypes = constructor.getArgumentTypes();
        if (argTypes.length == 0) {
            methodAdapter.invokeConstructor(Type.getType(superclassClass),
                    new Method("<init>", Type.VOID_TYPE, NO_ARGS));
        } else {
            for (Type type : argTypes) {
                switch (type.getSort()) {
                case Type.ARRAY:
                    // We need to process any array or multidimentional arrays.
                    String elementDesc = type.getElementType().getDescriptor();
                    String typeDesc = type.getDescriptor();

                    // Iterate over the number of arrays and load 0 for each one. Keep a count of the number of 
                    // arrays as we will need to run different code fo multi dimentional arrays.
                    int index = 0;
                    while (!elementDesc.equals(typeDesc)) {
                        typeDesc = typeDesc.substring(1);
                        methodAdapter.visitInsn(Opcodes.ICONST_0);
                        index++;
                    }
                    // If we're just a single array, then call the newArray method, otherwise use the MultiANewArray instruction.
                    if (index == 1) {
                        methodAdapter.newArray(type.getElementType());
                    } else {
                        methodAdapter.visitMultiANewArrayInsn(type.getDescriptor(), index);
                    }
                    break;
                case Type.BOOLEAN:
                    methodAdapter.push(true);
                    break;
                case Type.BYTE:
                    methodAdapter.push(Type.VOID_TYPE);
                    break;
                case Type.CHAR:
                    methodAdapter.push(Type.VOID_TYPE);
                    break;
                case Type.DOUBLE:
                    methodAdapter.push(0.0);
                    break;
                case Type.FLOAT:
                    methodAdapter.push(0.0f);
                    break;
                case Type.INT:
                    methodAdapter.push(0);
                    break;
                case Type.LONG:
                    methodAdapter.push(0l);
                    break;
                case Type.SHORT:
                    methodAdapter.push(0);
                    break;
                default:
                case Type.OBJECT:
                    methodAdapter.visitInsn(Opcodes.ACONST_NULL);
                    break;
                }
            }

            methodAdapter.invokeConstructor(Type.getType(superclassClass),
                    new Method("<init>", Type.VOID_TYPE, argTypes));
        }
    }
    methodAdapter.returnValue();
    methodAdapter.endMethod();

    // add a method for getting the invocation handler
    Method setter = new Method("setInvocationHandler", Type.VOID_TYPE, new Type[] { IH_TYPE });
    m = new Method("getInvocationHandler", IH_TYPE, NO_ARGS);
    methodAdapter = new GeneratorAdapter(ACC_PUBLIC | ACC_FINAL, m, null, null, cv);
    // load this to get the field
    methodAdapter.loadThis();
    // get the ih field and return
    methodAdapter.getField(newClassType, IH_FIELD, IH_TYPE);
    methodAdapter.returnValue();
    methodAdapter.endMethod();

    // add a method for setting the invocation handler
    methodAdapter = new GeneratorAdapter(ACC_PUBLIC | ACC_FINAL, setter, null, null, cv);
    // load this to put the field
    methodAdapter.loadThis();
    // load the method arguments (i.e. the invocation handler) to the stack
    methodAdapter.loadArgs();
    // set the ih field using the method argument
    methodAdapter.putField(newClassType, IH_FIELD, IH_TYPE);
    methodAdapter.returnValue();
    methodAdapter.endMethod();

    // loop through the class hierarchy to get any needed methods off the
    // supertypes
    // start by finding the methods declared on the class of interest (the
    // superclass of our dynamic subclass)
    java.lang.reflect.Method[] observedMethods = superclassClass.getDeclaredMethods();
    // add the methods to a set of observedMethods
    ProxySubclassMethodHashSet<String> setOfObservedMethods = new ProxySubclassMethodHashSet<String>(
            observedMethods.length);
    setOfObservedMethods.addMethodArray(observedMethods);
    // get the next superclass in the hierarchy
    Class<?> nextSuperClass = superclassClass.getSuperclass();
    while (nextSuperClass != null) {
        // set the fields for the current class
        setCurrentAnalysisClassFields(nextSuperClass);

        // add a static field and static initializer code to the generated
        // subclass
        // for each of the superclasses in the hierarchy
        addClassStaticField(currentlyAnalysedClassName);

        LOGGER.debug("Class currently being analysed: {} {}", currentlyAnalysedClassName,
                currentlyAnalysedClass);

        // now find the methods declared on the current class and add them
        // to a set of foundMethods
        java.lang.reflect.Method[] foundMethods = currentlyAnalysedClass.getDeclaredMethods();
        ProxySubclassMethodHashSet<String> setOfFoundMethods = new ProxySubclassMethodHashSet<String>(
                foundMethods.length);
        setOfFoundMethods.addMethodArray(foundMethods);
        // remove from the set of foundMethods any methods we saw on a
        // subclass
        // because we want to use the lowest level declaration of a method
        setOfFoundMethods.removeAll(setOfObservedMethods);
        try {
            // read the current class and use a
            // ProxySubclassHierarchyAdapter
            // to process only methods on that class that are in the list
            ClassLoader loader = currentlyAnalysedClass.getClassLoader();
            if (loader == null) {
                loader = this.loader;
            }
            ClassReader cr = new ClassReader(loader
                    .getResourceAsStream(currentlyAnalysedClass.getName().replaceAll("\\.", "/") + ".class"));
            ClassVisitor hierarchyAdapter = new ProxySubclassHierarchyAdapter(this, setOfFoundMethods);
            cr.accept(hierarchyAdapter, ClassReader.SKIP_DEBUG);
        } catch (IOException e) {
            throw new TypeNotPresentException(currentlyAnalysedClassName, e);
        }
        // now add the foundMethods to the overall list of observed methods
        setOfObservedMethods.addAll(setOfFoundMethods);
        // get the next class up in the hierarchy and go again
        nextSuperClass = currentlyAnalysedClass.getSuperclass();
    }

    // we've finished looking at the superclass hierarchy
    // set the fields for the immediate superclass of our dynamic subclass
    setCurrentAnalysisClassFields(superclassClass);

    // add the class static field
    addClassStaticField(currentlyAnalysedClassName);
    // we do the lowest class last because we are already visiting the class
    // when in this adapter code
    // now we are ready to visit all the methods on the lowest class
    // which will happen by the ASM ClassVisitor implemented in this adapter

    LOGGER.debug(Constants.LOG_EXIT, "visit");
}

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private static boolean addGetWrapperParts(String newClassName, Class<?> wrapperClass, Method getMethods[],
        Field fields[], ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getWrapperParts",
            "(Ljava/lang/Object;)Ljava/util/List;", "(Ljava/lang/Object;)Ljava/util/List<Ljava/lang/Object;>;",
            new String[] { "org/apache/cxf/interceptor/Fault" });
    mv.visitCode();/* w w w  . j  a  v a2s .  c om*/
    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(108, lBegin);

    // the ret List
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ASTORE, 2);

    // cast the Object to the wrapperType type
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, periodToSlashes(wrapperClass.getName()));
    mv.visitVarInsn(Opcodes.ASTORE, 3);

    for (int x = 0; x < getMethods.length; x++) {
        Method method = getMethods[x];
        if (method == null && fields[x] != null) {
            // fallback to reflection mode
            return false;
        }

        if (method == null) {
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(200 + x, l3);

            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        } else {
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLineNumber(250 + x, l3);

            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitVarInsn(Opcodes.ALOAD, 3);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, periodToSlashes(wrapperClass.getName()), method.getName(),
                    getMethodSignature(method));
            if (method.getReturnType().isPrimitive()) {
                // wrap into Object type
                createObjectWrapper(mv, method.getReturnType());
            }
            if (JAXBElement.class.isAssignableFrom(method.getReturnType())) {
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "javax/xml/bind/JAXBElement", "getValue",
                        "()Ljava/lang/Object;");
            }

            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        }
    }

    // return the list
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(108, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);

    Label lEnd = new Label();
    mv.visitLabel(lEnd);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, lBegin, lEnd, 0);
    mv.visitLocalVariable("o", "Ljava/lang/Object;", null, lBegin, lEnd, 1);
    mv.visitLocalVariable("ret", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/Object;>;", lBegin, lEnd, 2);
    mv.visitLocalVariable("ok", "L" + periodToSlashes(wrapperClass.getName()) + ";", null, lBegin, lEnd, 3);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return true;
}

From source file:org.apache.drill.exec.compile.bytecode.ValueHolderIden.java

License:Apache License

private static void initType(int index, Type t, DirectSorter v) {
    switch (t.getSort()) {
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.CHAR:
    case Type.SHORT:
    case Type.INT:
        v.visitInsn(Opcodes.ICONST_0);//ww  w.j a v  a  2s.  co  m
        v.directVarInsn(Opcodes.ISTORE, index);
        break;
    case Type.LONG:
        v.visitInsn(Opcodes.LCONST_0);
        v.directVarInsn(Opcodes.LSTORE, index);
        break;
    case Type.FLOAT:
        v.visitInsn(Opcodes.FCONST_0);
        v.directVarInsn(Opcodes.FSTORE, index);
        break;
    case Type.DOUBLE:
        v.visitInsn(Opcodes.DCONST_0);
        v.directVarInsn(Opcodes.DSTORE, index);
        break;
    case Type.OBJECT:
        v.visitInsn(Opcodes.ACONST_NULL);
        v.directVarInsn(Opcodes.ASTORE, index);
        break;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:org.apache.felix.scrplugin.helper.ClassModifier.java

License:Apache License

private static void createMethod(final ClassWriter cw, final String className, final String referenceName,
        final String fieldName, final String typeName, final boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase()
            + referenceName.substring(1);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V",
            null, null);/*  w  w w.  j  a  va  2  s. co m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
}

From source file:org.apache.felix.scrplugin.tags.qdox.QDoxJavaClassDescription.java

License:Apache License

protected void createMethod(ClassWriter cw, String propertyName, String typeName, boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + propertyName.substring(0, 1).toUpperCase()
            + propertyName.substring(1);
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V", null,
            null);// ww  w .  j  a va 2s .c om
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, this.getName().replace('.', '/'), propertyName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    // add to qdox
    final JavaParameter param = new JavaParameter(new Type(typeName), "param");
    final JavaParameter[] params = new JavaParameter[] { param };
    final com.thoughtworks.qdox.model.JavaMethod meth = new com.thoughtworks.qdox.model.JavaMethod();
    meth.setName(methodName);
    for (int i = 0; i < params.length; i++) {
        meth.addParameter(params[i]);
    }
    meth.setModifiers(new String[] { "protected" });
    this.javaClass.addMethod(meth);
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createNoArgConstructor(final String enhancingClassName, final String enhancedClassName,
        final String descEnhancer, final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, Enhancer.CONSTRUCTOR_INIT,
            Enhancer.makeDescription(Void.TYPE), null, null);
    mv.visitCode();// ww  w  . j a v  a2s .  c o m

    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, enhancingClassName, Enhancer.CONSTRUCTOR_INIT,
            Enhancer.makeDescription(Void.TYPE));

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);

    mv.visitInsn(Opcodes.RETURN);

    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}