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:net.sf.profiler4j.agent.BytecodeTransformer.java

License:Apache License

private static boolean isGetterSetter(int flag, String name, String methodDescriptor) {
    if ((Opcodes.ACC_PUBLIC | flag) == 0 || ((Opcodes.ACC_STATIC | flag) | (Opcodes.ACC_NATIVE | flag)
            | (Opcodes.ACC_ABSTRACT | flag) | (Opcodes.ACC_SYNCHRONIZED | flag)) != 0) {
        return false;
    }/*from  ww w.j a va 2  s  .c  om*/
    Type[] pTypes = Type.getArgumentTypes(methodDescriptor);
    Type rType = Type.getReturnType(methodDescriptor);
    if (getterRegex.matcher(name).matches() || getterBoolRegex.matcher(name).matches()) {
        return pTypes.length == 0 && !rType.equals(Type.VOID_TYPE);
    }
    if (setterRegex.matcher(name).matches()) {
        return pTypes.length == 1 && !rType.equals(Type.VOID_TYPE);
    }
    return false;
}

From source file:org.actorsguildframework.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Creates a synchronized delegate method for a message method.
 * @param actorClass the actor class/*from  w w  w. ja  va  2  s  .  co m*/
 * @param classNameDescriptor the descriptor of the resulting class
 * @param cw the class writer to write to
 * @param method the method being invoked
 * @param simpleDescriptor the descriptor of the method
 * @param genericSignature the generic signature of the method
 * @param isSynchronized true to make the method synchronized, false otherwise
 */
private static void writeSuperProxyMethod(Class<?> actorClass, String classNameDescriptor, ClassWriter cw,
        Method method, String simpleDescriptor, String genericSignature, boolean isSynchronized)
        throws NoSuchMethodException {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + (isSynchronized ? Opcodes.ACC_SYNCHRONIZED : 0),
            String.format(SUPER_CALLER_NAME_FORMAT, method.getName()), simpleDescriptor, genericSignature,
            null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    for (int j = 0; j < method.getParameterTypes().length; j++)
        mv.visitVarInsn(Type.getType(method.getParameterTypes()[j]).getOpcode(Opcodes.ILOAD), j + 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(actorClass), method.getName(),
            simpleDescriptor);
    mv.visitInsn(Type.getType(method.getReturnType()).getOpcode(Opcodes.IRETURN));
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
    for (int j = 0; j < method.getParameterTypes().length; j++)
        mv.visitLocalVariable("arg" + j, Type.getDescriptor(method.getParameterTypes()[j]),
                GenericTypeHelper.getSignatureIfGeneric(method.getGenericParameterTypes()[j]), l0, l1, j + 1);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.actorsguildframework.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Creates and loads the actor's proxy class.
 * @param actorClass the Actor class//from  w ww . ja v  a2 s .  c om
 * @param acd the actor's class descriptor
 * @throws ConfigurationException if the agent is not configured correctly
 */
@SuppressWarnings("unchecked")
private static Class<?> generateProxyClass(Class<?> actorClass, final ActorClassDescriptor acd)
        throws NoSuchMethodException {
    BeanClassDescriptor bcd = acd.getBeanClassDescriptor();

    String className = String.format("%s__ACTORPROXY", actorClass.getName());
    final String classNameInternal = className.replace('.', '/');
    String classNameDescriptor = "L" + classNameInternal + ";";

    final Type actorState = Type
            .getType(acd.getConcurrencyModel().isMultiThreadingCapable() ? MultiThreadedActorState.class
                    : SingleThreadedActorState.class);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC,
            classNameInternal, null, Type.getInternalName(actorClass),
            new String[] { "org/actorsguildframework/internal/ActorProxy" });

    cw.visitSource(null, null);

    {
        for (int i = 0; i < acd.getMessageCount(); i++)
            cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
                    String.format(MESSAGE_CALLER_NAME_FORMAT, i),
                    "Lorg/actorsguildframework/internal/MessageCaller;",
                    "Lorg/actorsguildframework/internal/MessageCaller<*>;", null).visitEnd();

        cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "actorState__ACTORPROXY",
                actorState.getDescriptor(), null, null).visitEnd();
    }

    BeanCreator.writePropFields(bcd, cw);

    {
        mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();

        for (int i = 0; i < acd.getMessageCount(); i++) {
            Class<?> caller = createMessageCaller(acd.getMessage(i).getOwnerClass(),
                    acd.getMessage(i).getMethod());
            String mcName = Type.getInternalName(caller);
            mv.visitTypeInsn(Opcodes.NEW, mcName);
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, mcName, "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTSTATIC, classNameInternal,
                    String.format(MESSAGE_CALLER_NAME_FORMAT, i),
                    "Lorg/actorsguildframework/internal/MessageCaller;");
        }
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    BeanCreator.writeConstructor(actorClass, bcd, classNameInternal, cw, new BeanCreator.SnippetWriter() {
        @Override
        public void write(MethodVisitor mv) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitTypeInsn(Opcodes.NEW, actorState.getInternalName());
            mv.visitInsn(Opcodes.DUP);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, actorState.getInternalName(), "<init>",
                    "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Actor;)V");
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal, "actorState__ACTORPROXY",
                    actorState.getDescriptor());
        }
    });

    BeanCreator.writePropAccessors(bcd, classNameInternal, cw);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getState__ACTORPROXYMETHOD",
                "()Lorg/actorsguildframework/internal/ActorState;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal, "actorState__ACTORPROXY",
                actorState.getDescriptor());
        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    for (int i = 0; i < acd.getMessageCount(); i++) {
        MessageImplDescriptor mid = acd.getMessage(i);
        Method method = mid.getMethod();
        String simpleDescriptor = Type.getMethodDescriptor(method);
        String genericSignature = GenericTypeHelper.getSignature(method);

        writeProxyMethod(classNameInternal, classNameDescriptor, cw, i, actorState, acd.getConcurrencyModel(),
                mid, method, simpleDescriptor, genericSignature);

        writeSuperProxyMethod(actorClass, classNameDescriptor, cw, method, simpleDescriptor, genericSignature,
                !acd.getConcurrencyModel().isMultiThreadingCapable());
    }

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNCHRONIZED, "toString", "()Ljava/lang/String;",
                null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    try {
        return (Class<? extends ActorProxy>) GenerationUtils.loadClass(className, cw.toByteArray());
    } catch (Exception e) {
        throw new ConfigurationException("Failure loading ActorProxy", e);
    }
}

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the accessor methods for @Prop generated properties.
 * @param bcd the class descriptor/*from w w  w. j  a  v  a 2s.  c o  m*/
 * @param classNameInternal the internal name of this class
 * @param cw the ClassWriter to write to
 */
public static void writePropAccessors(BeanClassDescriptor bcd, String classNameInternal, ClassWriter cw) {
    String classNameDescriptor = "L" + classNameInternal + ";";

    MethodVisitor mv;
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (!pd.getPropertySource().isGenerating())
            continue;

        {
            Type t = Type.getType(pd.getPropertyClass());
            Method orig = pd.getGetter();
            mv = cw.visitMethod(
                    GenerationUtils.convertAccessModifiers(orig.getModifiers())
                            + (bcd.isThreadSafe() ? Opcodes.ACC_SYNCHRONIZED : 0),
                    orig.getName(), Type.getMethodDescriptor(orig), GenericTypeHelper.getSignature(orig), null);

            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()),
                    Type.getDescriptor(pd.getPropertyClass()));
            mv.visitInsn(t.getOpcode(Opcodes.IRETURN));
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }

        if (pd.getAccess().isWritable()) {
            Type t = Type.getType(pd.getPropertyClass());
            Method orig = pd.getSetter();
            mv = cw.visitMethod(
                    GenerationUtils.convertAccessModifiers(orig.getModifiers())
                            + (bcd.isThreadSafe() ? Opcodes.ACC_SYNCHRONIZED : 0),
                    orig.getName(), Type.getMethodDescriptor(orig), GenericTypeHelper.getSignature(orig), null);

            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), 1);
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()),
                    Type.getDescriptor(pd.getPropertyClass()));
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
            mv.visitLocalVariable("value", Type.getDescriptor(pd.getPropertyClass()), null, l0, l1, 1);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
    }
}

From source file:org.adjective.stout.writer.ByteCodeWriter.java

License:Apache License

private int getModifierCode(Set<ElementModifier> modifiers, MemberType type) {
    int code = getModifierCode(modifiers, (Sort) null);
    int illegal = Opcodes.ACC_ANNOTATION | Opcodes.ACC_BRIDGE | Opcodes.ACC_ENUM | Opcodes.ACC_INTERFACE
            | Opcodes.ACC_SUPER;//from   w w w  .  j  a v a  2s  . c o  m
    switch (type) {
    case FIELD:
        illegal |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE | Opcodes.ACC_STRICT | Opcodes.ACC_SYNCHRONIZED;
        break;
    case METHOD:
        if (isBitSet(Opcodes.ACC_ABSTRACT, code)) {
            illegal |= Opcodes.ACC_NATIVE | Opcodes.ACC_STRICT | Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_STATIC
                    | Opcodes.ACC_FINAL;
        }
        break;
    }
    if (isBitSet(illegal, code)) {
        throw new IllegalStateException(
                "Illegal combination of modifier codes: " + code + " (illegal codes: " + illegal + ")");
    }
    return code;
}

From source file:org.apache.aries.versioning.utils.GenericDeclaration.java

License:Apache License

public GenericDeclaration(int access, String name, String signature) {
    int updatedAccess = access;
    // ignore the native or synchronized modifier as they do not affect binary compatibility
    if (Modifier.isNative(access)) {
        updatedAccess = updatedAccess - Opcodes.ACC_NATIVE;
    }/*w  ww .ja v  a  2 s  . c om*/
    if (Modifier.isSynchronized(access)) {
        updatedAccess = updatedAccess - Opcodes.ACC_SYNCHRONIZED;
    }
    this.access = access;
    this.name = name;
    this.signature = signature;
}

From source file:org.apache.tika.parser.asm.XHTMLClassVisitor.java

License:Apache License

private void writeAccess(int access) throws SAXException {
    writeAccess(access, Opcodes.ACC_PRIVATE, "private");
    writeAccess(access, Opcodes.ACC_PROTECTED, "protected");
    writeAccess(access, Opcodes.ACC_PUBLIC, "public");
    writeAccess(access, Opcodes.ACC_STATIC, "static");
    writeAccess(access, Opcodes.ACC_FINAL, "final");
    writeAccess(access, Opcodes.ACC_ABSTRACT, "abstract");
    writeAccess(access, Opcodes.ACC_SYNCHRONIZED, "synchronized");
    writeAccess(access, Opcodes.ACC_TRANSIENT, "transient");
    writeAccess(access, Opcodes.ACC_VOLATILE, "volatile");
    writeAccess(access, Opcodes.ACC_NATIVE, "native");
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void annotationDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<>();
    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Annotation Definition", modifiers, Opcodes.ACC_SYNCHRONIZED,
                "synchronized");
        node = node.getNextSibling();/*from www . j  a v  a  2 s.c om*/
    }
    modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;

    String name = identifier(node);
    node = node.getNextSibling();
    ClassNode superClass = ClassHelper.OBJECT_TYPE;

    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }

    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(EXTENDS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }

    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
    classNode.setSyntheticPublic(syntheticPublic);
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    classNode.addInterface(ClassHelper.Annotation_TYPE);
    configureAST(classNode, classDef);

    assertNodeType(OBJBLOCK, node);
    objectBlock(node);
    output.addClass(classNode);
    classNode = null;
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void innerInterfaceDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<>();
    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Interface", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();//from  w w w.j  av a  2s  .c o  m
    }
    modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE;

    String name = identifier(node);
    node = node.getNextSibling();
    ClassNode superClass = ClassHelper.OBJECT_TYPE;

    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }

    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(EXTENDS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }

    ClassNode outerClass = classNode;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    if (classNode != null) {
        name = classNode.getNameWithoutPackage() + "$" + name;
        String fullName = dot(classNode.getPackageName(), name);
        classNode = new InnerClassNode(classNode, fullName, modifiers, superClass, interfaces, null);
    } else {
        classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
    }
    classNode.setSyntheticPublic(syntheticPublic);
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    configureAST(classNode, classDef);

    assertNodeType(OBJBLOCK, node);
    objectBlock(node);
    output.addClass(classNode);

    classNode = outerClass;
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void innerClassDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<>();

    if (isType(TRAIT_DEF, classDef)) {
        annotations.add(new AnnotationNode(ClassHelper.make("groovy.transform.Trait")));
    }// w  ww  .  ja v a  2  s  . c o  m

    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Class", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();
    }

    String name = identifier(node);
    node = node.getNextSibling();

    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }

    ClassNode superClass = null;
    if (isType(EXTENDS_CLAUSE, node)) {
        superClass = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }

    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(IMPLEMENTS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }

    // TODO read mixins
    MixinNode[] mixins = {};
    ClassNode outerClass = classNode;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    if (classNode != null) {
        name = classNode.getNameWithoutPackage() + "$" + name;
        String fullName = dot(classNode.getPackageName(), name);
        if (classNode.isInterface()) {
            modifiers |= Opcodes.ACC_STATIC;
        }
        classNode = new InnerClassNode(classNode, fullName, modifiers, superClass, interfaces, mixins);
    } else {
        classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, mixins);
    }
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    classNode.setSyntheticPublic(syntheticPublic);
    configureAST(classNode, classDef);

    // we put the class already in output to avoid the most inner classes
    // will be used as first class later in the loader. The first class
    // there determines what GCL#parseClass for example will return, so we
    // have here to ensure it won't be the inner class
    output.addClass(classNode);

    assertNodeType(OBJBLOCK, node);
    objectBlock(node);

    classNode = outerClass;
}