use of javassist.CtField in project java-chassis by ServiceComb.
the class JavassistUtils method createClass.
public static Class<?> createClass(ClassLoader classLoader, ClassConfig config) {
if (classLoader == null) {
classLoader = Thread.currentThread().getContextClassLoader();
}
appendThreadClassPath();
CtClass ctClass = null;
if (config.isIntf()) {
ctClass = POOL.makeInterface(config.getClassName());
} else {
ctClass = POOL.makeClass(config.getClassName());
}
try {
for (String intfName : config.getIntfList()) {
ctClass.addInterface(POOL.get(intfName));
}
for (FieldConfig fieldConfig : config.getFieldList()) {
CtField field = createCtField(POOL, ctClass, fieldConfig);
ctClass.addField(field);
}
for (MethodConfig methodConfig : config.getMethodList()) {
CtMethod ctMethod = CtMethod.make(methodConfig.getSource(), ctClass);
if (methodConfig.getGenericSignature() != null) {
ctMethod.setGenericSignature(methodConfig.getGenericSignature());
}
ctClass.addMethod(ctMethod);
}
return ctClass.toClass(classLoader, null);
} catch (Throwable e) {
throw new Error(e);
}
}
use of javassist.CtField in project afterburner by stephanenicolas.
the class AfterBurnerTest method testInsertMethod_after.
@Test
public void testInsertMethod_after() throws Exception {
// GIVEN
target.addMethod(CtNewMethod.make("public void bar() { }", target));
target.addMethod(CtNewMethod.make("public boolean foo() { bar(); return false; }", target));
target.addField(new CtField(CtClass.intType, "foo", target));
InsertableMethod insertableMethod = new SimpleInsertableMethod(target, "foo", null, "bar", "foo = 2;", null);
// WHEN
afterBurner.addOrInsertMethod(insertableMethod);
// THEN
targetClass = target.toClass();
targetInstance = targetClass.newInstance();
assertHasFooMethodWithReturnValue(target, false);
assertHasFooFieldWithValue(target, 2);
}
use of javassist.CtField in project afterburner by stephanenicolas.
the class AfterBurnerTest method testInsertConstructor_with_no_constructor.
@Test(expected = AfterBurnerImpossibleException.class)
public void testInsertConstructor_with_no_constructor() throws Exception {
// GIVEN
target.addConstructor(CtNewConstructor.make("public Target() {}", target));
target.addField(new CtField(CtClass.intType, "foo", target));
InsertableConstructor insertableConstructor = new SimpleInsertableConstructor(target, "foo = 2;", false);
// WHEN
afterBurner.insertConstructor(insertableConstructor);
// THEN
fail();
}
use of javassist.CtField in project hibernate-orm by hibernate.
the class FieldWriter method addWithModifiers.
private static void addWithModifiers(CtClass target, CtClass type, String name, int modifiers, Class<?>... annotations) {
try {
final CtField f = new CtField(type, name, target);
f.setModifiers(f.getModifiers() | modifiers);
addAnnotations(f.getFieldInfo(), annotations);
target.addField(f);
} catch (CannotCompileException cce) {
final String msg = String.format("Could not enhance class [%s] to add field [%s]", target.getName(), name);
throw new EnhancementException(msg, cce);
}
}
use of javassist.CtField in project hibernate-orm by hibernate.
the class MethodWriter method addSetter.
public static CtMethod addSetter(CtClass target, String field, String name) {
CtField actualField = null;
try {
actualField = target.getField(field);
log.debugf("Writing setter method [%s] into [%s] for field [%s]", name, target.getName(), field);
CtMethod method = CtNewMethod.setter(name, actualField);
target.addMethod(method);
return method;
} catch (CannotCompileException cce) {
try {
// Fall back to create a getter from delegation.
CtMethod method = CtNewMethod.delegator(CtNewMethod.setter(name, actualField), target);
target.addMethod(method);
return method;
} catch (CannotCompileException ignored) {
String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
throw new EnhancementException(msg, cce);
}
} catch (NotFoundException nfe) {
String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
throw new EnhancementException(msg, nfe);
}
}
Aggregations