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.tools.layoutlib.create.DelegateClassAdapter.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {

    boolean isStatic = (access & Opcodes.ACC_STATIC) != 0;
    boolean isNative = (access & Opcodes.ACC_NATIVE) != 0;

    boolean useDelegate = (isNative && mDelegateMethods.contains(ALL_NATIVES))
            || mDelegateMethods.contains(name);

    if (!useDelegate) {
        // Not creating a delegate for this method, pass it as-is from the reader
        // to the writer.
        return super.visitMethod(access, name, desc, signature, exceptions);
    }/*w w  w. j  ava2 s.  c o  m*/

    if (useDelegate) {
        if (CONSTRUCTOR.equals(name) || CLASS_INIT.equals(name)) {
            // We don't currently support generating delegates for constructors.
            throw new UnsupportedOperationException(
                    String.format("Delegate doesn't support overriding constructor %1$s:%2$s(%3$s)", //$NON-NLS-1$
                            mClassName, name, desc));
        }
    }

    if (isNative) {
        // Remove native flag
        access = access & ~Opcodes.ACC_NATIVE;
        MethodVisitor mwDelegate = super.visitMethod(access, name, desc, signature, exceptions);

        DelegateMethodAdapter2 a = new DelegateMethodAdapter2(mLog, null /*mwOriginal*/, mwDelegate, mClassName,
                name, desc, isStatic);

        // A native has no code to visit, so we need to generate it directly.
        a.generateDelegateCode();

        return mwDelegate;
    }

    // Given a non-native SomeClass.MethodName(), we want to generate 2 methods:
    // - A copy of the original method named SomeClass.MethodName_Original().
    //   The content is the original method as-is from the reader.
    // - A brand new implementation of SomeClass.MethodName() which calls to a
    //   non-existing method named SomeClass_Delegate.MethodName().
    //   The implementation of this 'delegate' method is done in layoutlib_brigde.

    int accessDelegate = access;
    // change access to public for the original one
    if (Main.sOptions.generatePublicAccess) {
        access &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
        access |= Opcodes.ACC_PUBLIC;
    }

    MethodVisitor mwOriginal = super.visitMethod(access, name + ORIGINAL_SUFFIX, desc, signature, exceptions);
    MethodVisitor mwDelegate = super.visitMethod(accessDelegate, name, desc, signature, exceptions);

    DelegateMethodAdapter2 a = new DelegateMethodAdapter2(mLog, mwOriginal, mwDelegate, mClassName, name, desc,
            isStatic);
    return a;
}

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

License:Apache License

@Override
public void visitEnd() {
    super.visitField(Opcodes.ACC_PUBLIC, "mLayoutlibCallback",
            "Lcom/android/ide/common/rendering/api/LayoutlibCallback;", null, null);
    super.visitField(Opcodes.ACC_PUBLIC, "mContext", "Lcom/android/layoutlib/bridge/android/BridgeContext;",
            null, null);//  w w w  . j a va  2  s  .  co m
    super.visitEnd();
}

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 ww.ja  va 2  s . c  o  m*/

    // 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.android.tools.layoutlib.create.TransformClassAdapter.java

License:Apache License

@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
    // 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

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

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

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {

    if (mDeleteReturns != null) {
        Type t = Type.getReturnType(desc);
        if (t.getSort() == Type.OBJECT) {
            String returnType = t.getInternalName();
            if (returnType != null) {
                if (mDeleteReturns.contains(returnType)) {
                    return null;
                }/*from w  w w  .j a v a 2s. co  m*/
            }
        }
    }

    String methodSignature = mClassName.replace('/', '.') + "#" + name;

    // change access to public
    access &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
    access |= Opcodes.ACC_PUBLIC;

    // remove final
    access = access & ~Opcodes.ACC_FINAL;

    // stub this method if they are all to be stubbed or if it is a native method
    // and don't try to stub interfaces nor abstract non-native methods.
    if (!mIsInterface && ((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != Opcodes.ACC_ABSTRACT)
            && (mStubAll || (access & Opcodes.ACC_NATIVE) != 0) || mStubMethods.contains(methodSignature)) {

        boolean isStatic = (access & Opcodes.ACC_STATIC) != 0;
        boolean isNative = (access & Opcodes.ACC_NATIVE) != 0;

        // remove abstract, final and native
        access = access & ~(Opcodes.ACC_ABSTRACT | Opcodes.ACC_FINAL | Opcodes.ACC_NATIVE);

        String invokeSignature = methodSignature + desc;
        mLog.debug("  Stub: %s (%s)", invokeSignature, isNative ? "native" : "");

        MethodVisitor mw = super.visitMethod(access, name, desc, signature, exceptions);
        return new StubMethodAdapter(mw, name, returnType(desc), invokeSignature, isStatic, isNative);

    } else {
        mLog.debug("  Keep: %s %s", name, desc);
        return super.visitMethod(access, name, desc, signature, exceptions);
    }
}

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

License:Apache License

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    // change access to public
    access &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
    access |= Opcodes.ACC_PUBLIC;

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

From source file:com.android.tools.lint.checks.FragmentDetector.java

License:Apache License

@Override
public void checkClass(@NonNull ClassContext context, @NonNull ClassNode classNode) {
    if ((classNode.access & Opcodes.ACC_ABSTRACT) != 0) {
        // Ignore abstract classes since they are clearly (and by definition) not intended to
        // be instantiated. We're looking for accidental non-static or missing constructor
        // scenarios here.
        return;//w  w w.j a va2 s .c o m
    }

    LintDriver driver = context.getDriver();

    if (!(driver.isSubclassOf(classNode, FRAGMENT) || driver.isSubclassOf(classNode, FRAGMENT_V4))) {
        return;
    }

    if ((classNode.access & Opcodes.ACC_PUBLIC) == 0) {
        context.report(ISSUE, context.getLocation(classNode),
                String.format("This fragment class should be public (%1$s)",
                        ClassContext.createSignature(classNode.name, null, null)),
                null);
        return;
    }

    if (classNode.name.indexOf('$') != -1 && !LintUtils.isStaticInnerClass(classNode)) {
        context.report(ISSUE, context.getLocation(classNode),
                String.format("This fragment inner class should be static (%1$s)",
                        ClassContext.createSignature(classNode.name, null, null)),
                null);
        return;
    }

    boolean hasDefaultConstructor = false;
    @SuppressWarnings("rawtypes") // ASM API
    List methodList = classNode.methods;
    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;
        if (method.name.equals(CONSTRUCTOR_NAME)) {
            if (method.desc.equals("()V")) { //$NON-NLS-1$
                // The constructor must be public
                if ((method.access & Opcodes.ACC_PUBLIC) != 0) {
                    hasDefaultConstructor = true;
                } else {
                    context.report(ISSUE, context.getLocation(method, classNode),
                            "The default constructor must be public", null);
                    // Also mark that we have a constructor so we don't complain again
                    // below since we've already emitted a more specific error related
                    // to the default constructor
                    hasDefaultConstructor = true;
                }
            } else if (!method.desc.contains("()")) { //$NON-NLS-1$
                context.report(ISSUE, context.getLocation(method, classNode),
                        // TODO: Use separate issue for this which isn't an error
                        "Avoid non-default constructors in fragments: use a default constructor "
                                + "plus Fragment#setArguments(Bundle) instead",
                        null);
            }
        }
    }

    if (!hasDefaultConstructor) {
        context.report(ISSUE, context.getLocation(classNode),
                String.format(
                        "This fragment should provide a default constructor (a public "
                                + "constructor with no arguments) (%1$s)",
                        ClassContext.createSignature(classNode.name, null, null)),
                null);
    }
}

From source file:com.android.tools.lint.checks.MissingClassDetector.java

License:Apache License

@Override
public void checkClass(@NonNull ClassContext context, @NonNull ClassNode classNode) {
    if (!mHaveClasses && !context.isFromClassLibrary() && context.getProject() == context.getMainProject()) {
        mHaveClasses = true;//from ww w  .  ja v a2s.  co m
    }
    String curr = classNode.name;
    if (mReferencedClasses != null && mReferencedClasses.containsKey(curr)) {
        boolean isCustomView = mCustomViews.contains(curr);
        mReferencedClasses.remove(curr);

        // Ensure that the class is public, non static and has a null constructor!

        if ((classNode.access & Opcodes.ACC_PUBLIC) == 0) {
            context.report(INSTANTIATABLE, context.getLocation(classNode),
                    String.format("This class should be public (%1$s)",
                            ClassContext.createSignature(classNode.name, null, null)),
                    null);
            return;
        }

        if (classNode.name.indexOf('$') != -1 && !LintUtils.isStaticInnerClass(classNode)) {
            context.report(INSTANTIATABLE, context.getLocation(classNode),
                    String.format("This inner class should be static (%1$s)",
                            ClassContext.createSignature(classNode.name, null, null)),
                    null);
            return;
        }

        boolean hasDefaultConstructor = false;
        @SuppressWarnings("rawtypes") // ASM API
        List methodList = classNode.methods;
        for (Object m : methodList) {
            MethodNode method = (MethodNode) m;
            if (method.name.equals(CONSTRUCTOR_NAME)) {
                if (method.desc.equals("()V")) { //$NON-NLS-1$
                    // The constructor must be public
                    if ((method.access & Opcodes.ACC_PUBLIC) != 0) {
                        hasDefaultConstructor = true;
                    } else {
                        context.report(INSTANTIATABLE, context.getLocation(method, classNode),
                                "The default constructor must be public", null);
                        // Also mark that we have a constructor so we don't complain again
                        // below since we've already emitted a more specific error related
                        // to the default constructor
                        hasDefaultConstructor = true;
                    }
                }
            }
        }

        if (!hasDefaultConstructor && !isCustomView && !context.isFromClassLibrary()
                && context.getProject().getReportIssues()) {
            context.report(INSTANTIATABLE, context.getLocation(classNode),
                    String.format(
                            "This class should provide a default constructor (a public "
                                    + "constructor with no arguments) (%1$s)",
                            ClassContext.createSignature(classNode.name, null, null)),
                    null);
        }
    }
}

From source file:com.android.tools.lint.checks.OnClickDetector.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*from   ww  w .j  a  v a  2  s. c o m*/
public void checkClass(@NonNull ClassContext context, @NonNull ClassNode classNode) {
    if (mNames == null) {
        // No onClick attributes in the XML files
        return;
    }

    mHaveBytecode = true;

    List methodList = classNode.methods;
    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;
        boolean rightArguments = method.desc.equals("(Landroid/view/View;)V"); //$NON-NLS-1$
        if (!mNames.containsKey(method.name)) {
            if (rightArguments) {
                // See if there's a possible typo instead
                for (String n : mNames.keySet()) {
                    if (LintUtils.editDistance(n, method.name) <= 2) {
                        recordSimilar(n, classNode, method);
                        break;
                    }
                }
            }
            continue;
        }

        // TODO: Validate class hierarchy: should extend a context method
        // Longer term, also validate that it's in a layout that corresponds to
        // the given activity

        if (rightArguments) {
            // Found: remove from list to be checked
            mNames.remove(method.name);

            // Make sure the method is public
            if ((method.access & Opcodes.ACC_PUBLIC) == 0) {
                Location location = context.getLocation(method, classNode);
                String message = String.format("On click handler %1$s(View) must be public", method.name);
                context.report(ISSUE, location, message, null);
            } else if ((method.access & Opcodes.ACC_STATIC) != 0) {
                Location location = context.getLocation(method, classNode);
                String message = String.format("On click handler %1$s(View) should not be static", method.name);
                context.report(ISSUE, location, message, null);
            }

            if (mNames.isEmpty()) {
                mNames = null;
                return;
            }
        }
    }
}

From source file:com.appfour.codestripper.Main.java

License:Open Source License

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Usage: java " + Main.class.getCanonicalName() + " <source dir> <dest file>");
        System.exit(1);//from   w w  w.jav  a2  s. c  om
    }

    File outFile = new File(args[1]);
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFile));
    zipOut.setLevel(9);
    Set<String> writtenEntries = new HashSet<String>();
    for (File file : new File(args[0]).listFiles()) {
        if (file.getName().toLowerCase(Locale.US).endsWith(".jar")) {
            System.err.println("Processing JAR: " + file.getPath());
            ZipInputStream zipIn = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry;
            while ((entry = zipIn.getNextEntry()) != null) {
                if (entry.isDirectory())
                    continue;
                if (entry.getName().toLowerCase(Locale.US).endsWith(".class")) {
                    if (writtenEntries.contains(entry.getName())) {
                        System.out.println("\tIgnoring duplicate CLASS: " + entry.getName());
                        continue;
                    }
                    writtenEntries.add(entry.getName());
                    System.out.println("\tProcessing CLASS: " + entry.getName());
                    ClassReader cr = new ClassReader(zipIn);
                    ClassWriter cw = new ClassWriter(0);
                    final boolean[] skip = new boolean[1];
                    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
                        @Override
                        public void visit(int version, int access, String name, String signature,
                                String superName, String[] interfaces) {
                            if (!INCLUDE_PACKAGE_PRIVATE_CLASSES
                                    && (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
                                skip[0] = true;
                            }
                            super.visit(version, access, name, signature, superName, interfaces);
                        }

                        @Override
                        public void visitSource(String source, String debug) {
                            // ignore
                        }

                        @Override
                        public FieldVisitor visitField(int access, String name, String desc, String signature,
                                Object value) {
                            if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0)
                                return null;
                            return super.visitField(access, name, desc, signature, value);
                        }

                        @Override
                        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                                String[] exceptions) {
                            if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0)
                                return null;
                            return new MethodVisitor(Opcodes.ASM5,
                                    super.visitMethod(access, name, desc, signature, exceptions)) {
                                @Override
                                public void visitCode() {
                                    // ignore
                                    super.visitCode();
                                    super.visitInsn(Opcodes.NOP);
                                }

                                @Override
                                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                                    // ignore
                                }

                                @Override
                                public void visitIincInsn(int var, int increment) {
                                    // ignore
                                }

                                @Override
                                public void visitFrame(int type, int nLocal, Object[] local, int nStack,
                                        Object[] stack) {
                                    // ignore
                                }

                                @Override
                                public void visitInsn(int opcode) {
                                    // ignore
                                }

                                @Override
                                public void visitJumpInsn(int opcode, Label label) {
                                    // ignore
                                }

                                @Override
                                public void visitLabel(Label label) {
                                    // ignore
                                }

                                @Override
                                public void visitLdcInsn(Object cst) {
                                    // ignore
                                }

                                @Override
                                public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
                                    // ignore
                                }

                                @Override
                                public void visitIntInsn(int opcode, int operand) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath,
                                        String desc, boolean visible) {
                                    // ignore
                                    return null;
                                }

                                @Override
                                public void visitInvokeDynamicInsn(String name, String desc, Handle bsm,
                                        Object... bsmArgs) {
                                    // ignore
                                }

                                @Override
                                public void visitMethodInsn(int opcode, String owner, String name,
                                        String desc) {
                                    // ignore
                                }

                                @Override
                                public void visitMultiANewArrayInsn(String desc, int dims) {
                                    // ignore
                                }

                                @Override
                                public void visitTableSwitchInsn(int min, int max, Label dflt,
                                        Label... labels) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath,
                                        String desc, boolean visible) {
                                    // ignore
                                    return null;
                                };

                                @Override
                                public void visitTryCatchBlock(Label start, Label end, Label handler,
                                        String type) {
                                    // ignore
                                }

                                @Override
                                public void visitLineNumber(int line, Label start) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitLocalVariableAnnotation(int typeRef,
                                        TypePath typePath, Label[] start, Label[] end, int[] index, String desc,
                                        boolean visible) {
                                    // ignore
                                    return null;
                                }

                                @Override
                                public void visitParameter(String name, int access) {
                                    // ignore
                                }

                                @Override
                                public AnnotationVisitor visitParameterAnnotation(int parameter, String desc,
                                        boolean visible) {
                                    // ignore
                                    return null;
                                }

                                @Override
                                public void visitTypeInsn(int opcode, String type) {
                                    // ignore
                                }

                                @Override
                                public void visitVarInsn(int opcode, int var) {
                                    // ignore
                                }

                            };
                        }

                        @Override
                        public void visitAttribute(Attribute attr) {
                            System.out.println("\t\tProcessing attr " + attr.type);
                            if (attr.isCodeAttribute())
                                return;

                            super.visitAttribute(attr);
                        }
                    };
                    cr.accept(cv, 0);
                    if (!skip[0]) {
                        byte[] b2 = cw.toByteArray(); // b2 represents the same class as b1
                        entry.setSize(b2.length);
                        entry.setCompressedSize(-1);
                        entry.setMethod(ZipEntry.DEFLATED);
                        zipOut.putNextEntry(entry);
                        new DataOutputStream(zipOut).write(b2);
                    }
                } else {
                    if (writtenEntries.contains(entry.getName())) {
                        System.out.println("\tIgnoring duplicate RESOURCE: " + entry.getName());
                        continue;
                    }
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    transfer(zipIn, bos, false);
                    writtenEntries.add(entry.getName());
                    System.out.println("\tProcessing RESOURCE: " + entry.getName());
                    entry.setSize(bos.size());
                    entry.setCompressedSize(-1);
                    entry.setMethod(ZipEntry.DEFLATED);
                    zipOut.putNextEntry(entry);
                    transfer(new ByteArrayInputStream(bos.toByteArray()), zipOut, false);
                }
            }
            zipIn.close();
        }
    }
    zipOut.close();
}