Search in sources :

Example 6 with ClassGen

use of org.apache.bcel.generic.ClassGen in project qpid-broker-j by apache.

the class LDAPSSLSocketFactoryGenerator method createSubClassByteCode.

/**
 * Creates the LDAPSocketFactoryImpl class (subclass of {@link AbstractLDAPSSLSocketFactory}.
 * A static method #getDefaulta, a static field _sslContent and no-arg constructor are added
 * to the class.
 *
 * @param className
 *
 * @return byte code
 */
private static byte[] createSubClassByteCode(final String className) {
    ClassGen classGen = new ClassGen(className, AbstractLDAPSSLSocketFactory.class.getName(), "<generated>", ACC_PUBLIC | ACC_SUPER, null);
    ConstantPoolGen constantPoolGen = classGen.getConstantPool();
    InstructionFactory factory = new InstructionFactory(classGen);
    createSslContextStaticField(classGen, constantPoolGen);
    createGetDefaultStaticMethod(classGen, constantPoolGen, factory);
    classGen.addEmptyConstructor(ACC_PROTECTED);
    JavaClass javaClass = classGen.getJavaClass();
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        javaClass.dump(out);
        return out.toByteArray();
    } catch (IOException ioex) {
        throw new IllegalStateException("Could not write to a ByteArrayOutputStream - should not happen", ioex);
    } finally {
        closeSafely(out);
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) JavaClass(org.apache.bcel.classfile.JavaClass) ClassGen(org.apache.bcel.generic.ClassGen) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InstructionFactory(org.apache.bcel.generic.InstructionFactory)

Aggregations

ClassGen (org.apache.bcel.generic.ClassGen)6 JavaClass (org.apache.bcel.classfile.JavaClass)5 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)3 InstructionFactory (org.apache.bcel.generic.InstructionFactory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Random (java.util.Random)2 BranchInstruction (org.apache.bcel.generic.BranchInstruction)2 INSTANCEOF (org.apache.bcel.generic.INSTANCEOF)2 InstructionHandle (org.apache.bcel.generic.InstructionHandle)2 InstructionList (org.apache.bcel.generic.InstructionList)2 MethodGen (org.apache.bcel.generic.MethodGen)2 ObjectType (org.apache.bcel.generic.ObjectType)2 Type (org.apache.bcel.generic.Type)2 CallString (com.jopdesign.common.code.CallString)1 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ClassParser (org.apache.bcel.classfile.ClassParser)1