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();
}
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;
}
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();
}
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();
}
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");
}
Aggregations