Search in sources :

Example 6 with CtField

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);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod)

Example 7 with CtField

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);
}
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 8 with CtField

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();
}
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 9 with CtField

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);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) CannotCompileException(javassist.CannotCompileException)

Example 10 with CtField

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);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

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