Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

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);
    // }//w w  w.j  a  va2s  .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:net.sf.clirr.core.internal.asm.AbstractAsmScoped.java

License:Open Source License

public Scope getDeclaredScope() {
    if (checkFlag(Opcodes.ACC_PRIVATE)) {
        return Scope.PRIVATE;
    } else if (checkFlag(Opcodes.ACC_PROTECTED)) {
        return Scope.PROTECTED;
    } else if (checkFlag(Opcodes.ACC_PUBLIC)) {
        return Scope.PUBLIC;
    }//  w w w . j  av a2s  .  c om
    return Scope.PACKAGE;
}

From source file:net.sf.profiler4j.agent.BytecodeTransformer.java

License:Apache License

private static boolean isGetterSetter(int flag, String name, String methodDescriptor) {
    if ((Opcodes.ACC_PUBLIC | flag) == 0 || ((Opcodes.ACC_STATIC | flag) | (Opcodes.ACC_NATIVE | flag)
            | (Opcodes.ACC_ABSTRACT | flag) | (Opcodes.ACC_SYNCHRONIZED | flag)) != 0) {
        return false;
    }//from   w  ww .ja v  a  2  s . c om
    Type[] pTypes = Type.getArgumentTypes(methodDescriptor);
    Type rType = Type.getReturnType(methodDescriptor);
    if (getterRegex.matcher(name).matches() || getterBoolRegex.matcher(name).matches()) {
        return pTypes.length == 0 && !rType.equals(Type.VOID_TYPE);
    }
    if (setterRegex.matcher(name).matches()) {
        return pTypes.length == 1 && !rType.equals(Type.VOID_TYPE);
    }
    return false;
}

From source file:net.sourceforge.cobertura.instrument.pass3.AbstractCodeProvider.java

License:GNU General Public License

/**
 * {@inheritDoc}<br/><br/>/*from  w  ww .  j av  a2  s  .c  o  m*/
 * <p/>
 * Generates method (named  {@link #COBERTURA_CLASSMAP_METHOD_NAME}) with such a signature:
 * __cobertura_classmap( {@link LightClassmapListener} listener).</br>
 * <p/>
 * The method informs the listener about all lines, jumps and switches found, and about all counters tracking
 * the constructions.
 */
public void generateCoberturaClassMapMethod(ClassVisitor cv, ClassMap classMap) {

    LinkedList<TouchPointDescriptor> touchPointDescriptors = new LinkedList<TouchPointDescriptor>(
            classMap.getTouchPointsInLineOrder());
    int parts = 0;
    for (int j = 0; touchPointDescriptors.size() > 0; j++) {
        List<TouchPointDescriptor> bufor = new LinkedList<TouchPointDescriptor>();
        for (int i = 0; i < 1000 && touchPointDescriptors.size() > 0; i++) {
            bufor.add(touchPointDescriptors.removeFirst());
        }
        classMapContent(cv, j, bufor);
        parts++;
    }

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, COBERTURA_CLASSMAP_METHOD_NAME,
            "(" + Type.getType(LightClassmapListener.class).toString() + ")V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);

    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn(classMap.getClassName());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "setClazz",
            "(Ljava/lang/String;)V");

    if (classMap.getSource() != null) {
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(classMap.getSource());
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "setSource",
                "(Ljava/lang/String;)V");
    }

    for (int i = 0; i < parts; i++) {
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, classMap.getClassName(),
                COBERTURA_CLASSMAP_METHOD_NAME + "_" + i,
                "(" + Type.getType(LightClassmapListener.class).toString() + ")V");
    }

    mv.visitInsn(Opcodes.POP);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.AbstractCodeProvider.java

License:GNU General Public License

private void classMapContent(ClassVisitor cv, int nr, List<TouchPointDescriptor> touchPointDescriptors) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_CLASSMAP_METHOD_NAME + "_" + nr,
            "(" + Type.getType(LightClassmapListener.class).toString() + ")V", null, null);
    mv.visitCode();//from  w  ww  .  j ava 2 s .  c  om
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    for (TouchPointDescriptor tpd : touchPointDescriptors) {
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(tpd.getLineNumber());
        if (tpd instanceof LineTouchPointDescriptor) {
            mv.visitLdcInsn(((LineTouchPointDescriptor) tpd).getCounterId());
            mv.visitLdcInsn(((LineTouchPointDescriptor) tpd).getMethodName());
            mv.visitLdcInsn(((LineTouchPointDescriptor) tpd).getMethodSignature());
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "putLineTouchPoint",
                    "(IILjava/lang/String;Ljava/lang/String;)V");
        } else if (tpd instanceof JumpTouchPointDescriptor) {
            mv.visitLdcInsn(((JumpTouchPointDescriptor) tpd).getCounterIdForTrue());
            mv.visitLdcInsn(((JumpTouchPointDescriptor) tpd).getCounterIdForFalse());
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "putJumpTouchPoint",
                    "(III)V");
        } else if (tpd instanceof SwitchTouchPointDescriptor) {
            SwitchTouchPointDescriptor stpd = (SwitchTouchPointDescriptor) tpd;
            final String enum_sign = ((SwitchTouchPointDescriptor) tpd).getEnumType();
            if (enum_sign == null) {
                mv.visitLdcInsn(Integer.MAX_VALUE);
            } else {
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, enum_sign, "values", "()[L" + enum_sign + ";");
                mv.visitInsn(Opcodes.ARRAYLENGTH);
            }
            Collection<Integer> ci = stpd.getCountersForLabels();
            mv.visitLdcInsn(ci.size());//Size of a new table
            mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
            int i = 0;
            for (Integer counterId : ci) {
                mv.visitInsn(Opcodes.DUP); //First for addition of items, second ad putSwitchTouchPoint parameter (or next loop iteration)
                mv.visitLdcInsn(i);
                mv.visitLdcInsn(counterId);
                mv.visitInsn(Opcodes.IASTORE);
                i++;
            }
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "putSwitchTouchPoint",
                    "(II[I)V");
        }
    }
    mv.visitInsn(Opcodes.POP);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.AbstractCodeProvider.java

License:GNU General Public License

public void generateCoberturaInitMethod(ClassVisitor cv, String className, int countersCnt) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, COBERTURA_INIT_METHOD_NAME,
            "()V", null, null);
    mv.visitCode();/*from  ww  w  .jav  a2  s  .  c om*/
    generateCINITmethod(mv, className, countersCnt);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0); //will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.AtomicArrayCodeProvider.java

License:GNU General Public License

public void generateCountersField(ClassVisitor cv) {
    FieldVisitor fv = cv.visitField(//from  ww  w.j  av a2s  .c o  m
            Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT,
            COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE, null, null);
    fv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.AtomicArrayCodeProvider.java

License:GNU General Public License

/**
 * <pre>/*from  w w  w  .j  a  va2 s  .co  m*/
 * int[] __cobertura_get_and_reset_counters() {
 * int[] res = new int[counters.length()];
 * for(int i=0; i<counters.length(); i++){
 * res[i]=counters.getAndSet(i, 0);
 * }
 * return res;
 * }
 * </pre>
 */
public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()[I", null, null);

    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length",
            "()I");
    mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, 1);
    Label l3 = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, l3);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "getAndSet",
            "(II)I");
    mv.visitInsn(Opcodes.IASTORE);
    mv.visitIincInsn(1, 1);
    mv.visitLabel(l3);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length",
            "()I");
    mv.visitJumpInsn(Opcodes.IF_ICMPLT, l4);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.FastArrayCodeProvider.java

License:GNU General Public License

public void generateCountersField(ClassVisitor cv) {
    /*final tooks 270ms, no-modifier 310ms, volatile 500ms*/
    FieldVisitor fv = cv.visitField(/* w w w .  ja va  2 s  . com*/
            Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT,
            COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE, null, null);
    fv.visitEnd();
}

From source file:net.sourceforge.cobertura.instrument.pass3.FastArrayCodeProvider.java

License:GNU General Public License

public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()[I", null, null);
    mv.visitCode();/*  ww  w.  j ava 2 s  .c  o m*/
    /*cobertura_counters.*/
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    /*cobertura_counters.*/
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitInsn(Opcodes.ARRAYLENGTH);

    mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME,
            COBERTURA_COUNTERS_FIELD_TYPE);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);//will be recalculated by writer
    mv.visitEnd();
}