Example usage for org.objectweb.asm Opcodes ACC_SYNCHRONIZED

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

Introduction

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

Prototype

int ACC_SYNCHRONIZED

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

Click Source Link

Usage

From source file:asmlib.InfoMethod.java

License:Open Source License

public boolean isSynchronized() {
    return (access() & Opcodes.ACC_SYNCHRONIZED) != 0;
}

From source file:blue.origami.asm.OAnno.java

License:Apache License

public void add(String spec) {
    String[] annos = spec.split(",");
    for (String s : annos) {
        s = s.toLowerCase();//  ww  w. j a  va 2  s  .c om
        switch (s) {
        case "public":
            acc |= Opcodes.ACC_PUBLIC;
            acc &= ~Opcodes.ACC_PROTECTED;
            acc &= ~Opcodes.ACC_PRIVATE;
            break;
        case "protected":
            acc |= Opcodes.ACC_PROTECTED;
            acc &= ~Opcodes.ACC_PUBLIC;
            acc &= ~Opcodes.ACC_PRIVATE;
            break;
        case "private":
            acc |= Opcodes.ACC_PRIVATE;
            acc &= ~Opcodes.ACC_PUBLIC;
            acc &= ~Opcodes.ACC_PROTECTED;
            break;
        case "static":
            acc |= Opcodes.ACC_STATIC;
            break;
        case "abstract":
            acc |= Opcodes.ACC_ABSTRACT;
            break;
        case "interface":
            acc |= Opcodes.ACC_INTERFACE;
            break;
        case "final":
            acc |= Opcodes.ACC_FINAL;
            break;
        case "native":
            acc |= Opcodes.ACC_NATIVE;
            break;
        case "synchronized":
            acc |= Opcodes.ACC_SYNCHRONIZED;
            break;
        case "strictfp":
            acc |= Opcodes.ACC_STRICT;
            break;
        }
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private String decodeAccess(int access) {
    String res = "";

    if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC)
        res += "public ";
    if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE)
        res += "private ";
    if ((access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED)
        res += "protected ";

    if ((access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC)
        res += "static ";
    if ((access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL)
        res += "final ";
    if ((access & Opcodes.ACC_VOLATILE) == Opcodes.ACC_VOLATILE)
        res += "protected ";
    if ((access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED)
        res += "synchronized ";
    if ((access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT)
        res += "abstract ";
    if ((access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE)
        res += "interface ";

    return res;/*from  www .j a  va 2 s .  c  o m*/
}

From source file:co.paralleluniverse.fibers.instrument.CheckInstrumentationVisitor.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, final String name, final String desc, String signature,
        String[] exceptions) {/*from  www . ja  va  2  s. co m*/
    SuspendableType suspendable = null;
    if (suspendableInterface)
        suspendable = SuspendableType.SUSPENDABLE_SUPER;
    if (suspendable == null)
        suspendable = classEntry.check(name, desc);
    if (suspendable == null)
        suspendable = classifier.isSuspendable(db, className, classEntry.getSuperName(),
                classEntry.getInterfaces(), name, desc, signature, exceptions);
    if (suspendable == SuspendableType.SUSPENDABLE) {
        hasSuspendable = true;
        // synchronized methods can't be made suspendable
        if ((access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED) {
            if (!className.equals("clojure/lang/LazySeq"))
                throw new UnableToInstrumentException("synchronized method", className, name, desc);
        }
    }
    classEntry.set(name, desc, suspendable);

    if (suspendable == null) // look for @Suspendable annotation
        return new MethodVisitor(Opcodes.ASM4) {
            private boolean susp = false;

            @Override
            public AnnotationVisitor visitAnnotation(String adesc, boolean visible) {
                if (adesc.equals(ANNOTATION_DESC))
                    susp = true;
                return null;
            }

            @Override
            public void visitEnd() {
                super.visitEnd();
                classEntry.set(name, desc,
                        susp ? SuspendableType.SUSPENDABLE : SuspendableType.NON_SUSPENDABLE);
                hasSuspendable = hasSuspendable | susp;
            }
        };
    else
        return null;
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentClass.java

License:Open Source License

private static boolean isSynchronized(int access) {
    return (access & Opcodes.ACC_SYNCHRONIZED) != 0;
}

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

/**
 * Parses an InputStream containing a class.
 * @param input The input stream with a class.
 * @return If a transformation occurred, the bytes for the changed class will be returned.  Otherwise null will be returned.
 * @throws Exception /*  ww w  .  ja  v  a 2s.  c om*/
 */
public static byte[] parse(InputStream input, ClassLoader classLoader) throws Exception {
    ClassReader r = new ClassReader(input);
    Parser p = new Parser();
    //ClassWriter w = new ClassWriter(r, 0);
    ClassNode classNode = new ClassNode();
    //p.classNode = classNode;

    r.accept(classNode, 0);
    //r.accept(p, ClassReader.EXPAND_FRAMES)

    List<MethodNode> methodsToAdd = new ArrayList<MethodNode>();
    int methodNum = 0;
    for (Object o : classNode.methods) {
        methodNum++;
        MethodNode methodNode = (MethodNode) o;
        boolean synchronizedMethod = (methodNode.access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED;
        if (synchronizedMethod) {
            // Check for a try statement
            final boolean[] tryCatchFound = new boolean[1];
            //System.out.println("Found sync method "+methodNode.name+". Checking for try blocks");
            methodNode.accept(new MethodVisitor(Opcodes.ASM5) {

                @Override
                public void visitTryCatchBlock(Label label, Label label1, Label label2, String string) {
                    tryCatchFound[0] = true;
                }

            });
            if (!tryCatchFound[0]) {
                continue;
            }

            //System.out.println("Instructions: "+Arrays.toString(methodNode.instructions.toArray()));

            System.out.println("Transforming method " + methodNode.name + " of class " + classNode.name);
            MethodDescriptor md = new MethodDescriptor(methodNode.access, methodNode.name, methodNode.desc);
            //methodNode.access = methodNode.access & ~Opcodes.ACC_SYNCHRONIZED;
            String privateMethodName = (md.constructor ? "___cn1init__" : methodNode.name) + "___cn1sync"
                    + (methodNum);
            MethodNode syncMethod = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
                    methodNode.signature,
                    (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));

            methodNode.name = privateMethodName;
            methodNode.access = (methodNode.access | Opcodes.ACC_PRIVATE) & ~Opcodes.ACC_PUBLIC
                    & ~Opcodes.ACC_PROTECTED & ~Opcodes.ACC_SYNCHRONIZED;
            LabelNode startLabel = new LabelNode();
            syncMethod.instructions.add(startLabel);
            LabelNode endLabel = new LabelNode();

            int argIndex = 0;
            if (!md.staticMethod) {
                //System.out.println(methodNode.name + " is not static");
                syncMethod.localVariables.add(new LocalVariableNode("arg" + (argIndex),
                        "L" + classNode.name + ";", null, startLabel, endLabel, argIndex));
                syncMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, argIndex++));

            }

            for (ByteCodeMethodArg arg : md.arguments) {
                char typeChar = arg.type;
                if (arg.dim > 0) {
                    typeChar = 'L';
                }
                if (arg.desc == null || arg.desc.isEmpty()) {
                    throw new RuntimeException(
                            "Invalid arg description for arg " + argIndex + " of method " + methodNode.name);
                }
                syncMethod.localVariables.add(new LocalVariableNode("arg" + (argIndex), arg.desc, arg.desc,
                        startLabel, endLabel, argIndex));

                switch (typeChar) {
                case 'L':
                    syncMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, argIndex++));
                    //syncMethod.localVariables.add(new LocalVariableNode("arg"+(argIndex-1), arg.desc, null, startLabel, endLabel, argIndex-1));
                    break;
                case 'S':
                case 'I':
                case 'B':
                case 'Z':
                case 'C':
                    syncMethod.instructions.add(new VarInsnNode(Opcodes.ILOAD, argIndex++));
                    break;
                case 'J':
                    syncMethod.instructions.add(new VarInsnNode(Opcodes.LLOAD, argIndex++));
                    argIndex++; // arg index increments 2 for double size args
                    break;
                case 'F':
                    syncMethod.instructions.add(new VarInsnNode(Opcodes.FLOAD, argIndex++));
                    break;
                case 'D':
                    syncMethod.instructions.add(new VarInsnNode(Opcodes.DLOAD, argIndex++));
                    argIndex++;// arg index increments 2 for double size args
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported argument type " + arg.type);
                }
            }

            if (md.staticMethod) {
                syncMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name,
                        privateMethodName, methodNode.desc));
            } else {
                syncMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name,
                        privateMethodName, methodNode.desc));
            }

            if (md.returnType != null) {
                char typeChar = md.returnType.type;
                if (md.returnType.dim > 0) {
                    typeChar = 'L';
                }
                switch (typeChar) {
                case 'L':
                    syncMethod.instructions.add(new InsnNode(Opcodes.ARETURN));
                    break;
                case 'S':
                case 'I':
                case 'B':
                case 'Z':
                case 'C':
                    syncMethod.instructions.add(new InsnNode(Opcodes.IRETURN));
                    break;
                case 'J':
                    syncMethod.instructions.add(new InsnNode(Opcodes.LRETURN));
                    break;
                case 'F':
                    syncMethod.instructions.add(new InsnNode(Opcodes.FRETURN));
                    break;
                case 'D':
                    syncMethod.instructions.add(new InsnNode(Opcodes.DRETURN));
                    break;
                case 'V':
                    syncMethod.instructions.add(new InsnNode(Opcodes.RETURN));
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported argument type " + md.returnType.type);
                }
            } else {
                syncMethod.instructions.add(new InsnNode(Opcodes.DRETURN));
            }

            syncMethod.instructions.add(endLabel);

            methodsToAdd.add(syncMethod);

        }
    }
    if (!methodsToAdd.isEmpty()) {
        changed = true;
        System.out
                .println("Transforming " + methodsToAdd.size() + " synchronized methods in " + classNode.name);
        classNode.methods.addAll(methodsToAdd);
        ClassWriter w = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(w);
        byte[] out = w.toByteArray();
        if (verify) {
            verify(out, classLoader);
        }
        return out;
    } else {
        ClassWriter w = new ClassWriter(0);
        classNode.accept(w);
        byte[] out = w.toByteArray();
        return out;
    }

}

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) {/*from  w  ww  . j av a  2  s  .  com*/
    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.facebook.buck.java.abi.MirrorTest.java

License:Apache License

@Test
public void shouldPreserveSynchronizedKeywordOnMethods() throws IOException {
    Path original = compileToJar(EMPTY_CLASSPATH, "A.java",
            Joiner.on("\n").join(ImmutableList.of("package com.example.buck;", "public class A {",
                    "  public synchronized void doMagic() {}", "}")));

    new StubJar(original).writeTo(filesystem, stubJar);

    AbiClass stub = readClass(stubJar, "com/example/buck/A.class");
    MethodNode magic = stub.findMethod("doMagic");
    assertTrue((magic.access & Opcodes.ACC_SYNCHRONIZED) > 0);
}

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

License:Apache License

@Test
public void testNotConfusedByOtherMethodAccessFlagsIncluding() {
    testIncludesMethodWithAccess(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNCHRONIZED);
}

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

License:Apache License

@Test
public void testNotConfusedByOtherMethodAccessFlagsExcluding() {
    testExcludesMethodWithAccess(Opcodes.ACC_PRIVATE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNCHRONIZED);
}