Search in sources :

Example 96 with ClassPool

use of javassist.ClassPool in project restfulie-java by caelum.

the class DefaultEnhancer method enhanceResource.

public <T> Class enhanceResource(Class<T> originalType) {
    ClassPool pool = ClassPool.getDefault();
    if (pool.find(DefaultEnhancer.class.getName()) == null) {
        pool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
    }
    try {
        // TODO extract this enhancement to an interface and test it appart
        CtClass newType = pool.makeClass(generateNewUniqueClassName(originalType));
        newType.setSuperclass(pool.get(originalType.getName()));
        newType.addInterface(pool.get(Resource.class.getName()));
        enhanceLinks(newType);
        return newType.toClass();
    } catch (NotFoundException e) {
        throw new IllegalStateException("Unable to extend type " + originalType.getName(), e);
    } catch (CannotCompileException e) {
        throw new IllegalStateException("Unable to extend type " + originalType.getName(), e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException)

Example 97 with ClassPool

use of javassist.ClassPool in project dubbo by alibaba.

the class ClassGenerator method getClassPool.

public static ClassPool getClassPool(ClassLoader loader) {
    if (loader == null) {
        return ClassPool.getDefault();
    }
    ClassPool pool = POOL_MAP.get(loader);
    if (pool == null) {
        pool = new ClassPool(true);
        pool.appendClassPath(new CustomizedLoaderClassPath(loader));
        POOL_MAP.put(loader, pool);
    }
    return pool;
}
Also used : ClassPool(javassist.ClassPool)

Example 98 with ClassPool

use of javassist.ClassPool in project ignite by apache.

the class BinaryObjectToStringTest method newExtInstance1.

/**
 */
private Object newExtInstance1() throws Exception {
    ClassPool classPool = new ClassPool(ClassPool.getDefault());
    CtClass aClass = classPool.makeClass("ExternalTestClass1");
    aClass.addInterface(classPool.get("java.io.Externalizable"));
    aClass.addField(CtField.make("private int x;", aClass));
    aClass.addConstructor(CtNewConstructor.make("public ExternalTestClass1() {}", aClass));
    aClass.addConstructor(CtNewConstructor.make("public ExternalTestClass1(int x0) { x = x0; }", aClass));
    aClass.addMethod(CtNewMethod.make("public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException { out.writeInt(x); }", aClass));
    aClass.addMethod(CtNewMethod.make("public void readExternal(java.io.ObjectInput in) throws java.io.IOException { x = in.readInt(); }", aClass));
    ClassLoader extClsLdr = new ClassLoader() {

        {
            byte[] bytecode = aClass.toBytecode();
            defineClass("ExternalTestClass1", bytecode, 0, bytecode.length);
        }
    };
    Class<?> extClass = extClsLdr.loadClass("ExternalTestClass1");
    Constructor<?> ctor = extClass.getConstructor(int.class);
    return ctor.newInstance(42);
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool)

Example 99 with ClassPool

use of javassist.ClassPool in project ignite by apache.

the class BinaryObjectToStringTest method newExtInstance2.

/**
 */
private Object newExtInstance2() throws Exception {
    ClassPool classPool = new ClassPool(ClassPool.getDefault());
    CtClass aClass = classPool.makeClass("ExternalTestClass2");
    aClass.addInterface(classPool.get("java.io.Serializable"));
    aClass.addField(CtField.make("private int x;", aClass));
    aClass.addConstructor(CtNewConstructor.make("public ExternalTestClass2() {}", aClass));
    aClass.addConstructor(CtNewConstructor.make("public ExternalTestClass2(int x0) { x = x0; }", aClass));
    aClass.addMethod(CtNewMethod.make("private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { out.writeInt(x); }", aClass));
    aClass.addMethod(CtNewMethod.make("private void readObject(java.io.ObjectInputStream in) throws java.io.IOException { x = in.readInt(); }", aClass));
    ClassLoader extClsLdr = new ClassLoader() {

        {
            byte[] bytecode = aClass.toBytecode();
            defineClass("ExternalTestClass2", bytecode, 0, bytecode.length);
        }
    };
    Class<?> extClass = extClsLdr.loadClass("ExternalTestClass2");
    Constructor<?> ctor = extClass.getConstructor(int.class);
    return ctor.newInstance(42);
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool)

Example 100 with ClassPool

use of javassist.ClassPool in project ignite by apache.

the class ConfigVariationsTestSuiteBuilder method makeTestClass.

/**
 * Creates test class for given config variation.
 *
 * @param pkg Package to put class in.
 * @param cfg Config variation.
 * @return Test class.
 */
private Class<?> makeTestClass(String pkg, VariationsTestsConfig cfg) {
    int idx = cntr.getAndIncrement();
    String clsName = cls.getSimpleName() + "_" + idx;
    cfgs.put(clsName, cfg);
    ClassPool cp = ClassPool.getDefault();
    cp.insertClassPath(new ClassClassPath(ConfigVariationsTestSuiteBuilder.class));
    CtClass cl = cp.makeClass(pkg + "." + clsName);
    try {
        cl.setSuperclass(cp.get(cls.getName()));
        cl.addConstructor(CtNewConstructor.make("public " + clsName + "() { " + "this.testsCfg = " + ConfigVariationsTestSuiteBuilder.class.getName() + ".getCfg(\"" + clsName + "\"); " + "}", cl));
        return cl.toClass();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) ClassClassPath(javassist.ClassClassPath)

Aggregations

ClassPool (javassist.ClassPool)120 CtClass (javassist.CtClass)93 CtMethod (javassist.CtMethod)48 NotFoundException (javassist.NotFoundException)40 CannotCompileException (javassist.CannotCompileException)28 IOException (java.io.IOException)23 LoaderClassPath (javassist.LoaderClassPath)21 CtField (javassist.CtField)20 CtConstructor (javassist.CtConstructor)17 Test (org.junit.Test)17 ClassFile (javassist.bytecode.ClassFile)15 File (java.io.File)13 Method (java.lang.reflect.Method)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ConstPool (javassist.bytecode.ConstPool)12 FileNotFoundException (java.io.FileNotFoundException)11 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)9 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)9 ClassClassPath (javassist.ClassClassPath)7 MethodInfo (javassist.bytecode.MethodInfo)7