Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

Click Source Link

Usage

From source file:com.android.build.gradle.internal2.incremental.IncrementalVisitor.java

License:Apache License

@Nullable
public static File instrumentClass(@NonNull File inputRootDirectory, @NonNull File inputFile,
        @NonNull File outputDirectory, @NonNull VisitorBuilder visitorBuilder) throws IOException {

    byte[] classBytes;
    String path = FileUtils.relativePath(inputFile, inputRootDirectory);
    if (!inputFile.getPath().endsWith(SdkConstants.DOT_CLASS)) {
        File outputFile = new File(outputDirectory, path);
        Files.createParentDirs(outputFile);
        Files.copy(inputFile, outputFile);
        return outputFile;
    }/*from  ww w .j a v  a2  s .  c  o  m*/
    classBytes = Files.toByteArray(inputFile);
    ClassReader classReader = new ClassReader(classBytes);
    // override the getCommonSuperClass to use the thread context class loader instead of
    // the system classloader. This is useful as ASM needs to load classes from the project
    // which the system classloader does not have visibility upon.
    // TODO: investigate if there is not a simpler way than overriding.
    ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES) {
        @Override
        protected String getCommonSuperClass(final String type1, final String type2) {
            Class<?> c, d;
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            try {
                c = Class.forName(type1.replace('/', '.'), false, classLoader);
                d = Class.forName(type2.replace('/', '.'), false, classLoader);
            } catch (Exception e) {
                throw new RuntimeException(e.toString());
            }
            if (c.isAssignableFrom(d)) {
                return type1;
            }
            if (d.isAssignableFrom(c)) {
                return type2;
            }
            if (c.isInterface() || d.isInterface()) {
                return "java/lang/Object";
            } else {
                do {
                    c = c.getSuperclass();
                } while (!c.isAssignableFrom(d));
                return c.getName().replace('.', '/');
            }
        }
    };

    ClassNode classNode = new ClassNode();
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);

    // when dealing with interface, we just copy the inputFile over without any changes unless
    // this is a package private interface.
    AccessRight accessRight = AccessRight.fromNodeAccess(classNode.access);
    File outputFile = new File(outputDirectory, path);
    if ((classNode.access & Opcodes.ACC_INTERFACE) != 0) {
        if (visitorBuilder.getOutputType() == OutputType.INSTRUMENT) {
            // don't change the name of interfaces.
            Files.createParentDirs(outputFile);
            if (accessRight == AccessRight.PACKAGE_PRIVATE) {
                classNode.access = classNode.access | Opcodes.ACC_PUBLIC;
                classNode.accept(classWriter);
                Files.write(classWriter.toByteArray(), outputFile);
            } else {
                // just copy the input file over, no change.
                Files.write(classBytes, outputFile);
            }
            return outputFile;
        } else {
            return null;
        }
    }

    List<ClassNode> parentsNodes = parseParents(inputFile, classNode);

    // if we could not determine the parent hierarchy, disable instant run.
    if (parentsNodes.isEmpty() || isPackageInstantRunDisabled(inputFile, classNode)) {
        if (visitorBuilder.getOutputType() == OutputType.INSTRUMENT) {
            Files.createParentDirs(outputFile);
            Files.write(classBytes, outputFile);
            return outputFile;
        } else {
            return null;
        }
    }

    outputFile = new File(outputDirectory, visitorBuilder.getMangledRelativeClassFilePath(path));
    Files.createParentDirs(outputFile);
    IncrementalVisitor visitor = visitorBuilder.build(classNode, parentsNodes, classWriter);
    classNode.accept(visitor);

    Files.write(classWriter.toByteArray(), outputFile);
    return outputFile;
}

From source file:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

/**
 * Modifies a {@link ClassNode} to clear final flags and rewrite byte code.
 *///from ww w.j av a2s  .  com
@SuppressWarnings("unchecked")
private void modifyClass(ClassNode classNode) {
    // Make the class not final.
    classNode.access &= ~Opcodes.ACC_FINAL;

    List<MethodNode> methodNodes = classNode.methods;
    for (MethodNode methodNode : methodNodes) {
        methodNode.access &= ~Opcodes.ACC_FINAL;
        fixMethodBody(methodNode, classNode);
    }

    List<FieldNode> fieldNodes = classNode.fields;
    for (FieldNode fieldNode : fieldNodes) {
        // Make public instance fields non-final. This is needed e.g. to "mock" SyncResult.stats.
        if ((fieldNode.access & Opcodes.ACC_PUBLIC) != 0 && (fieldNode.access & Opcodes.ACC_STATIC) == 0) {
            fieldNode.access &= ~Opcodes.ACC_FINAL;
        }
    }

    List<InnerClassNode> innerClasses = classNode.innerClasses;
    for (InnerClassNode innerClassNode : innerClasses) {
        innerClassNode.access &= ~Opcodes.ACC_FINAL;
    }
}

From source file:com.android.mkstubs.FilterClassAdapter.java

License:Apache License

/**
 * Visits a field./*  w  ww  .  j a v  a  2 s  . c  o m*/
 *
 * {@inheritDoc}
 *
 * Examples:
 * name = mArg
 * desc = Ljava/Lang/String;
 * signature = null (not a template) or template type
 */
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    // only accept public/protected fields
    if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
        return null;
    }

    // filter on field name
    String filterName = String.format("%s#%s", mClassName, name);

    if (!mFilter.accept(filterName)) {
        mLog.debug("- Remove field " + filterName);
        return null;
    }

    // TODO we should produce an error if a filtered desc/signature is being used.

    return super.visitField(access, name, desc, signature, value);
}

From source file:com.android.mkstubs.FilterClassAdapter.java

License:Apache License

/**
 * Visits a method.//from  w w w  . jav  a2s  .c om
 *
 * {@inheritDoc}
 *
 * Examples:
 * name = <init>
 * desc = ()V
 * signature = null (not a template) or template type
 */
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {

    // only accept public/protected methods
    if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
        return null;
    }

    // filter on method name using the non-generic descriptor
    String filterName = String.format("%s#%s%s", mClassName, name, desc);

    if (!mFilter.accept(filterName)) {
        mLog.debug("- Remove method " + filterName);
        return null;
    }

    // filter on method name using the generic signature
    if (signature != null) {
        filterName = String.format("%s#%s%s", mClassName, name, signature);

        if (!mFilter.accept(filterName)) {
            mLog.debug("- Remove method " + filterName);
            return null;
        }
    }

    // TODO we should produce an error if a filtered desc/signature/exception is being used.

    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:com.android.mkstubs.FilterClassAdapter.java

License:Apache License

@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {

    // only accept public/protected inner classes
    if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
        return;/* w  w w  .  j  av  a2 s .  c o  m*/
    }

    // filter on name
    if (!mFilter.accept(name)) {
        return;
    }

    super.visitInnerClass(name, outerName, innerName, access);
}

From source file:com.android.mkstubs.sourcer.AccessSourcerTest.java

License:Apache License

@Test
public void testAbstractPublic() throws Exception {
    mSourcer.write(Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC, AccessSourcer.IS_CLASS);

    String s = mWriter.toString();
    Assert.assertEquals("public abstract", s);
}

From source file:com.android.mkstubs.sourcer.FieldSourcerTest.java

License:Apache License

@Test
public void testStringField() throws Exception {

    FieldSourcer fs = new FieldSourcer(new Output(mWriter), Opcodes.ACC_PUBLIC, // access
            "mArg", // name
            "Ljava/lang/String;", // desc
            null // signature
    );/*from w  ww . j ava 2 s  .  co m*/
    fs.visitEnd();

    String s = mWriter.toString();
    Assert.assertEquals("public java.lang.String mArg;\n", s);
}

From source file:com.android.mkstubs.sourcer.MethodSourcerTest.java

License:Apache License

@Test
public void testVoid() {
    MethodSourcer m = new MethodSourcer(mOutput, "foo", //classname
            Opcodes.ACC_PUBLIC, //access
            "testVoid", //name
            "()V", //desc
            null, //signature
            null); //exception
    m.visitEnd();/*w  w  w. ja  va  2 s .  com*/

    assertSourceEquals("public void testVoid() { }", mWriter.toString());
}

From source file:com.android.mkstubs.sourcer.MethodSourcerTest.java

License:Apache License

@Test
public void testVoidThrow() {
    MethodSourcer m = new MethodSourcer(mOutput, "foo", //classname
            Opcodes.ACC_PUBLIC, //access
            "testVoid", //name
            "()V", //desc
            null, //signature
            new String[] { "java/lang/Exception" }); //exception
    m.visitEnd();//from   w  ww  .ja v a2  s . c o m

    assertSourceEquals("public void testVoid() throws java.lang.Exception { }", mWriter.toString());
}

From source file:com.android.mkstubs.sourcer.MethodSourcerTest.java

License:Apache License

@Test
public void testReturnMap() {
    MethodSourcer m = new MethodSourcer(mOutput, "foo", //classname
            Opcodes.ACC_PUBLIC, //access
            "getMap_T_U", //name
            "()Ljava/util/Map;", //desc
            "()Ljava/util/Map<TT;TU;>;", //signature
            null); //exception
    m.visitEnd();//from  w w  w.  j a  v  a 2  s  .  c o m

    assertSourceEquals("public java.util.Map<T, U> getMap_T_U() { }", mWriter.toString());
}