Search in sources :

Example 26 with CtClass

use of javassist.CtClass in project play-cookbook by spinscale.

the class XmlEnhancer method enhanceThisClass.

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
    CtClass ctClass = makeClass(applicationClass);
    if (!ctClass.subtypeOf(classPool.get("play.db.jpa.JPABase"))) {
        return;
    }
    if (!hasAnnotation(ctClass, "javax.persistence.Entity")) {
        return;
    }
    ConstPool constpool = ctClass.getClassFile().getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlAccessorType")) {
        Annotation annot = new Annotation("javax.xml.bind.annotation.XmlAccessorType", constpool);
        EnumMemberValue enumValue = new EnumMemberValue(constpool);
        enumValue.setType("javax.xml.bind.annotation.XmlAccessType");
        enumValue.setValue("FIELD");
        annot.addMemberValue("value", enumValue);
        attr.addAnnotation(annot);
        ctClass.getClassFile().addAttribute(attr);
    }
    if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlRootElement")) {
        Annotation annot = new Annotation("javax.xml.bind.annotation.XmlRootElement", constpool);
        String entityName = ctClass.getName();
        String entity = entityName.substring(entityName.lastIndexOf('.') + 1).toLowerCase();
        annot.addMemberValue("name", new StringMemberValue(entity, constpool));
        attr.addAnnotation(annot);
        ctClass.getClassFile().addAttribute(attr);
    }
    applicationClass.enhancedByteCode = ctClass.toBytecode();
    ctClass.defrost();
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(javassist.bytecode.annotation.Annotation)

Example 27 with CtClass

use of javassist.CtClass in project afterburner by stephanenicolas.

the class AfterBurner method extractExistingConstructors.

private List<CtConstructor> extractExistingConstructors(final InsertableConstructor insertableConstructor) throws NotFoundException, AfterBurnerImpossibleException {
    List<CtConstructor> constructors = new ArrayList<CtConstructor>();
    CtConstructor[] declaredConstructors = insertableConstructor.getClassToInsertInto().getDeclaredConstructors();
    for (CtConstructor constructor : declaredConstructors) {
        CtClass[] paramClasses = constructor.getParameterTypes();
        if (insertableConstructor.acceptParameters(paramClasses)) {
            constructors.add(constructor);
        }
    }
    return constructors;
}
Also used : CtClass(javassist.CtClass) ArrayList(java.util.ArrayList) CtConstructor(javassist.CtConstructor)

Example 28 with CtClass

use of javassist.CtClass in project afterburner by stephanenicolas.

the class CtMethodJavaWriter method extractThrowClause.

private String extractThrowClause(CtMethod overridenMethod) throws NotFoundException {
    int indexException = 0;
    StringBuilder builder = new StringBuilder();
    for (CtClass paramType : overridenMethod.getExceptionTypes()) {
        builder.append(paramType.getName());
        if (indexException < overridenMethod.getExceptionTypes().length - 1) {
            builder.append(", ");
        }
        indexException++;
    }
    if (builder.length() != 0) {
        builder.insert(0, " throws ");
    }
    return builder.toString();
}
Also used : CtClass(javassist.CtClass)

Example 29 with CtClass

use of javassist.CtClass in project afterburner by stephanenicolas.

the class CtMethodJavaWriter method extractParametersAndTypes.

private String extractParametersAndTypes(CtMethod overridenMethod) throws NotFoundException {
    StringBuilder builder = new StringBuilder();
    int indexParam = 0;
    for (CtClass paramType : overridenMethod.getParameterTypes()) {
        builder.append(paramType.getName());
        builder.append(" ");
        builder.append("p" + indexParam);
        if (indexParam < overridenMethod.getParameterTypes().length - 1) {
            builder.append(", ");
        }
        indexParam++;
    }
    return builder.toString();
}
Also used : CtClass(javassist.CtClass)

Example 30 with CtClass

use of javassist.CtClass in project afterburner by stephanenicolas.

the class InsertableConstructorBuilderTest method testCheckAllFields_should_throw_exceptions_if_no_body_defined.

@Test(expected = AfterBurnerImpossibleException.class)
public void testCheckAllFields_should_throw_exceptions_if_no_body_defined() throws AfterBurnerImpossibleException {
    //GIVEN
    CtClass classToInsertInto = CtClass.intType;
    builder.insertIntoClass(classToInsertInto);
    //WHEN
    builder.checkFields();
    //THEN
    fail("Should have thrown exception");
}
Also used : CtClass(javassist.CtClass) Test(org.junit.Test)

Aggregations

CtClass (javassist.CtClass)113 CtMethod (javassist.CtMethod)42 NotFoundException (javassist.NotFoundException)33 ClassPool (javassist.ClassPool)32 Test (org.junit.Test)32 CannotCompileException (javassist.CannotCompileException)22 CtField (javassist.CtField)20 IOException (java.io.IOException)10 CtConstructor (javassist.CtConstructor)8 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 CtMethodJavaWriter (com.github.stephanenicolas.afterburner.inserts.CtMethodJavaWriter)4 FileNotFoundException (java.io.FileNotFoundException)4 LoaderClassPath (javassist.LoaderClassPath)4 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)4 CodeAttribute (javassist.bytecode.CodeAttribute)4 ConstPool (javassist.bytecode.ConstPool)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 NamedClassPool (com.navercorp.pinpoint.profiler.instrument.classpool.NamedClassPool)3