Search in sources :

Example 31 with ClassWriter

use of org.objectweb.asm.ClassWriter in project platform_frameworks_base by android.

the class StubMethodAdapterTest method testBoolean.

/**
     * @param methodPredicate tests if the method should be replaced
     */
private void testBoolean(BiPredicate<String, Type> methodPredicate, Consumer<Boolean> assertion, String methodName) throws Exception {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    // Always rename the class to avoid conflict with the original class.
    String newClassName = STUB_CLASS_NAME + '_';
    new ClassReader(STUB_CLASS_NAME).accept(new ClassAdapter(newClassName, writer, methodPredicate), 0);
    MyClassLoader myClassLoader = new MyClassLoader(newClassName, writer.toByteArray());
    Class<?> aClass = myClassLoader.loadClass(newClassName);
    assertTrue("StubClass not loaded by the classloader. Likely a bug in the test.", myClassLoader.findClassCalled);
    Method method = aClass.getMethod(methodName);
    Object o = aClass.newInstance();
    assertion.accept((Boolean) method.invoke(o));
}
Also used : ClassReader(org.objectweb.asm.ClassReader) Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter)

Example 32 with ClassWriter

use of org.objectweb.asm.ClassWriter in project platform_frameworks_base by android.

the class AsmGenerator method transform.

/**
     * Transforms a class.
     * <p/>
     * There are 3 kind of transformations:
     *
     * 1- For "mock" dependencies classes, we want to remove all code from methods and replace
     * by a stub. Native methods must be implemented with this stub too. Abstract methods are
     * left intact. Modified classes must be overridable (non-private, non-final).
     * Native methods must be made non-final, non-private.
     *
     * 2- For "keep" classes, we want to rewrite all native methods as indicated above.
     * If a class has native methods, it must also be made non-private, non-final.
     *
     * Note that unfortunately static methods cannot be changed to non-static (since static and
     * non-static are invoked differently.)
     */
byte[] transform(ClassReader cr, boolean stubNativesOnly) {
    boolean hasNativeMethods = hasNativeMethods(cr);
    // Get the class name, as an internal name (e.g. com/android/SomeClass$InnerClass)
    String className = cr.getClassName();
    String newName = transformName(className);
    // transformName returns its input argument if there's no need to rename the class
    if (!newName.equals(className)) {
        mRenameCount++;
        // This class is being renamed, so remove it from the list of classes not renamed.
        mClassesNotRenamed.remove(className);
    }
    mLog.debug("Transform %s%s%s%s", className, newName.equals(className) ? "" : " (renamed to " + newName + ")", hasNativeMethods ? " -- has natives" : "", stubNativesOnly ? " -- stub natives only" : "");
    // Rewrite the new class from scratch, without reusing the constant pool from the
    // original class reader.
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = cw;
    // FIXME Generify
    if ("android/content/res/Resources".equals(className)) {
        cv = new FieldInjectorAdapter(cv);
    }
    if (mReplaceMethodCallsClasses.contains(className)) {
        cv = new ReplaceMethodCallsAdapter(cv, className);
    }
    cv = new RefactorClassAdapter(cv, mRefactorClasses);
    if (!newName.equals(className)) {
        cv = new RenameClassAdapter(cv, className, newName);
    }
    String binaryNewName = newName.replace('/', '.');
    if (mInjectedMethodsMap.keySet().contains(binaryNewName)) {
        cv = new InjectMethodsAdapter(cv, mInjectedMethodsMap.get(binaryNewName));
    }
    cv = new TransformClassAdapter(mLog, mStubMethods, mDeleteReturns.get(className), newName, cv, stubNativesOnly);
    Set<String> delegateMethods = mDelegateMethods.get(className);
    if (delegateMethods != null && !delegateMethods.isEmpty()) {
        // known to have no native methods, just skip this step.
        if (hasNativeMethods || !(delegateMethods.size() == 1 && delegateMethods.contains(DelegateClassAdapter.ALL_NATIVES))) {
            cv = new DelegateClassAdapter(mLog, cv, className, delegateMethods);
        }
    }
    Set<String> promoteFields = mPromotedFields.get(className);
    if (promoteFields != null && !promoteFields.isEmpty()) {
        cv = new PromoteFieldClassAdapter(cv, promoteFields);
    }
    cr.accept(cv, 0);
    return cw.toByteArray();
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 33 with ClassWriter

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

the class JavaAgent method crazyClojureOnceDisable.

public static byte[] crazyClojureOnceDisable(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (!Boolean.parseBoolean(System.getProperty("co.paralleluniverse.pulsar.disableOnce", "false")))
        return classfileBuffer;
    final ClassReader cr = new ClassReader(classfileBuffer);
    final ClassWriter cw = new ClassWriter(cr, 0);
    final ClassVisitor cv = new ClassVisitor(ASMAPI, cw) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            return new MethodVisitor(api, super.visitMethod(access, name, desc, signature, exceptions)) {

                @Override
                public void visitLdcInsn(Object cst) {
                    if (cst instanceof String && cst.equals("once")) {
                        super.visitLdcInsn("once$disabled-by-pulsar");
                    } else
                        super.visitLdcInsn(cst);
                }
            };
        }
    };
    cr.accept(cv, 0);
    return cw.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 34 with ClassWriter

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

the class RuntimeShadedJarCreator method remapClass.

private byte[] remapClass(String className, byte[] bytes) {
    ClassReader classReader = new ClassReader(bytes);
    ClassWriter classWriter = new ClassWriter(0);
    ClassVisitor remappingVisitor = new ShadingClassRemapper(classWriter, remapper);
    try {
        classReader.accept(remappingVisitor, ClassReader.EXPAND_FRAMES);
    } catch (Exception e) {
        throw new GradleException("Error in ASM processing class: " + className, e);
    }
    return classWriter.toByteArray();
}
Also used : GradleException(org.gradle.api.GradleException) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) UncheckedException(org.gradle.internal.UncheckedException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException)

Example 35 with ClassWriter

use of org.objectweb.asm.ClassWriter in project bazel by bazelbuild.

the class JavacTurbine method processBytecode.

/**
   * Remove code attributes and private members.
   *
   * <p>Most code will already have been removed after parsing, but the bytecode will still contain
   * e.g. lowered class and instance initializers.
   */
private static byte[] processBytecode(byte[] bytes) {
    ClassWriter cw = new ClassWriter(0);
    new ClassReader(bytes).accept(new PrivateMemberPruner(cw), ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG);
    return cw.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) 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