Search in sources :

Example 36 with ClassWriter

use of org.objectweb.asm.ClassWriter in project robovm by robovm.

the class ObjCProtocolProxyPlugin method beforeClass.

@Override
public void beforeClass(Config config, Clazz clazz, ModuleBuilder moduleBuilder) {
    init();
    SootClass sootClass = clazz.getSootClass();
    if (isObjCProtocol(sootClass)) {
        try {
            String proxyInternalName = clazz.getInternalName() + PROXY_CLASS_NAME_SUFFIX;
            ArrayList<String> interfazes = new ArrayList<>();
            collectProxyInterfaceInternalNames(sootClass, interfazes);
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            cw.visit(51, ACC_SUPER + ACC_FINAL + ACC_SYNTHETIC + ACC_PUBLIC, proxyInternalName, null, getProxySuperclassInternalName(sootClass), new String[] { clazz.getInternalName() });
            generateProxyMethods(config, interfazes, cw);
            cw.visitEnd();
            File f = clazz.getPath().getGeneratedClassFile(proxyInternalName);
            FileUtils.writeByteArrayToFile(f, cw.toByteArray());
            // The proxy class is created after the interface is compiled.
            // This prevents the triggering of a recompile of the interface.
            f.setLastModified(clazz.lastModified());
            // Add the proxy class as a dependency for the protocol interface.
            // Important! This must be done AFTER the class file has been written.
            clazz.getClazzInfo().addClassDependency(proxyInternalName, false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SootClass(soot.SootClass) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter)

Example 37 with ClassWriter

use of org.objectweb.asm.ClassWriter in project robovm by robovm.

the class AnnotationImplPlugin method beforeClass.

@Override
public void beforeClass(Config config, Clazz clazz, ModuleBuilder moduleBuilder) {
    init();
    SootClass sootClass = clazz.getSootClass();
    if ((sootClass.getModifiers() & MOD_ANNOTATION) > 0) {
        try {
            String implInternalName = clazz.getInternalName() + IMPL_CLASS_NAME_SUFFIX;
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
            cw.visit(V1_7, ACC_SUPER + ACC_FINAL + ACC_SYNTHETIC + ACC_PUBLIC, implInternalName, null, BASE_CLASS, new String[] { clazz.getInternalName() });
            generateConstructor(clazz, cw);
            generateAnnotationTypeMethod(clazz, cw);
            generateMembersToStringMethod(clazz, cw);
            generateFastEqualsMethod(clazz, cw);
            generateSlowEqualsMethod(clazz, cw);
            generateHashCodeMethod(clazz, cw);
            generateMemberFieldsAndAccessorMethods(clazz, cw);
            generateSetDefaultsMethod(clazz, cw);
            generateSingletonFactoryMethod(clazz, cw);
            generateFactoryMethod(clazz, cw);
            cw.visitEnd();
            File f = clazz.getPath().getGeneratedClassFile(implInternalName);
            FileUtils.writeByteArrayToFile(f, cw.toByteArray());
            // The impl class is created after the interface is compiled.
            // This prevents the triggering of a recompile of the interface.
            f.setLastModified(clazz.lastModified());
            // Add the impl class as a dependency for the annotation interface.
            // Important! This must be done AFTER the class file has been written.
            clazz.getClazzInfo().addClassDependency(implInternalName, false);
            // Make sure the factory methods are always linked in when the
            // annotation class is referenced.
            clazz.getClazzInfo().addInvokeMethodDependency(implInternalName, "$createSingleton", "()Ljava/lang/Object;", false);
            clazz.getClazzInfo().addInvokeMethodDependency(implInternalName, "$create", "()Ljava/lang/Object;", false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : IOException(java.io.IOException) SootClass(soot.SootClass) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter)

Example 38 with ClassWriter

use of org.objectweb.asm.ClassWriter in project robolectric by robolectric.

the class SandboxClassLoader method getInstrumentedBytes.

private byte[] getInstrumentedBytes(ClassNode classNode, boolean containsStubs) throws ClassNotFoundException {
    if (InvokeDynamic.ENABLED) {
        new InvokeDynamicClassInstrumentor(classNode, containsStubs).instrument();
    } else {
        new OldClassInstrumentor(classNode, containsStubs).instrument();
    }
    ClassWriter writer = new InstrumentingClassWriter(classNode);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter)

Example 39 with ClassWriter

use of org.objectweb.asm.ClassWriter in project quasar by puniverse.

the class QuasarInstrumentor method instrumentClass.

@VisibleForTesting
byte[] instrumentClass(ClassLoader loader, String className, InputStream is, boolean forceInstrumentation) throws IOException {
    className = className != null ? className.replace('.', '/') : null;
    byte[] cb = toByteArray(is);
    MethodDatabase db = getMethodDatabase(loader);
    if (className != null) {
        log(LogLevel.INFO, "TRANSFORM: %s %s", className, (db.getClassEntry(className) != null && db.getClassEntry(className).requiresInstrumentation()) ? "request" : "");
        examine(className, "quasar-1-preinstr", cb);
    } else {
        log(LogLevel.INFO, "TRANSFORM: null className");
    }
    // Phase 1, add a label before any suspendable calls, event API is enough
    final ClassReader r1 = new ClassReader(cb);
    final ClassWriter cw1 = new ClassWriter(r1, 0);
    final LabelSuspendableCallSitesClassVisitor ic1 = new LabelSuspendableCallSitesClassVisitor(cw1, db);
    r1.accept(ic1, 0);
    cb = cw1.toByteArray();
    examine(className, "quasar-2", cb);
    // Phase 2, instrument, tree API
    final ClassReader r2 = new ClassReader(cb);
    final ClassWriter cw2 = new DBClassWriter(db, r2);
    final ClassVisitor cv2 = (check && EXAMINED_CLASS == null) ? new CheckClassAdapter(cw2) : cw2;
    final InstrumentClass ic2 = new InstrumentClass(cv2, db, forceInstrumentation);
    try {
        r2.accept(ic2, ClassReader.SKIP_FRAMES);
        cb = cw2.toByteArray();
    } catch (final Exception e) {
        if (ic2.hasSuspendableMethods()) {
            error("Unable to instrument class " + className, e);
            throw e;
        } else {
            if (!MethodDatabase.isProblematicClass(className))
                log(LogLevel.DEBUG, "Unable to instrument class " + className);
            return null;
        }
    }
    examine(className, "quasar-4", cb);
    // Phase 4, fill suspendable call offsets, event API is enough
    final OffsetClassReader r3 = new OffsetClassReader(cb);
    final ClassWriter cw3 = new ClassWriter(r3, 0);
    final SuspOffsetsAfterInstrClassVisitor ic3 = new SuspOffsetsAfterInstrClassVisitor(cw3, db);
    r3.accept(ic3, 0);
    cb = cw3.toByteArray();
    // DEBUG
    if (EXAMINED_CLASS != null) {
        examine(className, "quasar-5-final", cb);
        if (check) {
            ClassReader r4 = new ClassReader(cb);
            ClassVisitor cv4 = new CheckClassAdapter(new TraceClassVisitor(null), true);
            r4.accept(cv4, 0);
        }
    }
    return cb;
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) VisibleForTesting(co.paralleluniverse.common.util.VisibleForTesting)

Example 40 with ClassWriter

use of org.objectweb.asm.ClassWriter in project felix by apache.

the class ProxyGenerator method dumpProxy.

/**
 * Generates a proxy class.
 * @param spec the proxied service specification
 * @return the byte[] for the generated proxy class.
 */
public static byte[] dumpProxy(Class spec) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    // Specification class internal name.
    String internalClassName = Type.getInternalName(spec);
    String[] itfs = new String[0];
    String parent = "java/lang/Object";
    if (spec.isInterface()) {
        // Implemented interface.
        itfs = new String[] { internalClassName };
    } else {
        parent = internalClassName;
    }
    // Unique name.
    String className = internalClassName + "$$Proxy";
    // Turn around the VM changes (FELIX-2716) about java.* classes.
    if (className.startsWith("java/")) {
        className = "$" + className;
    }
    // Method to delegate
    Method[] methods = spec.getMethods();
    cw.visit(Opcodes.V1_3, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, className, null, parent, itfs);
    addDependencyField(cw);
    // We try to call super() on the parent, however this should not be used as proxing does work only for interface.
    generateConstructor(cw, className, parent);
    // For each method, create the delegator code.
    for (int i = 0; i < methods.length; i++) {
        if ((methods[i].getModifiers() & (Modifier.STATIC | Modifier.FINAL)) == 0) {
            generateDelegator(cw, methods[i], className, internalClassName);
        }
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassWriter (org.objectweb.asm.ClassWriter)471 ClassReader (org.objectweb.asm.ClassReader)275 MethodVisitor (org.objectweb.asm.MethodVisitor)115 Test (org.junit.Test)94 ClassVisitor (org.objectweb.asm.ClassVisitor)82 ClassNode (org.objectweb.asm.tree.ClassNode)70 IOException (java.io.IOException)57 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)52 Label (org.objectweb.asm.Label)51 HashSet (java.util.HashSet)37 Method (java.lang.reflect.Method)36 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 Type (org.objectweb.asm.Type)30 File (java.io.File)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)27 FieldVisitor (org.objectweb.asm.FieldVisitor)26 InputStream (java.io.InputStream)25 MethodNode (org.objectweb.asm.tree.MethodNode)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23 InnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass)23