Example usage for org.objectweb.asm Opcodes ACC_INTERFACE

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

Introduction

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

Prototype

int ACC_INTERFACE

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

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 om*/
    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.build.gradle.shrinker.FullRunShrinker.java

License:Apache License

private void handleInterfaceInheritance(T klass) {
    TreeTraverser<T> interfaceTraverser = TypeHierarchyTraverser.interfaces(mGraph, mShrinkerLogger);

    if ((mGraph.getModifiers(klass) & Opcodes.ACC_INTERFACE) != 0) {

        // The "children" name is unfortunate: in the type hierarchy tree traverser,
        // these are the interfaces that klass (which is an interface itself)
        // extends (directly).
        Iterable<T> superinterfaces = interfaceTraverser.children(klass);

        for (T superinterface : superinterfaces) {
            if (mGraph.isProgramClass(superinterface)) {
                // Add the arrow going "down", from the superinterface to this one.
                mGraph.addDependency(superinterface, klass, DependencyType.SUPERINTERFACE_KEPT);
            } else {
                // The superinterface is part of the SDK, so it's always kept. As
                // long as there's any class that implements this interface, it
                // needs to be kept.
                mGraph.addRoots(ImmutableMap.of(klass, DependencyType.SUPERINTERFACE_KEPT), CounterSet.SHRINK);
            }//from   w  w  w . j a v  a  2  s.  co m
        }
    }

    Iterable<T> implementedInterfaces =
            // Skip the class itself.
            interfaceTraverser.preOrderTraversal(klass).skip(1);

    for (T iface : implementedInterfaces) {
        if (mGraph.isProgramClass(iface)) {
            mGraph.addDependency(klass, iface, DependencyType.INTERFACE_IMPLEMENTED);
        }
    }
}

From source file:com.android.ide.eclipse.apt.internal.analysis.StaticVirtualAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeClass(final ClassNode classNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    if ((classNode.access & Opcodes.ACC_INTERFACE) == 0) {
        problems.addAll(super.analyzeClass(classNode));
    }/*ww  w.  ja v  a  2 s  .  c o m*/
    return problems;
}

From source file:com.android.tools.layoutlib.create.TransformClassAdapter.java

License:Apache License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {/*from   w  w  w .  ja  v a2 s.  com*/

    // This class might be being renamed.
    name = mClassName;

    // remove protected or private and set as public
    access = access & ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED);
    access |= Opcodes.ACC_PUBLIC;
    // remove final
    access = access & ~Opcodes.ACC_FINAL;
    // note: leave abstract classes as such
    // don't try to implement stub for interfaces

    mIsInterface = ((access & Opcodes.ACC_INTERFACE) != 0);
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:com.codename1.tools.translator.Parser.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {//from   w ww  . ja  v a 2s .  c o m
    cls.setBaseClass(superName);
    cls.setBaseInterfaces(interfaces);
    if ((access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT) {
        cls.setIsAbstract(true);
    }
    if ((access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE) {
        cls.setIsInterface(true);
    }
    if ((access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL) {
        cls.setFinalClass(true);
    }
    if ("com/codename1/testing/UnitTest".equals(superName)
            || "com/codename1/testing/AbstractTest".equals(superName)) {
        cls.setIsUnitTest(true);
    }
    if ((access & Opcodes.ACC_ENUM) == Opcodes.ACC_ENUM) {
        cls.setIsEnum(true);
    }
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:com.develorium.metracer.asm.MetracerClassWriter.java

License:Apache License

protected String getCommonSuperClass(String theType1, String theType2) {
    try {/*from   w  w w.  ja va2s  . co  m*/
        ClassReader info1 = typeInfo(theType1);
        ClassReader info2 = typeInfo(theType2);

        if ((info1.getAccess() & Opcodes.ACC_INTERFACE) != 0) {
            if (typeImplements(theType2, info2, theType1)) {
                return theType1;
            }
            if ((info2.getAccess() & Opcodes.ACC_INTERFACE) != 0) {
                if (typeImplements(theType1, info1, theType2)) {
                    return theType2;
                }
            }
            return "java/lang/Object";
        }

        if ((info2.getAccess() & Opcodes.ACC_INTERFACE) != 0) {
            if (typeImplements(theType1, info1, theType2)) {
                return theType2;
            } else {
                return "java/lang/Object";
            }
        }

        StringBuilder b1 = typeAncestors(theType1, info1);
        StringBuilder b2 = typeAncestors(theType2, info2);
        String result = "java/lang/Object";
        int end1 = b1.length();
        int end2 = b2.length();

        while (true) {
            int start1 = b1.lastIndexOf(";", end1 - 1);
            int start2 = b2.lastIndexOf(";", end2 - 1);
            if (start1 != -1 && start2 != -1 && end1 - start1 == end2 - start2) {
                String p1 = b1.substring(start1 + 1, end1);
                String p2 = b2.substring(start2 + 1, end2);
                if (p1.equals(p2)) {
                    result = p1;
                    end1 = start1;
                    end2 = start2;
                } else {
                    return result;
                }
            } else {
                return result;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:com.devexperts.aprof.transformer.ClassInfo.java

License:Open Source License

boolean isInterface() {
    return (access & Opcodes.ACC_INTERFACE) != 0;
}

From source file:com.facebook.buck.jvm.java.abi.AbiFilteringClassVisitor.java

License:Apache License

private boolean isInterface(int access) {
    return (access & Opcodes.ACC_INTERFACE) > 0;
}

From source file:com.facebook.buck.jvm.java.abi.AccessFlags.java

License:Apache License

/**
 * Gets the class access flags (see JVMS8 4.1) for the given type element, augmented by the
 * special ASM pseudo-access flag for @Deprecated types.
 *///from   w  ww . j a v a2s  .  com
public int getAccessFlags(TypeElement typeElement) {
    int result = getCommonAccessFlags(typeElement);

    switch (typeElement.getKind()) {
    case ANNOTATION_TYPE:
        // No ACC_SUPER here per JVMS 4.1
        result = result | Opcodes.ACC_ANNOTATION;
        result = result | Opcodes.ACC_INTERFACE;
        result = result | Opcodes.ACC_ABSTRACT;
        break;
    case ENUM:
        result = result | Opcodes.ACC_SUPER; // JVMS 4.1
        result = result | Opcodes.ACC_ENUM;

        // Enums have this lovely property that you can't declare them abstract in source, even
        // if they have abstract methods or incompletely implemented interfaces, yet the class
        // file will have ACC_ABSTRACT in that case. Because it's a pain to figure out if an
        // enum is abstract (and impossible in the no-deps case), and you can't instantiate or
        // subclass one directly anyway, we just leave the flag off.
        break;
    case INTERFACE:
        // No ACC_SUPER here per JVMS 4.1
        result = result | Opcodes.ACC_ABSTRACT;
        result = result | Opcodes.ACC_INTERFACE;
        break;
    // $CASES-OMITTED$
    default:
        result = result | Opcodes.ACC_SUPER; // JVMS 4.1
        break;
    }

    return result;
}

From source file:com.facebook.buck.jvm.java.abi.AccessFlagsTest.java

License:Apache License

@Test
public void testAnnotationTypeFlags() throws IOException {
    testTypeFlags("@java.lang.annotation.Documented @interface Foo { }", "Foo",
            Opcodes.ACC_ANNOTATION | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT);
}