Example usage for org.objectweb.asm Opcodes ACC_PRIVATE

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

Introduction

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

Prototype

int ACC_PRIVATE

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

Click Source Link

Usage

From source file:org.summer.aop.PointcutExpressionParser.java

License:Open Source License

private static boolean matchModifiersPattern(int access, String modifier1, String modifier2, String modifier3) {
    if (modifier1 == null && modifier2 == null && modifier3 == null)
        return true; // modifiers-pattern is optional
    if ("*".equals(modifier1))
        modifier1 = null;/*  w  ww.  ja v a2  s  .  c o m*/
    switch (access) {
    case Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("public")) && "static".equals(modifier2)
                && "final".equals(modifier3);
    case Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("protected")) && "static".equals(modifier2)
                && "final".equals(modifier3);
    case Opcodes.ACC_STATIC + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("package-private")) && "static".equals(modifier2)
                && "final".equals(modifier3);
    case Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("private")) && "static".equals(modifier2)
                && "final".equals(modifier3);
    case Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC:
        return (modifier1 == null || modifier1.equals("public")) && "static".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC:
        return (modifier1 == null || modifier1.equals("protected")) && "static".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_STATIC:
        return (modifier1 == null || modifier1.equals("package-private")) && "static".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC:
        return (modifier1 == null || modifier1.equals("private")) && "static".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("public")) && "final".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_PROTECTED + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("protected")) && "final".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("package-private")) && "final".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL:
        return (modifier1 == null || modifier1.equals("private")) && "final".equals(modifier2)
                && modifier3 == null;
    case Opcodes.ACC_PUBLIC:
        return (modifier1 == null || modifier1.equals("public")) && modifier2 == null && modifier3 == null;
    case Opcodes.ACC_PROTECTED:
        return (modifier1 == null || modifier1.equals("protected")) && modifier2 == null && modifier3 == null;
    case 0x0000:
        return (modifier1 == null || modifier1.equals("package-private")) && modifier2 == null
                && modifier3 == null;
    case Opcodes.ACC_PRIVATE:
        return (modifier1 == null || modifier1.equals("private")) && modifier2 == null && modifier3 == null;
    }
    return false;
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

public byte[] build(String className, Dfa dfa) {
    dfa = reorder(dfa, getOrdering(dfa));

    className = className.replace('.', '/');
    this.className = className;
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    ClassVisitor cv = writer;//from  w  ww .java  2  s .c om
    cv.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object",
            new String[] { Type.getInternalName(Matcher.class) });

    cv.visitField(Opcodes.ACC_PRIVATE, "state", "I", null, null).visitEnd();
    cv.visitField(Opcodes.ACC_PRIVATE, "domain", "I", null, null).visitEnd();
    cv.visitField(Opcodes.ACC_PRIVATE, "index", "I", null, null).visitEnd();

    buildConstructor(cv, className);
    buildValidMethod(cv, className);
    buildDomainMethod(cv, className);
    buildIndexMethod(cv, className);
    buildRestartMethod(cv, className);
    buildForkMethod(cv, className);
    buildEndMethod(cv, className, dfa);
    buildWorkerMethod(cv, className, dfa);

    cv.visitEnd();

    byte[] result = writer.toByteArray();
    return result;
}

From source file:org.teavm.parsing.Parser.java

License:Apache License

public static void parseModifiers(int access, ElementHolder member) {
    if ((access & Opcodes.ACC_PRIVATE) != 0) {
        member.setLevel(AccessLevel.PRIVATE);
    } else if ((access & Opcodes.ACC_PROTECTED) != 0) {
        member.setLevel(AccessLevel.PROTECTED);
    } else if ((access & Opcodes.ACC_PUBLIC) != 0) {
        member.setLevel(AccessLevel.PUBLIC);
    }//  w  ww.  jav a2s .  co  m

    if ((access & Opcodes.ACC_ABSTRACT) != 0) {
        member.getModifiers().add(ElementModifier.ABSTRACT);
    }
    if ((access & Opcodes.ACC_ANNOTATION) != 0) {
        member.getModifiers().add(ElementModifier.ANNOTATION);
    }
    if ((access & Opcodes.ACC_BRIDGE) != 0) {
        member.getModifiers().add(ElementModifier.BRIDGE);
    }
    if ((access & Opcodes.ACC_DEPRECATED) != 0) {
        member.getModifiers().add(ElementModifier.DEPRECATED);
    }
    if ((access & Opcodes.ACC_ENUM) != 0) {
        member.getModifiers().add(ElementModifier.ENUM);
    }
    if ((access & Opcodes.ACC_FINAL) != 0) {
        member.getModifiers().add(ElementModifier.FINAL);
    }
    if ((access & Opcodes.ACC_INTERFACE) != 0) {
        member.getModifiers().add(ElementModifier.INTERFACE);
    }
    if ((access & Opcodes.ACC_NATIVE) != 0) {
        member.getModifiers().add(ElementModifier.NATIVE);
    }
    if ((access & Opcodes.ACC_STATIC) != 0) {
        member.getModifiers().add(ElementModifier.STATIC);
    }
    if ((access & Opcodes.ACC_STRICT) != 0) {
        member.getModifiers().add(ElementModifier.STRICT);
    }
    if ((access & Opcodes.ACC_SUPER) != 0) {
        member.getModifiers().add(ElementModifier.SUPER);
    }
    if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) {
        member.getModifiers().add(ElementModifier.SYNCHRONIZED);
    }
    if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
        member.getModifiers().add(ElementModifier.SYNTHETIC);
    }
    if ((access & Opcodes.ACC_TRANSIENT) != 0) {
        member.getModifiers().add(ElementModifier.TRANSIENT);
    }
    if ((access & Opcodes.ACC_VARARGS) != 0) {
        member.getModifiers().add(ElementModifier.VARARGS);
    }
    if ((access & Opcodes.ACC_VOLATILE) != 0) {
        member.getModifiers().add(ElementModifier.VOLATILE);
    }
}

From source file:pl.asie.foamfix.coremod.BlockPosPatch.java

License:Open Source License

public static byte[] patchVec3i(byte[] data) {
    final ClassReader reader = new ClassReader(data);
    final ClassNode node = new ClassNode();
    reader.accept(node, 8);/*from   w  ww.ja  v  a 2s. c  o m*/
    for (FieldNode fn : node.fields) {
        if ("I".equals(fn.desc) && fn.access == (Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL)) {
            fn.access = Opcodes.ACC_PROTECTED;
        }
    }
    final ClassWriter writer = new ClassWriter(8);
    node.accept(writer);
    return writer.toByteArray();
}

From source file:pl.asie.foamfix.coremod.patches.BlockPosPatch.java

License:Open Source License

public static ClassNode patchVec3i(ClassNode node) {
    for (FieldNode fn : node.fields) {
        if ("I".equals(fn.desc) && fn.access == (Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL)) {
            fn.access = Opcodes.ACC_PROTECTED;
        }/*from w  w  w.jav  a2  s . c o  m*/
    }
    return node;
}

From source file:pl.clareo.coroutines.core.ClassTransformer.java

License:Apache License

@SuppressWarnings("unchecked")
void transform() {
    for (MethodNode coroutine : coroutines) {
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Generating method for coroutine " + coroutine.name + coroutine.desc);
        }/*  w  ww.j a  v  a2s.  c  o  m*/
        String coroutineName = getCoroutineName(coroutine);
        MethodTransformer methodTransformer = new MethodTransformer(coroutine, thisType);
        MethodNode coroutineImpl = methodTransformer.transform(coroutineName, generateDebugCode);
        thisNode.methods.add(coroutineImpl);
        /*
         * generate co iterators and method stubs
         */
        log.finest("Generating CoIterator implementation and method stubs");
        String baseCoIteratorName;
        Map<String, Object> annotation = getCoroutineAnnotationValues(coroutine);
        if (getBoolean(annotation, "threadLocal")) {
            baseCoIteratorName = Type.getInternalName(ThreadLocalCoIterator.class);
        } else {
            baseCoIteratorName = Type.getInternalName(SingleThreadedCoIterator.class);
        }
        String coIteratorClassName = "pl/clareo/coroutines/core/CoIterator" + num;
        ClassNode coIteratorClass = new ClassNode();
        coIteratorClass.version = Opcodes.V1_6;
        coIteratorClass.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER;
        coIteratorClass.name = coIteratorClassName;
        coIteratorClass.superName = baseCoIteratorName;
        if (generateDebugCode) {
            /*
             * If debugging code is emitted create field keeping JDK logger
             */
            FieldNode loggerField = new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC,
                    "logger", "Ljava/util/logging/Logger;", null, null);
            coIteratorClass.fields.add(loggerField);
            MethodNode clinit = new MethodNode();
            clinit.access = Opcodes.ACC_STATIC;
            clinit.name = "<clinit>";
            clinit.desc = "()V";
            clinit.exceptions = Collections.EMPTY_LIST;
            String loggerName = thisType.getClassName();
            InsnList clinitCode = clinit.instructions;
            clinitCode.add(new LdcInsnNode(loggerName));
            clinitCode.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/logging/Logger", "getLogger",
                    "(Ljava/lang/String;)Ljava/util/logging/Logger;"));
            clinitCode.add(new FieldInsnNode(Opcodes.PUTSTATIC, coIteratorClassName, "logger",
                    "Ljava/util/logging/Logger;"));
            clinitCode.add(new InsnNode(Opcodes.RETURN));
            clinit.maxStack = 1;
            clinit.maxLocals = 0;
            coIteratorClass.methods.add(clinit);
        }
        /*
         * Generate constructor
         */
        MethodNode init = new MethodNode();
        init.access = Opcodes.ACC_PUBLIC;
        init.name = "<init>";
        init.desc = CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR;
        init.exceptions = Collections.EMPTY_LIST;
        InsnList initCode = init.instructions;
        initCode.add(new VarInsnNode(Opcodes.ALOAD, 0));
        initCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
        initCode.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, baseCoIteratorName, "<init>",
                CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR));
        initCode.add(new InsnNode(Opcodes.RETURN));
        init.maxStack = 2;
        init.maxLocals = 2;
        coIteratorClass.methods.add(init);
        /*
         * Generate overriden call to coroutine
         */
        MethodNode call = new MethodNode();
        call.access = Opcodes.ACC_PROTECTED;
        call.name = "call";
        call.desc = CALL_METHOD_DESCRIPTOR;
        call.exceptions = Collections.EMPTY_LIST;
        InsnList callCode = call.instructions;
        /*
         * if debug needed generate call details
         */
        if (generateDebugCode) {
            String coroutineId = "Coroutine " + coroutine.name;
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER,
                    coroutineId + " call. Caller sent: ", 2));
            callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK));
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINEST,
                    coroutineId + " state ", 1));
            callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK));
        }
        /*
         * push call arguments: this (if not static), frame, input, output
         */
        boolean isStatic = (coroutine.access & Opcodes.ACC_STATIC) != 0;
        if (!isStatic) {
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
            callCode.add(
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getThis", "()Ljava/lang/Object;"));
            callCode.add(new TypeInsnNode(Opcodes.CHECKCAST, thisType.getInternalName()));
        }
        callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
        callCode.add(new InsnNode(Opcodes.ACONST_NULL));
        callCode.add(new VarInsnNode(Opcodes.ALOAD, 2));
        callCode.add(new MethodInsnNode(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
                thisType.getInternalName(), coroutineName, COROUTINE_METHOD_DESCRIPTOR));
        // stack: *
        if (!generateDebugCode) {
            callCode.add(new InsnNode(Opcodes.ARETURN));
        } else {
            // save result display suspension point (two more locals
            // needed)
            callCode.add(new VarInsnNode(Opcodes.ASTORE, 3));
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
            callCode.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLineOfCode", "()I"));
            callCode.add(box_int(Type.INT));
            callCode.add(new VarInsnNode(Opcodes.ASTORE, 4));
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER,
                    "Coroutine suspended at line ", 4, ". Yielded:", 3));
            callCode.add(new FrameNode(Opcodes.F_APPEND, 2,
                    new Object[] { "java/lang/Object", "java/lang/Integer" }, 0, EMPTY_STACK));
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 3));
            callCode.add(new InsnNode(Opcodes.ARETURN));
        }
        coIteratorClass.methods.add(call);
        // if debugging code is emitted it needs space for two
        // additional locals and 5 stack operand
        if (generateDebugCode) {
            call.maxStack = 5;
            call.maxLocals = 5;
        } else {
            if (isStatic) {
                call.maxStack = 3;
            } else {
                call.maxStack = 4;
            }
            call.maxLocals = 3;
        }
        /*
         * CoIterator created - define it in the runtime and verify if
         * needed
         */
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Generated class " + coIteratorClassName);
        }
        ClassWriter cw = new ClassWriter(0);
        coIteratorClass.accept(cw);
        byte[] classBytes = cw.toByteArray();
        try {
            CoroutineInstrumentator.dumpClass(coIteratorClassName, classBytes);
        } catch (IOException e) {
            throw new CoroutineGenerationException("Unable to write class " + coIteratorClassName, e);
        }
        /*
         * start generating method - new method is named as the method in
         * user code, it: returns instance of appropriate CoIterator (see
         * above), saves arguments of call
         */
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Instrumenting method " + coroutine.name);
        }
        InsnList code = coroutine.instructions;
        code.clear();
        /*
         * create new Frame
         */
        boolean isDebugFramePossible = generateDebugCode && coroutine.localVariables != null;
        if (isDebugFramePossible) {
            code.add(createDebugFrame(coroutine));
        } else {
            code.add(createFrame(coroutine));
        }
        /*
         * save frame in the first, and locals array in the second local
         * variable
         */
        int argsSize = Type.getArgumentsAndReturnSizes(coroutine.desc) >> 2;
        if (isStatic) {
            argsSize -= 1;
        }
        code.add(new VarInsnNode(Opcodes.ASTORE, argsSize));
        code.add(new VarInsnNode(Opcodes.ALOAD, argsSize));
        code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLocals", "()[Ljava/lang/Object;"));
        int localsArrayIndex = argsSize + 1;
        code.add(new VarInsnNode(Opcodes.ASTORE, localsArrayIndex));
        /*
         * save all call arguments (along with this if this method is not
         * static) into locals array
         */
        Type[] argsTypes = Type.getArgumentTypes(coroutine.desc);
        if (!isStatic) {
            code.add(saveloc(localsArrayIndex, 0, 0, JAVA_LANG_OBJECT));
            code.add(savelocs(localsArrayIndex, 1, 1, argsTypes));
        } else {
            code.add(savelocs(localsArrayIndex, 0, 0, argsTypes));
        }
        /*
         * create CoIterator instance with saved frame, make initial call to
         * next if needed and return to caller
         */
        code.add(new TypeInsnNode(Opcodes.NEW, coIteratorClassName));
        code.add(new InsnNode(Opcodes.DUP));
        code.add(new VarInsnNode(Opcodes.ALOAD, argsSize));
        code.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, coIteratorClassName, "<init>",
                CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR));
        if (!getBoolean(annotation, "generator", true)) {
            code.add(new InsnNode(Opcodes.DUP));
            code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, coIteratorClassName, "next",
                    "()Ljava/lang/Object;"));
            code.add(new InsnNode(Opcodes.POP));
        }
        code.add(new InsnNode(Opcodes.ARETURN));
        /*
         * end method generation; maxs can be statically determined 3
         * operands on stack (call to frame setLocals and CoIterator
         * constructor) + 1 if any argument is long or double (debug frame
         * needs 7 operands for variable names creation); locals = argsSize
         * + 1 reference to frame + 1 array of locals
         */
        if (isDebugFramePossible) {
            coroutine.maxStack = 7;
        } else {
            boolean isCategory2ArgumentPresent = false;
            for (Type argType : argsTypes) {
                int sort = argType.getSort();
                if (sort == Type.LONG || sort == Type.DOUBLE) {
                    isCategory2ArgumentPresent = true;
                    break;
                }
            }
            coroutine.maxStack = isCategory2ArgumentPresent ? 4 : 3;
        }
        coroutine.maxLocals = localsArrayIndex + 1;
        coroutine.localVariables.clear();
        coroutine.tryCatchBlocks.clear();
        num++;
    }
}

From source file:pt.minha.kernel.instrument.SyncToMonitorClassVisitor.java

License:Open Source License

public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (trans.isSynchronized()) {
        if (name.equals("<clinit>")) {
            hasClinit = true;/*from   w w  w . j a  v a 2 s.  c o m*/
            return new ClinitVisitor(super.visitMethod(access, name, desc, signature, exceptions));
        }

        if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) {
            makeStub(access & ~Opcodes.ACC_SYNCHRONIZED, name, desc, signature, exceptions);

            return super.visitMethod(
                    (access & ~(Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)
                            | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE),
                    "_" + name, desc, signature, exceptions);
        }
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:scouter.agent.asm.MethodASM.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (mv == null || mset.isA(name, desc) == false) {
        return mv;
    }/*  w  w  w. j a  va  2s.c  o  m*/
    if (AsmUtil.isSpecial(name)) {
        return mv;
    }

    // check exclude method set
    for (int i = 0; i < excludeTarget.size(); i++) {
        HookingSet excludeSet = excludeTarget.get(i);
        if (excludeSet.classMatch.include(className)) {
            if (excludeSet.isA(name, desc)) {
                return mv;
            }
        }
    }

    Configure conf = Configure.getInstance();
    boolean isPublic = conf.hook_method_access_public_enabled;
    boolean isProtected = conf.hook_method_access_protected_enabled;
    boolean isPrivate = conf.hook_method_access_private_enabled;
    boolean isNone = conf.hook_method_access_none_enabled;

    //lambda method
    if (conf.hook_method_lambda_enable && (access & Opcodes.ACC_SYNTHETIC) == Opcodes.ACC_SYNTHETIC
            && name.indexOf("lambda$") == 0) {
        //if lambda method with hook_method_lambda_enabled then go on without method accessor check
    } else { // non-lambda method
        switch (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE)) {
        case Opcodes.ACC_PUBLIC:
            if (isPublic == false)
                return mv;
            break;
        case Opcodes.ACC_PROTECTED:
            if (isProtected == false)
                return mv;
            break;
        case Opcodes.ACC_PRIVATE:
            if (isPrivate == false)
                return mv;
            break;
        default:
            if (isNone == false)
                return mv;
            break;
        }
    }

    // check prefix, to ignore simple method such as getter,setter
    if (conf.isIgnoreMethodPrefix(name))
        return mv;

    String fullname = AsmUtil.makeMethodFullName(className, name, desc);
    int fullname_hash = DataProxy.sendMethodName(fullname);

    return new MethodMV(access, desc, mv, fullname, fullname_hash);
}

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/*from w  w w. j a va  2 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:sg.atom.core.actor.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Write the fields of//from  w ww .  j a  va 2  s.c  o  m
 *
 * @Prop properties.
 * @param bcd the class descriptor
 * @param cw the ClassWriter to write to
 */
public static void writePropFields(BeanClassDescriptor bcd, ClassWriter cw) {
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (pd.getPropertySource() != PropertySource.ABSTRACT_METHOD) {
            continue;
        }

        cw.visitField(Opcodes.ACC_PRIVATE + (pd.getAccess().isWritable() ? 0 : Opcodes.ACC_FINAL),
                String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()),
                Type.getDescriptor(pd.getPropertyClass()), GenericTypeHelper.getSignature(pd.getPropertyType()),
                null).visitEnd();
    }
}