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);
}
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations