Example usage for org.objectweb.asm Opcodes ACC_STATIC

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

Introduction

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

Prototype

int ACC_STATIC

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

Click Source Link

Usage

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

License:Apache License

/**
 * Adds serialVersionUID for the classes that does not define one. Reason for this is that if a
 * class does not define a serialVersionUID value, and is used for serialization, instrumented
 * and non-instrumented class will have different serialVersionUID values, and that will break
 * serialization./*ww  w.j a v  a2 s . c  om*/
 */
private void addSerialUidIfMissing() {
    // noinspection unchecked
    for (FieldNode f : (List<FieldNode>) classNode.fields) {
        if (f.name.equals("serialVersionUID")) {
            // We should not generate serial uuid, field already exists. Although it might
            // not be static, final, and long, adding would break the instrumented class.
            return;
        }
    }
    try {
        String className = Type.getObjectType(classNode.name).getClassName();
        Class<?> clazz = Class.forName(className, false, Thread.currentThread().getContextClassLoader());

        ObjectStreamClass objectStreamClass = ObjectStreamClass.lookupAny(clazz);
        long serialUuid = objectStreamClass.getSerialVersionUID();

        // adds the field
        super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "serialVersionUID",
                Type.LONG_TYPE.getDescriptor(), null, serialUuid);

    } catch (ClassNotFoundException ex) {
        LOG.verbose("Unable to add auto-generated serialVersionUID for %1$s : %2$s", classNode.name,
                ex.getMessage());
    } catch (LinkageError | AssertionError e) {
        // http://b.android.com/220635 - static initializer might be invoked
        LOG.warning("Unable to generate serialVersionUID for %s. In case you make this class"
                + " serializable and use it to persist data in InstantRun mode, please"
                + " add a serialVersionUID field.", classNode.name);
    }
}

From source file:com.android.build.gradle.shrinker.DependencyFinderVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    T method = mGraph.getMemberReference(mClassName, name, desc);

    if ((access & Opcodes.ACC_STATIC) == 0 && !name.equals(ByteCodeUtils.CONSTRUCTOR)) {
        handleVirtualMethod(method);//www.  ja v a 2  s  . c  om
    }

    // Keep class initializers, but also keep all default constructors. This is the ProGuard
    // behavior,
    if (name.equals(ByteCodeUtils.CLASS_INITIALIZER)
            || (name.equals(ByteCodeUtils.CONSTRUCTOR) && desc.equals("()V"))) {
        handleDependency(mKlass, method, DependencyType.REQUIRED_CLASS_STRUCTURE);
    }

    if (mIsAnnotation) {
        // TODO: Strip unused annotation classes members.
        handleDependency(mKlass, method, DependencyType.REQUIRED_CLASS_STRUCTURE);
    }

    if (signature != null) {
        SignatureReader reader = new SignatureReader(signature);
        SignatureVisitor visitor = new DependencyFinderSignatureVisitor();
        reader.accept(visitor);
    }

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

From source file:com.android.builder.shrinker.DependencyFinderVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    T method = mGraph.getMemberReference(mClassName, name, desc);

    if ((access & Opcodes.ACC_STATIC) == 0 && !name.equals(AsmUtils.CONSTRUCTOR) && mVirtualMethods != null) {
        mVirtualMethods.add(method);/*  ww  w .  jav  a 2s .  c o m*/
    }

    Type methodType = Type.getMethodType(desc);
    handleDeclarationType(method, methodType.getReturnType());
    for (Type argType : methodType.getArgumentTypes()) {
        handleDeclarationType(method, argType);
    }

    if (name.equals(AsmUtils.CLASS_INITIALIZER)) {
        handleDependency(mKlass, method, DependencyType.REQUIRED);
    }

    if (mIsAnnotation) {
        // TODO: Strip annotation members.
        handleDependency(mKlass, method, DependencyType.REQUIRED);
    }

    if (signature != null) {
        handleClassSignature(method, signature);
    }

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

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

License:Apache License

/**
 * Modifies a {@link ClassNode} to clear final flags and rewrite byte code.
 *///w ww  .  j  a v  a 2 s  . co m
@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.ide.eclipse.apt.internal.analysis.InternalGetSetAnalyzer.java

License:Apache License

/**
 * Checks if a method is a getter/*from w  w  w  .j a v  a  2 s .  co m*/
 * @param methodTest The method to test
 * @return True if the method is a getter, false otherwise
 */
private boolean isGetter(final MethodNode methodTest) {
    boolean getter = false;
    final String desc = methodTest.desc;
    final Type[] arguments = Type.getArgumentTypes(desc);
    final Type returnType = Type.getReturnType(desc);
    if (arguments.length == 0 && returnType.getSort() != Type.VOID) {
        final InsnList instructions = methodTest.instructions;
        //three next to skip label and line number instructions
        final AbstractInsnNode first = instructions.getFirst().getNext().getNext();
        final int returnOp = returnType.getOpcode(Opcodes.IRETURN);
        final int firstOp = first.getOpcode();
        //check for static getter
        if ((Opcodes.ACC_STATIC & methodTest.access) == 0) {
            if (firstOp == Opcodes.ALOAD) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == Opcodes.GETFIELD) {
                    final AbstractInsnNode third = second.getNext();
                    if (third.getOpcode() == returnOp) {
                        getter = true;
                    }
                }
            }
        } else {
            if (firstOp == Opcodes.GETSTATIC) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == returnOp) {
                    getter = true;
                }
            }
        }
    }
    return getter;
}

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

License:Apache License

/**
 * Checks if a method is a setter//  ww w.  j  a v  a  2 s  .com
 * @param methodTest The method to be checked
 * @return True if the method is a setter, false otherwise
 */
private boolean isSetter(final MethodNode methodTest) {
    boolean setter = false;
    final String desc = methodTest.desc;
    final Type[] arguments = Type.getArgumentTypes(desc);
    final Type returnType = Type.getReturnType(desc);
    if (arguments.length == 1 && returnType.getSort() == Type.VOID) {
        final InsnList instructions = methodTest.instructions;
        //skip label and line number instructions
        final AbstractInsnNode first = instructions.getFirst().getNext().getNext();
        final int loadOp = arguments[0].getOpcode(Opcodes.ILOAD);
        final int firstOp = first.getOpcode();
        //check for static setter
        if ((Opcodes.ACC_STATIC & methodTest.access) == 0) {
            if (firstOp == Opcodes.ALOAD) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == loadOp) {
                    final AbstractInsnNode third = second.getNext();
                    if (third.getOpcode() == Opcodes.PUTFIELD) {
                        //three next to skip label and line number instructions
                        final AbstractInsnNode fourth = third.getNext().getNext().getNext();
                        if (fourth.getOpcode() == Opcodes.RETURN) {
                            setter = true;
                        }
                    }
                }
            }
        } else {
            if (firstOp == loadOp) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == Opcodes.PUTSTATIC) {
                    final AbstractInsnNode third = second.getNext().getNext().getNext();
                    if (third.getOpcode() == Opcodes.RETURN) {
                        setter = true;
                    }
                }
            }
        }
    }
    return setter;
}

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

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(final MethodNode methodNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    final int acc = methodNode.access;
    if ((acc & Opcodes.ACC_STATIC) == 0 && (acc & Opcodes.ACC_ABSTRACT) == 0) {
        final InsnList instructions = methodNode.instructions;
        //find if a method load this (ALOAD 0)
        boolean aload0 = false;
        for (int i = 0; i < instructions.size(); i++) {
            final AbstractInsnNode instruction = instructions.get(i);
            if (instruction.getOpcode() == Opcodes.ALOAD) {
                final VarInsnNode aload = (VarInsnNode) instruction;
                aload0 = aload.var == 0;
                if (aload0) {
                    break;
                }//from   w  ww .j a  v a 2s . c  o m
            }
        }
        if (!aload0) {
            final Problem problem = new StaticVirtualProblem(instructions.get(1));
            problems.add(problem);
        }
    }
    return problems;
}

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

License:Apache License

@Test
public void testPrivateFinalStatic() throws Exception {
    mSourcer.write(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, AccessSourcer.IS_METHOD);

    String s = mWriter.toString();
    Assert.assertEquals("private static final", s);
}

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);
    }/*ww  w .  j  a  va  2 s .  co  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.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;
                }//  w w  w .  j a v a  2  s  .  c om
            }
        }
    }

    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);
    }
}