Search in sources :

Example 1 with ClassFile

use of org.jboss.classfilewriter.ClassFile 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 2 with ClassFile

use of org.jboss.classfilewriter.ClassFile 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

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