Example usage for org.objectweb.asm Opcodes ASM5

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

Introduction

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

Prototype

int ASM5

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

Click Source Link

Usage

From source file:com.google.monitoring.runtime.instrumentation.adapters.VerifyingClassAdapter.java

License:Apache License

/**
 * @param cw        A class writer that is wrapped by this class adapter
 * @param original  the original bytecode
 * @param className the name of the class being examined.
 *///from  www  .  j  a v a2s . c  om
public VerifyingClassAdapter(final ClassWriter cw, final byte[] original, final String className) {
    super(Opcodes.ASM5, cw);
    state = State.UNKNOWN;
    message = "The class has not finished being examined";
    this.cw = cw;
    this.original = original;
    this.className = className.replace('/', '.');
}

From source file:com.google.template.soy.jbcsrc.BytecodeProducer.java

License:Apache License

/**
 * Returns a human readable string for the code that this {@link BytecodeProducer} generates.
 *///from   w w  w .  jav a2s .  c om
final String trace() {
    // TODO(lukes): textifier has support for custom label names by overriding appendLabel.
    // Consider trying to make use of (using the Label.info field? adding a custom NamedLabel
    // sub type?)
    Textifier textifier = new Textifier(Opcodes.ASM5) {
        {
            // reset tab sizes.  Since we don't care about formatting class names or method signatures
            // (only code). We only need to set the tab2,tab3 and ltab settings (tab is for class
            // members).
            this.tab = null; // trigger an error if used.
            this.tab2 = "  "; // tab setting for instructions
            this.tab3 = ""; // tab setting for switch cases
            this.ltab = ""; // tab setting for labels
        }
    };
    gen(new CodeBuilder(new TraceMethodVisitor(textifier), 0, "trace", "()V"));
    StringWriter writer = new StringWriter();
    textifier.print(new PrintWriter(writer));
    return writer.toString(); // Note textifier always adds a trailing newline
}

From source file:com.google.template.soy.jbcsrc.CodeBuilder.java

License:Apache License

CodeBuilder(MethodVisitor mv, int access, String name, String desc) {
    super(Opcodes.ASM5, mv);
    this.adapter = new GeneratorAdapter(mv, access, name, desc);
}

From source file:com.heliosdecompiler.appifier.Appifier.java

License:Apache License

private static byte[] transform(byte[] classBytes) {
    ClassReader reader = new ClassReader(classBytes);
    ClassWriter writer = new ClassWriter(Opcodes.ASM5);

    reader.accept(new ClassVisitor(Opcodes.ASM5, writer) {
        @Override//from w w  w .ja  va  2  s.  com
        public MethodVisitor visitMethod(int i, String s, String s1, String s2, String[] strings) {
            return new MethodVisitor(Opcodes.ASM5, writer.visitMethod(i, s, s1, s2, strings)) {
                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (opcode == Opcodes.GETSTATIC) {
                        if (owner.equals("java/lang/System")) {
                            if (desc.equals("Ljava/io/PrintStream;")) {
                                if (name.equals("out")) {
                                    super.visitFieldInsn(Opcodes.GETSTATIC,
                                            "com/heliosdecompiler/appifier/SystemHook", "out",
                                            "Ljava/lang/ThreadLocal;");
                                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "get",
                                            "()Ljava/lang/Object;", false);
                                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/io/PrintStream");
                                    return;
                                } else if (name.equals("err")) {
                                    super.visitFieldInsn(Opcodes.GETSTATIC,
                                            "com/heliosdecompiler/appifier/SystemHook", "err",
                                            "Ljava/lang/ThreadLocal;");
                                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "get",
                                            "()Ljava/lang/Object;", false);
                                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/io/PrintStream");
                                    return;
                                }
                            }
                        }
                    }
                    super.visitFieldInsn(opcode, owner, name, desc);
                }
            };
        }
    }, 0);

    return writer.toByteArray();
}

From source file:com.ifedorenko.m2e.sourcelookup.javaagent.ClassfileTransformer.java

License:Open Source License

public byte[] transform(byte[] classfileBuffer, final String location) {

    final ClassReader r = new ClassReader(classfileBuffer, 0, classfileBuffer.length);
    final ClassWriter w = new ClassWriter(0);

    r.accept(new ClassVisitor(Opcodes.ASM5, w) {
        public void visitSource(String source, String debug) {
            String javaSource = source;
            if (debug != null) {
                System.err.println("m2e SMAP merge is not supported!");
                System.err.println(debug);
            } else {
                StringBuilder smap = new StringBuilder();
                smap.append("SMAP\n");
                smap.append(javaSource).append("\n");
                smap.append("Java\n"); // default strata name
                smap.append("*S m2e\n");
                smap.append("*F\n");
                smap.append("1 ").append(source).append("\n");
                smap.append("2 ").append(location).append("\n");
                // JSR-045, StratumSection
                // "One FileSection and one LineSection (in either order) must follow the StratumSection"
                smap.append("*L\n");
                smap.append("*E\n");
                debug = smap.toString();
            }/*from   w  ww . j a v a2 s .c o  m*/

            super.visitSource(javaSource, debug);
        };

    }, 0);

    return w.toByteArray();
}

From source file:com.igormaznitsa.jute.TestClassProcessor.java

License:Apache License

TestClassProcessor(final boolean onlyAnnotated, final String juteTestParameter, final String classFilePath,
        final TestContainer baseParameters, final Log logger, final List<TestContainer> resultList,
        final String[] includedTestPatterns, final String[] excludedTestPatterns) {
    super(Opcodes.ASM5);
    this.onlyAnnotated = onlyAnnotated;
    this.juteTestParameter = juteTestParameter;
    this.detectedTestMethodList = resultList;
    this.includedTests = includedTestPatterns;
    this.excludedTests = excludedTestPatterns;
    this.logger = logger;
    this.baseParameters = baseParameters;
    this.classFilePath = classFilePath;
}

From source file:com.igormaznitsa.jute.TestClassProcessor.java

License:Apache License

@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
        final String[] exceptions) {
    if (this.inappropriateClass) {
        return null;
    }/*from w w  w  . j a va  2s. co m*/

    if (((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE | Opcodes.ACC_STATIC)) != 0)
            || !desc.equals("()V") || name.startsWith("<")) {
        return null;
    }
    final boolean foundInExcludedList = isTestIncluded(desc);
    final boolean testExcluded = isTestExcluded(desc);

    final String logTestName = this.className + '#' + name;

    if (!foundInExcludedList) {
        this.logger.info("Test method " + logTestName + " is ignored because not presented in include list");
        return null;
    }

    if (testExcluded) {
        this.logger.info("Test " + logTestName + " is ignored because presented in exclude list");
        return null;
    }

    return new MethodVisitor(Opcodes.ASM5) {
        private boolean junitTest;
        private boolean juteTest;
        private boolean junitIgnore;
        private TestContainer detectedMethod;

        @Override
        public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
            if (detectedMethod == null) {
                detectedMethod = new TestContainer(classFilePath, className, name,
                        clazzJuteTestParameters == null ? baseParameters : clazzJuteTestParameters, null);
            }

            AnnotationVisitor result = null;
            if (desc.equals(JuteMojo.ANNO_TEST)) {
                this.junitTest = true;
            } else if (desc.equals(JuteMojo.ANNO_IGNORE)) {
                this.junitIgnore = true;
            } else if (desc.equals(JuteMojo.ANNO_JUTE)) {
                this.juteTest = true;
                result = detectedMethod;
            }
            return result;
        }

        @Override
        public void visitEnd() {
            if (this.detectedMethod == null) {
                this.detectedMethod = new TestContainer(classFilePath, className, name,
                        clazzJuteTestParameters == null ? baseParameters : clazzJuteTestParameters, null);
            }
            this.juteTest = this.juteTest || clazzJuteTestParameters != null;

            this.detectedMethod.setJUnitIgnore(this.junitIgnore);
            this.detectedMethod.setJUnitTest(this.junitTest);
            this.detectedMethod.setJuteTest(this.juteTest);

            if ((this.junitTest || this.juteTest)
                    && Utils.checkClassAndMethodForPattern(juteTestParameter,
                            this.detectedMethod.getClassName(), this.detectedMethod.getMethodName(), false)
                    && isTestCanBeListed(this.detectedMethod)) {
                detectedTestMethodList.add(detectedMethod);
            }
        }
    };
}

From source file:com.igormaznitsa.jute.TestContainer.java

License:Apache License

public TestContainer(final String classFilePath, final String className, final String testName,
        final String jvm, final String[] jvmOpts, final String in, final int order,
        final boolean enforcePrintConsole, final boolean skip, final long timeout) {
    super(Opcodes.ASM5);
    this.classFilePath = classFilePath;
    this.className = className;
    this.methodName = testName;
    this.jvm = jvm;
    this.in = in;
    this.order = order;
    this.printConsole = enforcePrintConsole;
    this.skip = skip;
    this.timeout = timeout;
    if (jvmOpts != null) {
        for (final String s : jvmOpts) {
            this.jvmOpts.add(s);
        }/*from ww w  .j av a 2s  .  c om*/
    }
    this.endCall = null;
}

From source file:com.igormaznitsa.jute.TestContainer.java

License:Apache License

public TestContainer(final String classFilePath, final String className, final String testName,
        final TestContainer base, final Runnable endCall) {
    super(Opcodes.ASM5);
    this.classFilePath = classFilePath;
    this.className = className;
    this.methodName = testName;
    if (base != null) {
        this.jvm = base.jvm;
        this.in = base.in;
        this.order = base.order;
        this.printConsole = base.printConsole;
        this.skip = base.skip;
        this.timeout = base.timeout;
        this.jvmOpts.addAll(base.jvmOpts);
        this.junitIgnore = base.junitIgnore;
        this.juteTest = base.juteTest;
    }/*from   w ww. j  a  v a2 s.co  m*/
    this.endCall = endCall;
}

From source file:com.intellij.AbstractNotNullInstrumenterTask.java

License:Apache License

private static int getClassFileVersion(@NotNull final ClassReader reader) {
    final int[] classFileVersion = new int[1];
    reader.accept(new ClassVisitor(Opcodes.ASM5) {
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            classFileVersion[0] = version;
        }/* www .  j  a v  a  2s. c om*/
    }, 0);
    return classFileVersion[0];
}