Search in sources :

Example 1 with ClassMethod

use of org.jboss.classfilewriter.ClassMethod in project undertow by undertow-io.

the class AbstractParserGenerator method createStateMachine.

protected void createStateMachine(final String[] originalItems, final String className, final ClassFile file, final ClassMethod sctor, final AtomicInteger fieldCounter, final String methodName, final CustomStateMachine stateMachine) {
    //list of all states except the initial
    final List<State> allStates = new ArrayList<State>();
    final State initial = new State((byte) 0, "");
    for (String value : originalItems) {
        addStates(initial, value, allStates);
    }
    //we want initial to be number 0
    final AtomicInteger stateCounter = new AtomicInteger(-1);
    setupStateNo(initial, stateCounter, fieldCounter);
    for (State state : allStates) {
        setupStateNo(state, stateCounter, fieldCounter);
        createStateField(state, file, sctor.getCodeAttribute());
    }
    final int noStates = stateCounter.get();
    final ClassMethod handle = file.addMethod(Modifier.PROTECTED | Modifier.FINAL, methodName, "V", DescriptorUtils.makeDescriptor(ByteBuffer.class), parseStateDescriptor, httpExchangeDescriptor);
    writeStateMachine(className, file, handle.getCodeAttribute(), initial, allStates, noStates, stateMachine, sctor);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ClassMethod(org.jboss.classfilewriter.ClassMethod) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ClassMethod

use of org.jboss.classfilewriter.ClassMethod in project undertow by undertow-io.

the class AbstractParserGenerator method createTokenizer.

public byte[] createTokenizer(final String[] httpVerbs, String[] httpVersions, String[] standardHeaders) {
    final String className = existingClassName + CLASS_NAME_SUFFIX;
    final ClassFile file = new ClassFile(className, existingClassName);
    final ClassMethod ctor = file.addMethod(AccessFlag.PUBLIC, "<init>", "V", DescriptorUtils.parameterDescriptors(constructorDescriptor));
    ctor.getCodeAttribute().aload(0);
    ctor.getCodeAttribute().loadMethodParameters();
    ctor.getCodeAttribute().invokespecial(existingClassName, "<init>", constructorDescriptor);
    ctor.getCodeAttribute().returnInstruction();
    final ClassMethod sctor = file.addMethod(AccessFlag.PUBLIC | AccessFlag.STATIC, "<clinit>", "V");
    final AtomicInteger fieldCounter = new AtomicInteger(1);
    sctor.getCodeAttribute().invokestatic(existingClassName, "httpStrings", "()" + DescriptorUtils.makeDescriptor(Map.class));
    sctor.getCodeAttribute().astore(CONSTRUCTOR_HTTP_STRING_MAP_VAR);
    createStateMachines(httpVerbs, httpVersions, standardHeaders, className, file, sctor, fieldCounter);
    sctor.getCodeAttribute().returnInstruction();
    return file.toBytecode();
}
Also used : ClassFile(org.jboss.classfilewriter.ClassFile) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClassMethod(org.jboss.classfilewriter.ClassMethod)

Example 3 with ClassMethod

use of org.jboss.classfilewriter.ClassMethod in project wildfly by wildfly.

the class IIOPStubCompiler method generateCode.

/**
     * Generates the bytecodes of a stub class for a given interface.
     *
     * @param interfaceAnalysis an <code>InterfaceAnalysis</code> instance
     *                        describing the RMI/IIOP interface to be
     *                        implemented by the stub class
     * @param superclass      the superclass of the stub class
     * @param stubClassName   the name of the stub class
     * @return a byte array with the generated bytecodes.
     */
private static ClassFile generateCode(InterfaceAnalysis interfaceAnalysis, Class<?> superclass, String stubClassName) {
    final ClassFile asm = new ClassFile(stubClassName, superclass.getName(), interfaceAnalysis.getCls().getName());
    int methodIndex = 0;
    AttributeAnalysis[] attrs = interfaceAnalysis.getAttributes();
    for (int i = 0; i < attrs.length; i++) {
        OperationAnalysis op = attrs[i].getAccessorAnalysis();
        generateMethodCode(asm, superclass, op.getMethod(), op.getIDLName(), strategy(methodIndex), init(methodIndex));
        methodIndex++;
        op = attrs[i].getMutatorAnalysis();
        if (op != null) {
            generateMethodCode(asm, superclass, op.getMethod(), op.getIDLName(), strategy(methodIndex), init(methodIndex));
            methodIndex++;
        }
    }
    final OperationAnalysis[] ops = interfaceAnalysis.getOperations();
    for (int i = 0; i < ops.length; i++) {
        generateMethodCode(asm, superclass, ops[i].getMethod(), ops[i].getIDLName(), strategy(methodIndex), init(methodIndex));
        methodIndex++;
    }
    // Generate the constructor
    final ClassMethod ctor = asm.addMethod(Modifier.PUBLIC, "<init>", "V");
    ctor.getCodeAttribute().aload(0);
    ctor.getCodeAttribute().invokespecial(superclass.getName(), "<init>", "()V");
    ctor.getCodeAttribute().returnInstruction();
    // Generate the method _ids(), declared as abstract in ObjectImpl
    final String[] ids = interfaceAnalysis.getAllTypeIds();
    asm.addField(Modifier.PRIVATE + Modifier.STATIC, ID_FIELD_NAME, String[].class);
    final CodeAttribute idMethod = asm.addMethod(Modifier.PUBLIC + Modifier.FINAL, "_ids", "[Ljava/lang/String;").getCodeAttribute();
    idMethod.getstatic(stubClassName, ID_FIELD_NAME, "[Ljava/lang/String;");
    idMethod.returnInstruction();
    // Generate the static initializer
    final CodeAttribute clinit = asm.addMethod(Modifier.STATIC, "<clinit>", "V").getCodeAttribute();
    clinit.iconst(ids.length);
    clinit.anewarray(String.class.getName());
    for (int i = 0; i < ids.length; i++) {
        clinit.dup();
        clinit.iconst(i);
        clinit.ldc(ids[i]);
        clinit.aastore();
    }
    clinit.putstatic(stubClassName, ID_FIELD_NAME, "[Ljava/lang/String;");
    // last methodIndex + 1
    int n = methodIndex;
    for (methodIndex = 0; methodIndex < n; methodIndex++) {
        clinit.invokestatic(stubClassName, init(methodIndex), "()V");
    }
    clinit.returnInstruction();
    return asm;
}
Also used : ClassFile(org.jboss.classfilewriter.ClassFile) CodeAttribute(org.jboss.classfilewriter.code.CodeAttribute) AttributeAnalysis(org.wildfly.iiop.openjdk.rmi.AttributeAnalysis) ClassMethod(org.jboss.classfilewriter.ClassMethod) OperationAnalysis(org.wildfly.iiop.openjdk.rmi.OperationAnalysis)

Aggregations

ClassMethod (org.jboss.classfilewriter.ClassMethod)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClassFile (org.jboss.classfilewriter.ClassFile)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 CodeAttribute (org.jboss.classfilewriter.code.CodeAttribute)1 AttributeAnalysis (org.wildfly.iiop.openjdk.rmi.AttributeAnalysis)1 OperationAnalysis (org.wildfly.iiop.openjdk.rmi.OperationAnalysis)1