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:sg.atom.core.actor.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Creates a synchronized delegate method for a message method.
 *
 * @param actorClass the actor class//w  w w. j  a v a  2s .c  om
 * @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:sg.atom.core.actor.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Creates and loads the actor's proxy class.
 *
 * @param actorClass the Actor class/*w w w . ja  v  a2 s .c  o  m*/
 * @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:sg.atom.core.actor.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the accessor methods for/*w w  w  .ja v  a  2  s .  com*/
 *
 * @Prop generated properties.
 * @param bcd the class descriptor
 * @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:the.bytecode.club.bytecodeviewer.decompilers.bytecode.MethodNodeDecompiler.java

License:Open Source License

private static String getAccessString(int access) {
    // public, protected, private, abstract, static,
    // final, synchronized, native & strictfp are permitted
    List<String> tokens = new ArrayList<String>();
    if ((access & Opcodes.ACC_PUBLIC) != 0)
        tokens.add("public");
    if ((access & Opcodes.ACC_PRIVATE) != 0)
        tokens.add("private");
    if ((access & Opcodes.ACC_PROTECTED) != 0)
        tokens.add("protected");
    if ((access & Opcodes.ACC_STATIC) != 0)
        tokens.add("static");
    if ((access & Opcodes.ACC_ABSTRACT) != 0)
        tokens.add("abstract");
    if ((access & Opcodes.ACC_FINAL) != 0)
        tokens.add("final");
    if ((access & Opcodes.ACC_SYNCHRONIZED) != 0)
        tokens.add("synchronized");
    if ((access & Opcodes.ACC_NATIVE) != 0)
        tokens.add("native");
    if ((access & Opcodes.ACC_STRICT) != 0)
        tokens.add("strictfp");
    if ((access & Opcodes.ACC_BRIDGE) != 0)
        tokens.add("bridge");
    if ((access & Opcodes.ACC_VARARGS) != 0)
        tokens.add("varargs");
    if (tokens.size() == 0)
        return "";
    // hackery delimeters
    StringBuilder sb = new StringBuilder(tokens.get(0));
    for (int i = 1; i < tokens.size(); i++) {
        sb.append(" ");
        sb.append(tokens.get(i));//from   ww w  . j  a v  a  2  s  .  c  om
    }
    return sb.toString();
}