Example usage for org.objectweb.asm ClassVisitor ClassVisitor

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

Introduction

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

Prototype

public ClassVisitor(final int api) 

Source Link

Document

Constructs a new ClassVisitor .

Usage

From source file:scouter.agent.DirectPatch.java

License:Apache License

private static String getClassName(byte[] bytes) {
    try {//from w w  w .ja va2s  . com
        final ClassDesc classDesc = new ClassDesc();
        ClassReader cr = new ClassReader(bytes);
        cr.accept(new ClassVisitor(Opcodes.ASM7) {
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                classDesc.set(version, access, name, signature, superName, interfaces);
            }
        }, 0);
        return classDesc.name.replace('.', '/');
    } catch (Throwable t) {
        return null;
    }
}

From source file:scouter.agent.LambdaFormTransformer.java

License:Apache License

public byte[] transform(final ClassLoader loader, String className, final Class classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer, String lambdaMethodName,
        String lambdaMethodDesc, String factoryMethodName, String factoryMethodDesc)
        throws IllegalClassFormatException {
    try {/*from   w w  w . j  av a2 s .c om*/
        if (className == null)
            return null;

        boolean scoped = false;

        for (int i = 0; i < scanScopePrefix.size(); i++) {
            if (className.indexOf(scanScopePrefix.get(i)) == 0) {
                scoped = true;
                break;
            }
        }

        if (!scoped) {
            return null;
        }

        final ClassDesc classDesc = new ClassDesc();
        ClassReader cr = new ClassReader(classfileBuffer);
        cr.accept(new ClassVisitor(Opcodes.ASM7) {
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                classDesc.set(version, access, name, signature, superName, interfaces);
                super.visit(version, access, name, signature, superName, interfaces);
            }
        }, 0);
        if (AsmUtil.isInterface(classDesc.access)) {
            return null;
        }
        classDesc.classBeingRedefined = classBeingRedefined;
        ClassWriter cw = getClassWriter(classDesc);
        ClassVisitor cv = cw;
        List<ILASM> workAsms = asms;
        for (int i = workAsms.size() - 1; i >= 0; i--) {
            cv = workAsms.get(i).transform(cv, className, classDesc, lambdaMethodName, lambdaMethodDesc,
                    factoryMethodName, factoryMethodDesc);
            if (cv != cw) {
                cr = new ClassReader(classfileBuffer);
                cr.accept(cv, ClassReader.EXPAND_FRAMES);
                classfileBuffer = cw.toByteArray();
                cv = cw = getClassWriter(classDesc);
            }
        }
        return classfileBuffer;
    } catch (Throwable t) {
        Logger.println("B101", "LambdaFormTransformer Error", t);
        t.printStackTrace();
    } finally {
    }
    return null;
}

From source file:se.eris.functional.nested.NestedAnnotatedInstrumenterTest.java

License:Apache License

private List<AsmInnerClass> getAsmInnerClasses(final ClassReader cr) {
    final List<AsmInnerClass> asmInnerClasses = new ArrayList<>();
    cr.accept(new ClassVisitor(AsmUtils.ASM_OPCODES_VERSION) {
        @Override//from w  w  w .  jav  a2s .co  m
        public void visitInnerClass(final String name, final String outerName, final String innerName,
                final int access) {
            asmInnerClasses.add(new AsmInnerClass(name, outerName, innerName, access));
        }
    }, 0);
    return asmInnerClasses;
}

From source file:se.eris.functional.nested.NestedAnnotatedInstrumenterTest.java

License:Apache License

@NotNull
private List<String> getStringConstants(final ClassReader cr, final String methodName) {
    final List<String> strings = new ArrayList<>();
    cr.accept(new ClassVisitor(AsmUtils.ASM_OPCODES_VERSION) {
        @Override//from   ww  w  .j  a  v a2 s  .  co  m
        public MethodVisitor visitMethod(final int access, final String name, final String desc,
                final String signature, final String[] exceptions) {
            if (name.equals(methodName)) {
                return new MethodVisitor(AsmUtils.ASM_OPCODES_VERSION) {
                    @Override
                    public void visitLdcInsn(final Object cst) {
                        if (cst instanceof String) {
                            strings.add(desc + ":" + cst);
                        }
                    }
                };
            }
            return super.visitMethod(access, name, desc, signature, exceptions);
        }
    }, 0);
    return strings;
}