Example usage for org.objectweb.asm Opcodes ASM4

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

Introduction

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

Prototype

int ASM4

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

Click Source Link

Usage

From source file:org.greencheek.gc.memusage.agent.MonitoringAspectGenerator.java

License:Apache License

/**
 * Visits a method of the class. This method <i>must</i> return a new
 * {@link MethodVisitor} instance (or <tt>null</tt>) each time it is
 * called, i.e., it should not return a previously returned visitor.
 * //w w  w  .jav  a  2s . com
 * This is overriden to:
 * <ol>
 * <li>statically initialising the AtomicLong if a static initialiser method is present in the class</li>
 * <li>Add to the method on with the @RecordGCMemUsage was present, a piece of code that updates the atomic long</li>
 * </ol>
 *
 * @param access the method's access flags (see {@link Opcodes}). This
 *        parameter also indicates if the method is synthetic and/or
 *        deprecated.
 * @param name the method's name.
 * @param desc the method's descriptor (see {@link Type Type}).
 * @param signature the method's signature. May be <tt>null</tt> if the
 *        method parameters, return type and exceptions do not use generic
 *        types.
 * @param exceptions the internal names of the method's exception classes
 *        (see {@link Type#getInternalName() getInternalName}). May be
 *        <tt>null</tt>.
 * @return an object to visit the byte code of the method, or <tt>null</tt>
 *         if this class visitor is not interested in visiting the code of
 *         this method.
 */
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
        final String[] exceptions) {

    MethodVisitor r = super.visitMethod(access, name, desc, signature, exceptions);

    if ("<clinit>".equals(name)) {
        r = new AddStaticAtomicLongInitializer(Opcodes.ASM4, r, overrideMethodNames.values());
    }

    MethodInfo currentMethod = new MethodInfo(access, name, desc, signature, exceptions, null, null);
    if (overrideMethodNames.containsKey(currentMethod.toString())) {
        final MethodInfo info = overrideMethodNames.get(currentMethod.toString());

        r = new AdviceAdapter(Opcodes.ASM4, r, access, name, desc) {
            protected void onMethodEnter() {
                System.out.println("Aspecting: " + info.getAnnotatedClassName() + "." + info.toString());
                System.out.println("Increments: " + info.getFieldName());
                super.visitFieldInsn(GETSTATIC, info.getAnnotatedClassName(), info.getFieldName(),
                        "Ljava/util/concurrent/atomic/AtomicLong;");
                super.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicLong",
                        "incrementAndGet", "()J");
                super.visitInsn(POP2);
            }
        };
    }

    return r;

}

From source file:org.greencheek.gc.memusage.agent.MonitoringAspectGenerator.java

License:Apache License

/**
 * Visits a field of the class.// ww  w.j  av a 2s. co m
 * The method is overriden to perform the functions of:
 * <ol>
 * <li>Adding a static AtomicLong to the class</li>
 * <li>statically initialising the AtomicLong if a static initialiser isn't already present</li>
 * </ol>
 *
 * @param access the field's access flags (see {@link Opcodes}). This
 *        parameter also indicates if the field is synthetic and/or
 *        deprecated.
 * @param name the field's name.
 * @param desc the field's descriptor (see {@link Type Type}).
 * @param signature the field's signature. May be <tt>null</tt> if the
 *        field's type does not use generic types.
 * @param value the field's initial value. This parameter, which may be
 *        <tt>null</tt> if the field does not have an initial value, must
 *        be an {@link Integer}, a {@link Float}, a {@link Long}, a
 *        {@link Double} or a {@link String} (for <tt>int</tt>,
 *        <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
 *        respectively). <i>This parameter is only used for static fields</i>.
 *        Its value is ignored for non static fields, which must be
 *        initialized through bytecode instructions in constructors or
 *        methods.
 * @return a visitor to visit field annotations and attributes, or
 *         <tt>null</tt> if this class visitor is not interested in
 *         visiting these annotations and attributes.
 */
public void visitEnd() {
    for (MethodInfo method : overrideMethodNames.values()) {
        System.out.println("Adding: public final static AtomicLong " + method.getFieldName());
        FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
                method.getFieldName(), "Ljava/util/concurrent/atomic/AtomicLong;", null, null);
        fv.visitEnd();
    }

    if (!hasCinit) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        AddStaticAtomicLongInitializer initialiser = new AddStaticAtomicLongInitializer(Opcodes.ASM4, mv,
                overrideMethodNames.values());
        initialiser.visitCode();
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    super.visitEnd();

}

From source file:org.greencheek.gc.memusage.agent.RecordGCMemUsageAnnotationCollector.java

License:Apache License

public RecordGCMemUsageAnnotationCollector(String className) {
    super(Opcodes.ASM4);
    this.className = className;
}

From source file:org.greencheek.gc.memusage.agent.RecordGCMemUsageAnnotationCollector.java

License:Apache License

@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
        final String[] exceptions) {

    if ("<clinit>".equals(name)) {
        cinitAvailable = true;/*from  www.j a va  2  s. c o  m*/
    }

    return new MethodVisitor(Opcodes.ASM4) {

        @Override
        public AnnotationVisitor visitAnnotation(final String sig, boolean visible) {
            if (sig.equals("Lorg/greencheek/gc/memusage/agent/RecordGCMemUsage;")) {

                return new AnnotationVisitor(Opcodes.ASM4) {
                    private String fieldName = name;

                    public void visit(String name, Object value) {
                        if (name.equals("fieldName"))
                            fieldName = value.toString();
                    }

                    @Override
                    public void visitEnd() {
                        if (sig.equals("Lorg/greencheek/gc/memusage/agent/RecordGCMemUsage;")) {
                            createMatchingMethod(access, name, desc, signature, exceptions, fieldName,
                                    className);
                        }
                        super.visitEnd();
                    }

                };
            } else {
                return null;
            }
        }

    };
}

From source file:org.gridgain.grid.kernal.processors.hadoop.GridHadoopClassLoader.java

License:Open Source License

/**
 * @param clsName Class name./* w w w.j  a  v  a2s.co m*/
 * @return {@code true} If the class has external dependencies.
 */
boolean hasExternalDependencies(final String clsName, final Set<String> visited) {
    if (isHadoop(clsName)) // Hadoop must not be in classpath but Idea sucks, so filtering explicitly as external.
        return true;

    // Try to get from parent to check if the type accessible.
    InputStream in = loadClassBytes(getParent(), clsName);

    if (in == null) // The class is external itself, it must be loaded from this class loader.
        return true;

    if (!isGgfsOrGgHadoop(clsName)) // Other classes should not have external dependencies.
        return false;

    final ClassReader rdr;

    try {
        rdr = new ClassReader(in);
    } catch (IOException e) {
        throw new RuntimeException("Failed to read class: " + clsName, e);
    }

    visited.add(clsName);

    final AtomicBoolean hasDeps = new AtomicBoolean();

    rdr.accept(new ClassVisitor(Opcodes.ASM4) {
        AnnotationVisitor av = new AnnotationVisitor(Opcodes.ASM4) {
            // TODO
        };

        FieldVisitor fv = new FieldVisitor(Opcodes.ASM4) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean b) {
                onType(desc);

                return av;
            }
        };

        MethodVisitor mv = new MethodVisitor(Opcodes.ASM4) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean b) {
                onType(desc);

                return av;
            }

            @Override
            public AnnotationVisitor visitParameterAnnotation(int i, String desc, boolean b) {
                onType(desc);

                return av;
            }

            @Override
            public AnnotationVisitor visitAnnotationDefault() {
                return av;
            }

            @Override
            public void visitFieldInsn(int i, String owner, String name, String desc) {
                onType(owner);
                onType(desc);
            }

            @Override
            public void visitFrame(int i, int i2, Object[] locTypes, int i3, Object[] stackTypes) {
                for (Object o : locTypes) {
                    if (o instanceof String)
                        onType((String) o);
                }

                for (Object o : stackTypes) {
                    if (o instanceof String)
                        onType((String) o);
                }
            }

            @Override
            public void visitLocalVariable(String name, String desc, String signature, Label lb, Label lb2,
                    int i) {
                onType(desc);
            }

            @Override
            public void visitMethodInsn(int i, String owner, String name, String desc) {
                onType(owner);
            }

            @Override
            public void visitMultiANewArrayInsn(String desc, int dim) {
                onType(desc);
            }

            @Override
            public void visitTryCatchBlock(Label lb, Label lb2, Label lb3, String e) {
                onType(e);
            }
        };

        void onClass(String depCls) {
            assert validateClassName(depCls) : depCls;

            if (depCls.startsWith("java.")) // Filter out platform classes.
                return;

            if (visited.contains(depCls))
                return;

            Boolean res = cache.get(depCls);

            if (res == Boolean.TRUE || (res == null && hasExternalDependencies(depCls, visited)))
                hasDeps.set(true);
        }

        void onType(String type) {
            if (type == null)
                return;

            int off = 0;

            while (type.charAt(off) == '[')
                off++; // Handle arrays.

            if (off != 0)
                type = type.substring(off);

            if (type.length() == 1)
                return; // Get rid of primitives.

            if (type.charAt(type.length() - 1) == ';') {
                assert type.charAt(0) == 'L' : type;

                type = type.substring(1, type.length() - 1);
            }

            type = type.replace('/', '.');

            onClass(type);
        }

        @Override
        public void visit(int i, int i2, String name, String signature, String superName, String[] ifaces) {
            onType(superName);

            if (ifaces != null) {
                for (String iface : ifaces)
                    onType(iface);
            }
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            onType(desc);

            return av;
        }

        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int i) {
            onType(name);
        }

        @Override
        public FieldVisitor visitField(int i, String name, String desc, String signature, Object val) {
            onType(desc);

            return fv;
        }

        @Override
        public MethodVisitor visitMethod(int i, String name, String desc, String signature,
                String[] exceptions) {
            if (exceptions != null) {
                for (String e : exceptions)
                    onType(e);
            }

            return mv;
        }
    }, 0);

    if (hasDeps.get()) // We already know that we have dependencies, no need to check parent.
        return true;

    // Here we are known to not have any dependencies but possibly we have a parent which have them.
    int idx = clsName.lastIndexOf('$');

    if (idx == -1) // No parent class.
        return false;

    String parentCls = clsName.substring(0, idx);

    if (visited.contains(parentCls))
        return false;

    Boolean res = cache.get(parentCls);

    if (res == null)
        res = hasExternalDependencies(parentCls, visited);

    return res;
}

From source file:org.jacoco.core.internal.flow.ClassProbesAdapter.java

License:Open Source License

/**
 * Creates a new adapter that delegates to the given visitor.
 * /*  w w  w .  jav a2s.c  o m*/
 * @param cv
 *            instance to delegate to
 */
public ClassProbesAdapter(final ClassProbesVisitor cv) {
    super(Opcodes.ASM4, cv);
    this.cv = cv;
}

From source file:org.jacoco.core.internal.flow.ClassProbesVisitor.java

License:Open Source License

/**
 * New visitor instance that delegates to the given visitor.
 * /*from w w w  . jav a 2s  .  co  m*/
 * @param cv
 *            optional next visitor in chain
 */
public ClassProbesVisitor(final ClassVisitor cv) {
    super(Opcodes.ASM4, cv);
}

From source file:org.jacoco.core.internal.flow.LabelFlowAnalyzer.java

License:Open Source License

/**
 * Create new instance.
 */
public LabelFlowAnalyzer() {
    super(Opcodes.ASM4);
}

From source file:org.jacoco.core.internal.flow.MethodProbesAdapter.java

License:Open Source License

/**
 * Create a new adapter instance.//from w w w.j a va 2 s .  c  o m
 * 
 * @param probesVisitor
 *            visitor to delegate to
 * @param idGenerator
 *            generator for unique probe ids
 */
public MethodProbesAdapter(final MethodProbesVisitor probesVisitor, final IProbeIdGenerator idGenerator) {
    super(Opcodes.ASM4, probesVisitor);
    this.probesVisitor = probesVisitor;
    this.idGenerator = idGenerator;
}

From source file:org.jacoco.core.internal.flow.MethodProbesVisitor.java

License:Open Source License

/**
 * New visitor instance that delegates to the given visitor.
 * /* w w w.j a v  a 2  s  .  c om*/
 * @param mv
 *            optional next visitor in chain
 */
public MethodProbesVisitor(final MethodVisitor mv) {
    super(Opcodes.ASM4, mv);
}