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.tools.lint.checks.CordovaVersionDetector.java

License:Apache License

@Override
public void checkClass(@NonNull ClassContext context, @NonNull ClassNode classNode) {
    //noinspection VariableNotUsedInsideIf
    if (mCordovaVersion != null) {
        // Exit early if we have already found the cordova version in the JS file.
        // This will be the case for all versions > 3.x.x
        return;/*from   www . jav a  2  s .c  o m*/
    }

    // For cordova versions such as 2.7.1, the version is a static *non-final* field in
    // a class named Device. Since it is non-final, it is initialized in the <clinit> method.
    // Example:
    //
    // ldc           #5                  // String 2.7.1
    // putstatic     #6                  // Field cordovaVersion:Ljava/lang/String;
    // ...
    if (classNode.name.equals(FQN_CORDOVA_DEVICE)) {
        //noinspection unchecked ASM api.
        List<MethodNode> methods = classNode.methods;
        for (MethodNode method : methods) {
            if (SdkConstants.CLASS_CONSTRUCTOR.equals(method.name)) {
                InsnList nodes = method.instructions;
                for (int i = 0, n = nodes.size(); i < n; i++) {
                    AbstractInsnNode instruction = nodes.get(i);
                    int type = instruction.getType();
                    if (type == AbstractInsnNode.FIELD_INSN) {
                        checkInstructionInternal(context, classNode, instruction);
                        break;
                    }
                }
            }
        }
    } else if (classNode.name.equals(FQN_CORDOVA_WEBVIEW)) {
        // For versions > 3.x.x, the version string is stored as a static final String in
        // CordovaWebView.
        // Note that this is also stored in the cordova.js.* but from a lint api perspective,
        // it's much faster to look it up here than load and check in the JS file.
        // e.g.
        //   public static final java.lang.String CORDOVA_VERSION;
        //      descriptor: Ljava/lang/String;
        //      flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
        //      ConstantValue: String 4.1.1
        //
        //noinspection unchecked ASM api.
        List<FieldNode> fields = classNode.fields;
        for (FieldNode node : fields) {
            if (FIELD_NAME_CORDOVA_VERSION_WEBVIEW.equals(node.name)
                    && (node.access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL // is final
                    && (node.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC // is static
                    && node.value instanceof String) {
                mCordovaVersion = createVersion((String) node.value);
                if (mCordovaVersion != null) {
                    validateCordovaVersion(context, mCordovaVersion, context.getLocation(classNode));
                }
            }
        }
    }
}

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

License:Apache License

@Override
public void checkInstruction(@NonNull ClassContext context, @NonNull ClassNode classNode,
        @NonNull MethodNode method, @NonNull AbstractInsnNode instruction) {
    // As of Gingerbread/API 9, Dalvik performs this optimization automatically
    if (context.getProject().getMinSdk() >= 9) {
        return;/*from w  ww.j av  a  2s  .  co  m*/
    }

    if ((method.access & Opcodes.ACC_STATIC) != 0) {
        // Not an instance method
        return;
    }

    if (instruction.getOpcode() != Opcodes.INVOKEVIRTUAL) {
        return;
    }

    MethodInsnNode node = (MethodInsnNode) instruction;
    String name = node.name;
    String owner = node.owner;

    AbstractInsnNode prev = LintUtils.getPrevInstruction(instruction);
    if (prev == null || prev.getOpcode() != Opcodes.ALOAD) {
        return;
    }
    VarInsnNode prevVar = (VarInsnNode) prev;
    if (prevVar.var != 0) { // Not on "this", variable 0 in instance methods?
        return;
    }

    if (((name.startsWith("get") && name.length() > 3 //$NON-NLS-1$
            && Character.isUpperCase(name.charAt(3)))
            || (name.startsWith("is") && name.length() > 2 //$NON-NLS-1$
                    && Character.isUpperCase(name.charAt(2))))
            && owner.equals(classNode.name)) {
        // Calling a potential getter method on self. We now need to
        // investigate the method body of the getter call and make sure
        // it's really a plain getter, not just a method which happens
        // to have a method name like a getter, or a method which not
        // only returns a field but possibly computes it or performs
        // other initialization or side effects. This is done in a
        // second pass over the bytecode, initiated by the finish()
        // method.
        if (mPendingCalls == null) {
            mPendingCalls = new ArrayList<Entry>();
        }

        mPendingCalls.add(new Entry(name, node, method));
    }

    super.checkInstruction(context, classNode, method, instruction);
}

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

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*  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.android.tools.lint.checks.ViewConstructorDetector.java

License:Apache License

@Override
public void checkClass(@NonNull ClassContext context, @NonNull ClassNode classNode) {
    if (classNode.name.indexOf('$') != -1 && (classNode.access & Opcodes.ACC_STATIC) == 0) {
        // Ignore inner classes that aren't static: we can't create these
        // anyway since we'd need the outer instance
        return;/*from   w w w  .java  2s . c  o  m*/
    }

    // Ignore abstract classes
    if ((classNode.access & Opcodes.ACC_ABSTRACT) != 0) {
        return;
    }

    if (isViewClass(context, classNode)) {
        checkConstructors(context, classNode);
    }
}

From source file:com.asakusafw.dag.compiler.builtin.FoldOperatorGenerator.java

License:Apache License

private ClassData generateClass(Context context, UserOperator operator, ClassDescription target) {
    ClassDescription combinerClass = generateCombinerClass(context, operator, target);
    ClassWriter writer = newWriter(target, CombineResult.class);
    writer.visitInnerClass(combinerClass.getInternalName(), target.getInternalName(),
            combinerClass.getSimpleName(), Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);

    OperatorInput input = operator.getInput(Fold.ID_INPUT);
    defineDependenciesConstructor(context, operator.getOutputs(), target, writer, method -> {
        method.visitVarInsn(Opcodes.ALOAD, 0);
        getNew(method, combinerClass);// ww w. j a va 2  s .  c  o m
        getNew(method, input.getDataType());
        method.visitVarInsn(Opcodes.ALOAD, 1);
        method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(CombineResult.class).getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ObjectCombiner.class), typeOf(DataModel.class),
                        typeOf(Result.class)),
                false);
    }, Lang.discard());
    writer.visitEnd();

    return new ClassData(target, writer::toByteArray);
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

private static ClassData generateClass(Context context, UserOperator operator, ClassDescription target) {
    ClassDescription mapperClass = generateMapperClass(context, operator, target);
    ClassDescription combinerClass = generateCombinerClass(context, operator, target);

    ClassWriter writer = newWriter(target, CombineResult.class);
    writer.visitInnerClass(mapperClass.getInternalName(), target.getInternalName(), mapperClass.getSimpleName(),
            Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
    writer.visitInnerClass(combinerClass.getInternalName(), target.getInternalName(),
            combinerClass.getSimpleName(), Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);

    OperatorOutput output = operator.getOutput(Summarize.ID_OUTPUT);
    defineDependenciesConstructor(context, operator.getOutputs(), target, writer, method -> {
        method.visitVarInsn(Opcodes.ALOAD, 0);
        getNew(method, combinerClass);/*from ww w  . j  a  v a2  s  .c o m*/
        getNew(method, output.getDataType());
        method.visitVarInsn(Opcodes.ALOAD, 1);
        method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(CombineResult.class).getInternalName(),
                CONSTRUCTOR_NAME, Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ObjectCombiner.class),
                        typeOf(DataModel.class), typeOf(Result.class)),
                false);
    }, Lang.discard());

    return new ClassData(target, writer::toByteArray);
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

private static void defineCheckNull(ClassWriter writer, UserOperator operator, DataModelReference inputType) {

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, METHOD_CHECK_NON_NULL,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ValueOption.class), typeOf(Object.class),
                    typeOf(String.class)),
            null, null);//from  www  .j a  va2  s . com

    LocalVarRef optionVar = new LocalVarRef(Opcodes.ALOAD, 0);
    LocalVarRef objectVar = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef nameVar = new LocalVarRef(Opcodes.ALOAD, 2);

    // if (option.isNull()) {
    Label ifEnd = new Label();
    optionVar.load(method);
    getNullity(method, VALUE_DESC);
    method.visitJumpInsn(Opcodes.IFEQ, ifEnd);

    // new NullPointerException ...
    method.visitTypeInsn(Opcodes.NEW, typeOf(NullPointerException.class).getInternalName());
    method.visitInsn(Opcodes.DUP);

    // str = String.format("<type>.%s must not be null (in <operator>): %s", name, object)
    getConst(method,
            String.format("%s.%%s must not be null (in %s.%s): %%s", inputType.getDeclaration().getSimpleName(),
                    operator.getMethod().getDeclaringClass().getSimpleName(), operator.getMethod().getName()));

    getArray(method, typeOf(Object.class), new LocalVarRef[] { nameVar, objectVar });
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(String.class).getInternalName(), "format",
            Type.getMethodDescriptor(typeOf(String.class), typeOf(String.class), typeOf(Object[].class)),
            false);

    // throw new NullPointerException(str)
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(NullPointerException.class).getInternalName(),
            CONSTRUCTOR_NAME, Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(String.class)), false);

    method.visitInsn(Opcodes.ATHROW);

    method.visitLabel(ifEnd);
    // }
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Open Source License

public ByteCodeField(String clsName, int access, String name, String desc, String signature, Object value) {
    this.clsName = clsName;
    this.value = value;
    privateField = (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE;
    if (value != null && value instanceof String) {
        Parser.addToConstantPool((String) value);
    }//from  ww w  .j  av  a  2  s.  c  o m
    staticField = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
    finalField = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
    fieldName = name.replace('$', '_');

    arrayDimensions = 0;
    while (desc.startsWith("[")) {
        desc = desc.substring(1);
        arrayDimensions++;
    }
    char currentType = desc.charAt(0);
    switch (currentType) {
    case 'L':
        // Object skip until ;
        int idx = desc.indexOf(';');
        String objectType = desc.substring(1, idx);
        objectType = objectType.replace('/', '_').replace('$', '_');
        if (!dependentClasses.contains(objectType)) {
            dependentClasses.add(objectType);
        }
        //if (!privateField && !exportedClasses.contains(objectType)) {
        //    exportedClasses.add(objectType);
        //}

        type = objectType;
        break;
    case 'I':
        primitiveType = Integer.TYPE;
        break;
    case 'J':
        primitiveType = Long.TYPE;
        break;
    case 'B':
        primitiveType = Byte.TYPE;
        break;
    case 'S':
        primitiveType = Short.TYPE;
        break;
    case 'F':
        primitiveType = Float.TYPE;
        break;
    case 'D':
        primitiveType = Double.TYPE;
        break;
    case 'Z':
        primitiveType = Boolean.TYPE;
        break;
    case 'C':
        primitiveType = Character.TYPE;
        break;
    }
}

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

License:Open Source License

public BytecodeMethod(String clsName, int access, String name, String desc, String signature,
        String[] exceptions) {//ww  w.java  2  s  .  c  om
    methodName = name;
    this.clsName = clsName;
    this.desc = desc;
    privateMethod = (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE;
    nativeMethod = (access & Opcodes.ACC_NATIVE) == Opcodes.ACC_NATIVE;
    staticMethod = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
    finalMethod = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
    synchronizedMethod = (access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED;
    int pos = desc.lastIndexOf(')');
    if (!staticMethod) {
        if (!dependentClasses.contains("java_lang_NullPointerException")) {
            dependentClasses.add("java_lang_NullPointerException");
        }
    } // 
    if (methodName.equals("<init>")) {
        methodName = "__INIT__";
        constructor = true;
        returnType = new ByteCodeMethodArg(Void.TYPE, 0);
    } else {
        if (methodName.equals("<clinit>")) {
            methodName = "__CLINIT__";
            returnType = new ByteCodeMethodArg(Void.TYPE, 0);
            staticMethod = true;
        } else {
            String retType = desc.substring(pos + 1);
            if (retType.equals("V")) {
                returnType = new ByteCodeMethodArg(Void.TYPE, 0);
            } else {
                int dim = 0;
                while (retType.startsWith("[")) {
                    retType = retType.substring(1);
                    dim++;
                }
                char currentType = retType.charAt(0);
                switch (currentType) {
                case 'L':
                    // Object skip until ;
                    int idx = retType.indexOf(';');
                    String objectType = retType.substring(1, idx);
                    objectType = objectType.replace('/', '_').replace('$', '_');
                    if (!dependentClasses.contains(objectType)) {
                        dependentClasses.add(objectType);
                    }
                    //if (!this.isPrivate() && !exportedClasses.contains(objectType)) {
                    //    exportedClasses.add(objectType);
                    //}
                    returnType = new ByteCodeMethodArg(objectType, dim);
                    break;
                case 'I':
                    returnType = new ByteCodeMethodArg(Integer.TYPE, dim);
                    break;
                case 'J':
                    returnType = new ByteCodeMethodArg(Long.TYPE, dim);
                    break;
                case 'B':
                    returnType = new ByteCodeMethodArg(Byte.TYPE, dim);
                    break;
                case 'S':
                    returnType = new ByteCodeMethodArg(Short.TYPE, dim);
                    break;
                case 'F':
                    returnType = new ByteCodeMethodArg(Float.TYPE, dim);
                    break;
                case 'D':
                    returnType = new ByteCodeMethodArg(Double.TYPE, dim);
                    break;
                case 'Z':
                    returnType = new ByteCodeMethodArg(Boolean.TYPE, dim);
                    break;
                case 'C':
                    returnType = new ByteCodeMethodArg(Character.TYPE, dim);
                    break;
                }
            }
        }
    }
    int currentArrayDim = 0;
    desc = desc.substring(1, pos);
    for (int i = 0; i < desc.length(); i++) {
        char currentType = desc.charAt(i);
        switch (currentType) {
        case '[':
            // array of...
            currentArrayDim++;
            continue;
        case 'L':
            // Object skip until ;
            int idx = desc.indexOf(';', i);
            String objectType = desc.substring(i + 1, idx);
            objectType = objectType.replace('/', '_').replace('$', '_');
            if (!dependentClasses.contains(objectType)) {
                dependentClasses.add(objectType);
            }
            //if (!this.isPrivate() && !exportedClasses.contains(objectType)) {
            //    exportedClasses.contains(objectType);
            //}
            i = idx;
            arguments.add(new ByteCodeMethodArg(objectType, currentArrayDim));
            break;
        case 'I':
            arguments.add(new ByteCodeMethodArg(Integer.TYPE, currentArrayDim));
            break;
        case 'J':
            arguments.add(new ByteCodeMethodArg(Long.TYPE, currentArrayDim));
            break;
        case 'B':
            arguments.add(new ByteCodeMethodArg(Byte.TYPE, currentArrayDim));
            break;
        case 'S':
            arguments.add(new ByteCodeMethodArg(Short.TYPE, currentArrayDim));
            break;
        case 'F':
            arguments.add(new ByteCodeMethodArg(Float.TYPE, currentArrayDim));
            break;
        case 'D':
            arguments.add(new ByteCodeMethodArg(Double.TYPE, currentArrayDim));
            break;
        case 'Z':
            arguments.add(new ByteCodeMethodArg(Boolean.TYPE, currentArrayDim));
            break;
        case 'C':
            arguments.add(new ByteCodeMethodArg(Character.TYPE, currentArrayDim));
            break;
        }
        currentArrayDim = 0;
    }
}

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

License:Apache License

public PatternMatchedMethodMutator(String theClassName, MethodNode theMethod, int theApiVersion,
        MethodVisitor theDelegatingMethodVisitor, int theAccess, String theMethodName,
        String theMethodDescription, boolean theIsWithStackTraces) {
    super(theApiVersion, theDelegatingMethodVisitor, theAccess, theMethodName, theMethodDescription);
    className = theClassName;//from  w w w.j  a  v a 2 s  .  co  m
    method = theMethod;
    methodName = theMethodName;
    isStatic = (theAccess & Opcodes.ACC_STATIC) != 0;
    isWithStackTraces = theIsWithStackTraces;
}