Example usage for org.objectweb.asm MethodVisitor MethodVisitor

List of usage examples for org.objectweb.asm MethodVisitor MethodVisitor

Introduction

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

Prototype

public MethodVisitor(final int api) 

Source Link

Document

Constructs a new MethodVisitor .

Usage

From source file:org.glowroot.agent.embedded.preinit.MyRemappingMethodAdapter.java

License:Apache License

MyRemappingMethodAdapter(int access, String descriptor, MethodCollector remapper) {
    super(ASM7, access, descriptor, new MethodVisitor(ASM7) {
    });//from www.  j  a va  2  s.  c  om
    this.remapper = remapper;
}

From source file:org.glowroot.agent.weaving.preinit.MyRemappingMethodAdapter.java

License:Apache License

MyRemappingMethodAdapter(int access, String desc, MethodCollector remapper) {
    super(ASM5, access, desc, new MethodVisitor(ASM5) {
    });/*from   www.j a  v a2s . co  m*/
    this.remapper = remapper;
}

From source file:org.gradle.api.internal.tasks.compile.ApiMemberSelector.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if ("<clinit>".equals(name)) {
        // discard static initializers
        return null;
    }// ww  w.  j a  v  a  2  s.  co  m
    if (isCandidateApiMember(access, apiIncludesPackagePrivateMembers)
            || ("<init>".equals(name) && isInnerClass)) {
        final MethodMember methodMember = new MethodMember(access, name, desc, signature, exceptions);
        methods.add(methodMember);
        return new MethodVisitor(ASM5) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                AnnotationMember ann = new AnnotationMember(desc, visible);
                methodMember.addAnnotation(ann);
                return new SortingAnnotationVisitor(ann, super.visitAnnotation(desc, visible));
            }

            @Override
            public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
                ParameterAnnotationMember ann = new ParameterAnnotationMember(desc, visible, parameter);
                methodMember.addParameterAnnotation(ann);
                return new SortingAnnotationVisitor(ann,
                        super.visitParameterAnnotation(parameter, desc, visible));
            }
        };
    }
    return null;
}

From source file:org.gradle.api.internal.tasks.testing.junit.JUnitTestClassDetector.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (!isTest()) {
        return new MethodVisitor(AsmConstants.ASM_LEVEL) {
            @Override//w w w  .j  a va 2s  .  c o m
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                if ("Lorg/junit/Test;".equals(desc)) {
                    setTest(true);
                }
                return null;
            }
        };
    } else {
        return null;
    }
}

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   w w w  . j av  a 2s  .c  om*/
    }

    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./*from   w w  w . ja v a 2s  .  c  om*/
 * @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.h2.test.unit.TestKeywords.java

License:Mozilla Public License

@Override
public void test() throws Exception {
    final HashSet<String> set = new HashSet<>();
    ClassReader r = new ClassReader(Parser.class.getResourceAsStream("Parser.class"));
    r.accept(new ClassVisitor(Opcodes.ASM6) {
        @Override//from   ww w  .  j av a2  s. c o m
        public FieldVisitor visitField(int access, String name, String descriptor, String signature,
                Object value) {
            add(set, value);
            return null;
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
                String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM6) {
                @Override
                public void visitLdcInsn(Object value) {
                    add(set, value);
                }
            };
        }

        void add(HashSet<String> set, Object value) {
            if (!(value instanceof String)) {
                return;
            }
            String s = (String) value;
            int l = s.length();
            if (l == 0 || ParserUtil.getSaveTokenType(s, false, 0, l, true) != ParserUtil.IDENTIFIER) {
                return;
            }
            for (int i = 0; i < l; i++) {
                char ch = s.charAt(i);
                if ((ch < 'A' || ch > 'Z') && ch != '_') {
                    return;
                }
            }
            set.add(s);
        }
    }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    try (Connection conn = DriverManager.getConnection("jdbc:h2:mem:keywords")) {
        Statement stat = conn.createStatement();
        for (String s : set) {
            // _ROWID_ is a special virtual column
            String column = s.equals("_ROWID_") ? "C" : s;
            try {
                stat.execute("CREATE TABLE " + s + '(' + column + " INT)");
                stat.execute("INSERT INTO " + s + '(' + column + ") VALUES (10)");
                try (ResultSet rs = stat.executeQuery("SELECT " + column + " FROM " + s)) {
                    assertTrue(rs.next());
                    assertEquals(10, rs.getInt(1));
                    assertFalse(rs.next());
                }
            } catch (Throwable t) {
                throw new AssertionError(s + " cannot be used as identifier.", t);
            }
        }
    }
}

From source file:org.jacoco.core.instr.ClassFileVersionsTest.java

License:Open Source License

private void assertFrames(byte[] source, final boolean expected) {
    InstrSupport.classReaderFor(source).accept(new ClassVisitor(InstrSupport.ASM_API_VERSION) {

        @Override/* w w w.j  a v a  2 s  .  c  o m*/
        public MethodVisitor visitMethod(int access, String name, final String desc, String signature,
                String[] exceptions) {
            return new MethodVisitor(InstrSupport.ASM_API_VERSION) {
                boolean frames = false;

                @Override
                public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
                    frames = true;
                }

                @Override
                public void visitEnd() {
                    if (CondyProbeArrayStrategy.B_DESC.equals(desc)) {
                        assertFalse("CondyProbeArrayStrategy does not need frames", frames);
                    } else {
                        assertEquals(Boolean.valueOf(expected), Boolean.valueOf(frames));
                    }
                }
            };
        }
    }, 0);
}

From source file:org.jacoco.core.runtime.RuntimeTestBase.java

License:Open Source License

@Test
public void testNoLocalVariablesInDataAccessor() throws InstantiationException, IllegalAccessException {
    runtime.generateDataAccessor(1001, "Target", 5, new MethodVisitor(Opcodes.ASM4) {
        @Override//  w w w .j  a va2s . co  m
        public void visitVarInsn(int opcode, int var) {
            fail("No usage of local variables allowed.");
        }
    });
}

From source file:org.jacoco.core.test.validation.ClassFileVersionsTest.java

License:Open Source License

private void assertFrames(byte[] source, boolean expected) {
    final boolean[] hasFrames = new boolean[] { false };
    new ClassReader(source).accept(new ClassVisitor(Opcodes.ASM4) {

        @Override/*from www. j ava  2  s.c om*/
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM4) {

                @Override
                public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
                    hasFrames[0] = true;
                }

            };
        }

    }, 0);
    assertEquals(Boolean.valueOf(expected), Boolean.valueOf(hasFrames[0]));
}