hasDefaultConstructor = true;
break;
}
}
if (!hasDefaultConstructor && !ctClass.isInterface()) {
CtConstructor defaultConstructor = CtNewConstructor.make("public " + ctClass.getSimpleName() + "() {}", ctClass);
ctClass.addConstructor(defaultConstructor);
}
} catch (Exception e) {
Logger.error(e, "Error in PropertiesEnhancer");
throw new UnexpectedException("Error in PropertiesEnhancer", e);
}
if (isScala(applicationClass)) {
// Temporary hack for Scala. Done.
applicationClass.enhancedByteCode = ctClass.toBytecode();
ctClass.defrost();
return;
}
for (CtField ctField : ctClass.getDeclaredFields()) {
try {
if (isProperty(ctField)) {
// Property name
String propertyName = ctField.getName().substring(0, 1).toUpperCase() + ctField.getName().substring(1);
String getter = "get" + propertyName;
String setter = "set" + propertyName;
try {
CtMethod ctMethod = ctClass.getDeclaredMethod(getter);
if (ctMethod.getParameterTypes().length > 0 || Modifier.isStatic(ctMethod.getModifiers())) {
throw new NotFoundException("it's not a getter !");
}
} catch (NotFoundException noGetter) {
// Créé le getter
String code = "public " + ctField.getType().getName() + " " + getter + "() { return this." + ctField.getName() + "; }";
CtMethod getMethod = CtMethod.make(code, ctClass);
getMethod.setModifiers(getMethod.getModifiers() | AccessFlag.SYNTHETIC);
ctClass.addMethod(getMethod);
}
try {
CtMethod ctMethod = ctClass.getDeclaredMethod(setter);
if (ctMethod.getParameterTypes().length != 1 || !ctMethod.getParameterTypes()[0].equals(ctField.getType()) || Modifier.isStatic(ctMethod.getModifiers())) {
throw new NotFoundException("it's not a setter !");
}
} catch (NotFoundException noSetter) {
// Créé le setter
CtMethod setMethod = CtMethod.make("public void " + setter + "(" + ctField.getType().getName() + " value) { this." + ctField.getName() + " = value; }", ctClass);
setMethod.setModifiers(setMethod.getModifiers() | AccessFlag.SYNTHETIC);
ctClass.addMethod(setMethod);
createAnnotation(getAnnotations(setMethod), PlayPropertyAccessor.class);
}
}
} catch (Exception e) {
Logger.error(e, "Error in PropertiesEnhancer");
throw new UnexpectedException("Error in PropertiesEnhancer", e);
}
}
// Add a default constructor if needed
try {
boolean hasDefaultConstructor = false;
for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
if (constructor.getParameterTypes().length == 0) {
hasDefaultConstructor = true;
break;
}
}
if (!hasDefaultConstructor) {
CtConstructor defaultConstructor = CtNewConstructor.defaultConstructor(ctClass);
ctClass.addConstructor(defaultConstructor);
}
} catch (Exception e) {
Logger.error(e, "Error in PropertiesEnhancer");
throw new UnexpectedException("Error in PropertiesEnhancer", e);