use of org.grails.web.databinding.DefaultASTDatabindingHelper in project grails-core by grails.
the class ControllerActionTransformer method initializeAndValidateCommandObjectParameter.
protected void initializeAndValidateCommandObjectParameter(final BlockStatement wrapper, final ClassNode controllerNode, final ClassNode commandObjectNode, final ASTNode actionNode, final String actionName, final String paramName, final SourceUnit source, final GeneratorContext context) {
final DeclarationExpression declareCoExpression = new DeclarationExpression(new VariableExpression(paramName, commandObjectNode), Token.newSymbol(Types.EQUALS, 0, 0), new EmptyExpression());
wrapper.addStatement(new ExpressionStatement(declareCoExpression));
if (commandObjectNode.isInterface() || Modifier.isAbstract(commandObjectNode.getModifiers())) {
final String warningMessage = "The [" + actionName + "] action in [" + controllerNode.getName() + "] accepts a parameter of type [" + commandObjectNode.getName() + "]. Interface types and abstract class types are not supported as command objects. This parameter will be ignored.";
GrailsASTUtils.warning(source, actionNode, warningMessage);
} else {
initializeCommandObjectParameter(wrapper, commandObjectNode, paramName, source);
@SuppressWarnings("unchecked") boolean argumentIsValidateable = GrailsASTUtils.hasAnyAnnotations(commandObjectNode, grails.persistence.Entity.class, javax.persistence.Entity.class) || commandObjectNode.implementsInterface(ClassHelper.make(Validateable.class));
if (!argumentIsValidateable && commandObjectNode.isPrimaryClassNode()) {
final ModuleNode commandObjectModule = commandObjectNode.getModule();
if (commandObjectModule != null && this.compilationUnit != null) {
if (commandObjectModule == controllerNode.getModule() || doesModulePathIncludeSubstring(commandObjectModule, "grails-app" + File.separator + "controllers" + File.separator)) {
TraitInjectionUtils.injectTrait(compilationUnit, source, commandObjectNode, Validateable.class);
List<ConstructorNode> declaredConstructors = commandObjectNode.getDeclaredConstructors();
List<Statement> objectInitializerStatements = commandObjectNode.getObjectInitializerStatements();
if (declaredConstructors.isEmpty() && !objectInitializerStatements.isEmpty()) {
BlockStatement constructorLogic = new BlockStatement();
ConstructorNode constructorNode = new ConstructorNode(Modifier.PUBLIC, constructorLogic);
commandObjectNode.addConstructor(constructorNode);
constructorLogic.addStatements(objectInitializerStatements);
}
argumentIsValidateable = true;
} else if (doesModulePathIncludeSubstring(commandObjectModule, "grails-app" + File.separator + "domain" + File.separator)) {
argumentIsValidateable = true;
}
}
}
if (argumentIsValidateable) {
final MethodCallExpression validateMethodCallExpression = new MethodCallExpression(new VariableExpression(paramName), "validate", EMPTY_TUPLE);
final MethodNode validateMethod = commandObjectNode.getMethod("validate", new Parameter[0]);
if (validateMethod != null) {
validateMethodCallExpression.setMethodTarget(validateMethod);
}
final Statement ifCommandObjectIsNotNullThenValidate = new IfStatement(new BooleanExpression(new VariableExpression(paramName)), new ExpressionStatement(validateMethodCallExpression), new ExpressionStatement(new EmptyExpression()));
wrapper.addStatement(ifCommandObjectIsNotNullThenValidate);
} else {
// try to dynamically invoke the .validate() method if it is available at runtime...
final Expression respondsToValidateMethodCallExpression = new MethodCallExpression(new VariableExpression(paramName), "respondsTo", new ArgumentListExpression(new ConstantExpression("validate")));
final Expression validateMethodCallExpression = new MethodCallExpression(new VariableExpression(paramName), "validate", new ArgumentListExpression());
final Statement ifRespondsToValidateThenValidateStatement = new IfStatement(new BooleanExpression(respondsToValidateMethodCallExpression), new ExpressionStatement(validateMethodCallExpression), new ExpressionStatement(new EmptyExpression()));
final Statement ifCommandObjectIsNotNullThenValidate = new IfStatement(new BooleanExpression(new VariableExpression(paramName)), ifRespondsToValidateThenValidateStatement, new ExpressionStatement(new EmptyExpression()));
wrapper.addStatement(ifCommandObjectIsNotNullThenValidate);
final String warningMessage = "The [" + actionName + "] action accepts a parameter of type [" + commandObjectNode.getName() + "] which does not implement grails.validation.Validateable. Data binding will still be applied " + "to this command object but the instance will not be validateable.";
GrailsASTUtils.warning(source, actionNode, warningMessage);
}
if (GrailsASTUtils.isInnerClassNode(commandObjectNode)) {
final String warningMessage = "The [" + actionName + "] action accepts a parameter of type [" + commandObjectNode.getName() + "] which is an inner class. Command object classes should not be inner classes.";
GrailsASTUtils.warning(source, actionNode, warningMessage);
} else {
new DefaultASTDatabindingHelper().injectDatabindingCode(source, context, commandObjectNode);
}
}
}
use of org.grails.web.databinding.DefaultASTDatabindingHelper in project grails-core by grails.
the class ControllerDomainTransformer method performInjection.
@Override
public void performInjection(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) {
super.performInjection(source, context, classNode);
new DefaultASTDatabindingHelper().injectDatabindingCode(source, context, classNode);
}
Aggregations