use of javassist.CtField in project afterburner by stephanenicolas.
the class AfterBurnerTest method testInsertConstructor.
@Test
public void testInsertConstructor() 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;", true);
// WHEN
afterBurner.insertConstructor(insertableConstructor);
// THEN
targetClass = target.toClass();
targetInstance = targetClass.newInstance();
assertHasFooFieldWithValue(target, 2);
}
use of javassist.CtField in project afterburner by stephanenicolas.
the class AfterBurnerTest method testInsertMethod_before.
@Test
public void testInsertMethod_before() 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", "bar", null, "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 testInsertMethod_not_before_not_after.
@Test(expected = AfterBurnerImpossibleException.class)
public void testInsertMethod_not_before_not_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, null, "foo = 2;", null);
// WHEN
afterBurner.addOrInsertMethod(insertableMethod);
// THEN
fail();
}
use of javassist.CtField in project afterburner by stephanenicolas.
the class AfterBurnerTest method assertHasFooFieldWithValue.
private void assertHasFooFieldWithValue(CtClass clazz, int value) throws Exception {
CtField fooField = clazz.getField("foo");
assertNotNull(fooField);
CtClass fooFieldType = fooField.getType();
assertEquals(CtClass.intType, fooFieldType);
Field realFooField = targetInstance.getClass().getDeclaredField("foo");
realFooField.setAccessible(true);
assertEquals(value, realFooField.get(targetInstance));
}
use of javassist.CtField in project pinpoint by naver.
the class JavassistClass method addField0.
private void addField0(String accessorTypeName, String initValExp) throws InstrumentException {
try {
Class<?> accessorType = pluginContext.injectClass(classLoader, accessorTypeName);
final AccessorAnalyzer accessorAnalyzer = new AccessorAnalyzer();
final AccessorDetails accessorDetails = accessorAnalyzer.analyze(accessorType);
Class<?> fieldType = accessorDetails.getFieldType();
String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(fieldType.getName());
final CtField newField = CtField.make("private " + fieldTypeName + " " + FIELD_PREFIX + JavaAssistUtils.javaClassNameToVariableName(accessorTypeName) + ";", ctClass);
if (initValExp == null) {
ctClass.addField(newField);
} else {
ctClass.addField(newField, initValExp);
}
final CtClass accessorInterface = getCtClass(accessorTypeName);
ctClass.addInterface(accessorInterface);
CtMethod getterMethod = CtNewMethod.getter(accessorDetails.getGetter().getName(), newField);
ctClass.addMethod(getterMethod);
CtMethod setterMethod = CtNewMethod.setter(accessorDetails.getSetter().getName(), newField);
ctClass.addMethod(setterMethod);
} catch (Exception e) {
throw new InstrumentException("Failed to add field with accessor [" + accessorTypeName + "]. Cause:" + e.getMessage(), e);
}
}
Aggregations