Example usage for org.objectweb.asm ClassReader ClassReader

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

Introduction

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

Prototype

ClassReader(final byte[] classFileBuffer, final int classFileOffset, final boolean checkClassVersion) 

Source Link

Document

Constructs a new ClassReader object.

Usage

From source file:com.android.build.gradle.internal.incremental.fixture.ClassEnhancement.java

License:Apache License

@SuppressWarnings("unused") // Helpful for debugging.
public static String traceClass(byte[] bytes) {
    ClassReader classReader = new ClassReader(bytes, 0, bytes.length);
    StringWriter sw = new StringWriter();
    TraceClassVisitor traceClassVisitor = new TraceClassVisitor(new PrintWriter(sw));
    classReader.accept(traceClassVisitor, 0);
    return sw.toString();
}

From source file:com.coradec.corajet.cldr.CarInjector.java

License:Open Source License

/**
 * Embeds the class file in the specified buffer by resolving its static injection points and
 * prepares it to resolve its instance injection points.
 *
 * @param name   the name of the class./*from   ww w. j  a  v a2s . c om*/
 * @param buffer the buffer containing the class file.
 * @param off    the offset in the buffer at which the class file starts.
 * @param len    the length of the class file in the buffer.
 * @return a byte array containing the embedded class.
 */
public byte[] embedClass(final String name, final byte[] buffer, final int off, final int len) {
    ClassReader reader = new ClassReader(buffer, off, len);
    ClassWriter writer = new ClassWriter(reader, 0);
    reader.accept(new ClassModeler(writer), 0);
    return writer.toByteArray();
}

From source file:com.coradec.corajet.cldr.CarInjector.java

License:Open Source License

/**
 * Embeds the class file in the specified buffer by resolving its static injection points and
 * prepares it to resolve its instance injection points.
 *
 * @param name   the name of the class./*from ww w.ja  v a  2  s .c om*/
 * @param buffer the buffer containing the class file.
 * @param off    the offset in the buffer at which the class file starts.
 * @param len    the length of the class file in the buffer.
 * @return bit 0 set if the class is a component, bit 1 set if the class is an implementation,
 * all other bits are 0.
 */
public int analyzeClass(final String name, final byte[] buffer, final int off, final int len) {
    ClassReader reader = new ClassReader(buffer, off, len);
    ClassWriter writer = new ClassWriter(reader, 0);
    final ClassAnalyzer classAnalyzer = new ClassAnalyzer();
    reader.accept(classAnalyzer, 0);
    return classAnalyzer.getState();
}

From source file:com.google.code.jtracert.instrument.impl.asm.JTracertASMByteCodeTransformer.java

License:Open Source License

/**
 * @param originalBytes/*from w ww  .j  a  va2  s  . c  om*/
 * @param offset
 * @param length
 * @return
 * @throws ByteCodeTransformException
 */
@Override
public byte[] transform(byte[] originalBytes, int offset, int length, boolean instrumentClass)
        throws ByteCodeTransformException {

    try {

        ClassReader classReader = new ClassReader(originalBytes, offset, length);

        ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);

        ClassVisitor classVisitor;
        classVisitor = new JTracertClassAdapter(classWriter, getInstrumentationProperties(), instrumentClass);

        classReader.accept(classVisitor, ClassReader.SKIP_FRAMES);

        byte[] transformedBytes = classWriter.toByteArray();

        //dumpBytes(transformedBytes);

        return transformedBytes;

    } catch (Throwable e) {
        throw new ByteCodeTransformException(e);
    }

}

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   ww  w  .  j a  v a 2 s  .  c  om

            super.visitSource(javaSource, debug);
        };

    }, 0);

    return w.toByteArray();
}