Alexander Reelsen
@Entity public User extends Model {
public String name;
public String password;
public void setPassword(String pwd) {
password = Crypto.passwordHash(pwd);
}
public boolean hasPassword(String pwd) {
return Crypto.passwordHash(pwd).equals(password);
}
}
public static void setPassword(String nw, String old) {
User user = User.findById((Long)session.get("id"));
notFoundIfNull(user);
if (!user.hasPassword(old)) {
index();
}
user.password = nw;
user.save();
render(user);
}
user.password = nw;?play.classloading.enhancers.PropertiesEnhancerString code =
"public String getName() { return this.name; }";
CtMethod getMethod = CtMethod.make(code, ctClass);
ctClass.addMethod(getMethod);
code = "public void setName(String value) " +
"{ this.name = value; }";
CtMethod setMethod = CtMethod.make(code, ctClass);
ctClass.addMethod(setMethod);
ExprEditor from javassist is used, can be used to replace calls inside a method body, instead of replacing the method itselfFieldAccessor.invokeReadProperty() or FieldAccessor.invokeWriteProperty(), which calls either getter or setter
User.find("byName", "alexander"); work?Model.find() is a static methodUser > Model > GenericModel > JPABasepublic static JPAQuery find(String query, Object... params) {
throw new UnsupportedOperationException("Please
annotate your JPA model...");
} play.db.jpa.JPAEnhancer// public static play.db.jpa.GenericModel.JPAQuery
// find(String query, Object[] params) {
// return play.db.jpa.JPQL.instance.find(entityName,
// query, params);
// };
CtMethod find = CtMethod.make(code, ctClass);
ctClass.addMethod(find);
play.db.jpa.JPAEnhancercount(), count(query, params)findAll(), findById(id), find(query, params), findOneBy(query, params)deleteAll(), delete(query, params)create()javap -verbose -c User
public static void redirect() {
index();
}
curl -v localhost:9000/redirect
< HTTP/1.1 302 Found
< Location: http://localhost:9000/
play.classloading.enhancers.ControllersEnhancerif(!play.classloading.enhancers.ControllersEnhancer.
ControllerInstrumentation.isActionCallAllowed()) {
play.mvc.Controller.redirect(
"controllers.Application.index", $args);
return;
}
play.classloading.enhancers.ControllersEnhancer.
ControllerInstrumentation.stopActionCall();
if (!ControllersEnhancer.
ControllerInstrumentation.isActionCallAllowed()) {
Controller.redirect("controllers.Application.index",
new Object[0]);
} else {
ControllersEnhancer.ControllerInstrumentation
.stopActionCall();
// controller logic comes here...
}
List<User> users = User.findAll();
String message = "FooBarBaz";
render(users, message);
<div>${message}</div>
#{list users, as:'user'}
<li>${user.name}</li>
#{/list}
play.classloading.enhancers.LocalvariablesNamesEnhancer
LocalVariablesNamesTracer.localVariables render() method is checked with this localVariables variableList<User> users = User.findAll();
LocalVariablesNamesTracer.addVariable("users", users);protected static void renderTemplate(String templateName,
Object... args) {
Map templateBinding = new HashMap(16);
for (Object o : args) {
List names =
LocalVariablesNamesTracer.getAllLocalVariableNames(o);
for (String name : names) {
templateBinding.put(name, o);
}
}
renderTemplate(templateName, templateBinding);
}

