Search in sources :

Example 41 with CtField

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);
}
Also used : CtField(javassist.CtField) InsertableConstructor(com.github.stephanenicolas.afterburner.inserts.InsertableConstructor) SimpleInsertableConstructor(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor) SimpleInsertableConstructor(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableConstructor) Test(org.junit.Test)

Example 42 with CtField

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);
}
Also used : SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) CtField(javassist.CtField) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) Test(org.junit.Test)

Example 43 with CtField

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();
}
Also used : SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) InsertableMethod(com.github.stephanenicolas.afterburner.inserts.InsertableMethod) CtField(javassist.CtField) SimpleInsertableMethod(com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod) Test(org.junit.Test)

Example 44 with CtField

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));
}
Also used : CtClass(javassist.CtClass) Field(java.lang.reflect.Field) CtField(javassist.CtField) CtField(javassist.CtField)

Example 45 with CtField

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);
    }
}
Also used : AccessorDetails(com.navercorp.pinpoint.profiler.instrument.AccessorAnalyzer.AccessorDetails) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException)

Aggregations

CtField (javassist.CtField)76 CtClass (javassist.CtClass)47 CtMethod (javassist.CtMethod)27 CannotCompileException (javassist.CannotCompileException)24 NotFoundException (javassist.NotFoundException)22 ClassPool (javassist.ClassPool)20 CtConstructor (javassist.CtConstructor)15 Test (org.junit.Test)12 ClassFile (javassist.bytecode.ClassFile)9 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ConstPool (javassist.bytecode.ConstPool)4 SMethod (org.bimserver.shared.meta.SMethod)4 SParameter (org.bimserver.shared.meta.SParameter)4 InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)3 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3