}
for (final CtMethod ctMethod : ctClass.getDeclaredMethods()) {
// Threaded access
ctMethod.instrument(new ExprEditor() {
@Override
public void edit(FieldAccess fieldAccess) throws CannotCompileException {
try {
if (isThreadedFieldAccess(fieldAccess.getField())) {
if (fieldAccess.isReader()) {
fieldAccess.replace("$_ = ($r)play.utils.Java.invokeStatic($type, \"current\");");
}
}
} catch (Exception e) {
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (fieldAccess %s).", applicationClass.name, ctMethod.getName(), fieldAccess);
throw new UnexpectedException(e);
}
}
});
// Auto-redirect
boolean isHandler = false;
for (Annotation a : getAnnotations(ctMethod).getAnnotations()) {
if (a.getTypeName().startsWith("play.mvc.")) {
isHandler = true;
break;
}
if (a.getTypeName().endsWith("$ByPass")) {
isHandler = true;
break;
}
}
// Perhaps it is a scala-generated method ?
if (ctMethod.getName().contains("$")) {
isHandler = true;
} else {
if (ctClass.getName().endsWith("$") && ctMethod.getParameterTypes().length == 0) {
try {
ctClass.getField(ctMethod.getName());
isHandler = true;
} catch (NotFoundException e) {
// ok
}
}
}
if (isScalaObject(ctClass)) {
// Auto reverse -->
if (Modifier.isPublic(ctMethod.getModifiers()) && ((ctClass.getName().endsWith("$") && !ctMethod.getName().contains("$default$"))) && !isHandler) {
try {
ctMethod.insertBefore(
"if(play.mvc.Controller._currentReverse.get() != null) {"
+ "play.mvc.Controller.redirect(\"" + ctClass.getName().replace("$", "") + "." + ctMethod.getName() + "\", $args);"
+ generateValidReturnStatement(ctMethod.getReturnType())
+ "}");
ctMethod.insertBefore(
"((java.util.Stack)play.classloading.enhancers.ControllersEnhancer.currentAction.get()).push(\"" + ctClass.getName().replace("$", "") + "." + ctMethod.getName() + "\");");
ctMethod.insertAfter(
"((java.util.Stack)play.classloading.enhancers.ControllersEnhancer.currentAction.get()).pop();", true);
} catch (Exception e) {
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (auto-reverse).", applicationClass.name, ctMethod.getName());
throw new UnexpectedException(e);
}
}
} else {
// Auto redirect -->
if (Modifier.isPublic(ctMethod.getModifiers()) && Modifier.isStatic(ctMethod.getModifiers()) && ctMethod.getReturnType().equals(CtClass.voidType) && !isHandler) {
try {
ctMethod.insertBefore(
"if(!play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation.isActionCallAllowed()) {"
+ "play.mvc.Controller.redirect(\"" + ctClass.getName().replace("$", "") + "." + ctMethod.getName() + "\", $args);"
+ generateValidReturnStatement(ctMethod.getReturnType()) + "}"
+ "play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation.stopActionCall();");
} catch (Exception e) {
Logger.error(e, "Error in ControllersEnhancer. %s.%s has not been properly enhanced (auto-redirect).", applicationClass.name, ctMethod.getName());
throw new UnexpectedException(e);
}
}
}
// Enhance global catch to avoid potential unwanted catching of play.mvc.results.Result
ctMethod.instrument(new ExprEditor() {
@Override
public void edit(Handler handler) throws CannotCompileException {
StringBuilder code = new StringBuilder();
try {