use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project grails-core by grails.
the class DefaultASTValidateableHelper method addValidateMethod.
protected void addValidateMethod(final ClassNode classNode) {
String fieldsToValidateParameterName = "$fieldsToValidate";
final MethodNode listArgValidateMethod = classNode.getMethod(VALIDATE_METHOD_NAME, new Parameter[] { new Parameter(new ClassNode(List.class), fieldsToValidateParameterName) });
if (listArgValidateMethod == null) {
final BlockStatement validateMethodCode = new BlockStatement();
final ArgumentListExpression validateInstanceArguments = new ArgumentListExpression();
validateInstanceArguments.addExpression(new VariableExpression("this"));
validateInstanceArguments.addExpression(new VariableExpression(fieldsToValidateParameterName, ClassHelper.LIST_TYPE));
final ClassNode validationSupportClassNode = ClassHelper.make(ValidationSupport.class);
final StaticMethodCallExpression invokeValidateInstanceExpression = new StaticMethodCallExpression(validationSupportClassNode, "validateInstance", validateInstanceArguments);
validateMethodCode.addStatement(new ExpressionStatement(invokeValidateInstanceExpression));
final Parameter fieldsToValidateParameter = new Parameter(new ClassNode(List.class), fieldsToValidateParameterName);
classNode.addMethod(new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, new Parameter[] { fieldsToValidateParameter }, EMPTY_CLASS_ARRAY, validateMethodCode));
}
final MethodNode noArgValidateMethod = classNode.getMethod(VALIDATE_METHOD_NAME, ZERO_PARAMETERS);
if (noArgValidateMethod == null) {
final BlockStatement validateMethodCode = new BlockStatement();
final ArgumentListExpression validateInstanceArguments = new ArgumentListExpression();
validateInstanceArguments.addExpression(new CastExpression(new ClassNode(List.class), new ConstantExpression(null)));
final Expression callListArgValidateMethod = new MethodCallExpression(new VariableExpression("this"), VALIDATE_METHOD_NAME, validateInstanceArguments);
validateMethodCode.addStatement(new ReturnStatement(callListArgValidateMethod));
classNode.addMethod(new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, validateMethodCode));
}
}
use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project grails-core by grails.
the class DefaultASTValidateableHelper method addGetConstraintsMethod.
protected void addGetConstraintsMethod(final ClassNode classNode, boolean defaultNullable) {
final String getConstraintsMethodName = "getConstraints";
MethodNode getConstraintsMethod = classNode.getMethod(getConstraintsMethodName, ZERO_PARAMETERS);
if (getConstraintsMethod == null || !getConstraintsMethod.getDeclaringClass().equals(classNode)) {
final BooleanExpression isConstraintsPropertyNull = new BooleanExpression(new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), new ConstantExpression(null)));
final BlockStatement ifConstraintsPropertyIsNullBlockStatement = new BlockStatement();
final ArgumentListExpression getConstrainedPropertiesForClassArguments = new ArgumentListExpression();
getConstrainedPropertiesForClassArguments.addExpression(new VariableExpression("this"));
getConstrainedPropertiesForClassArguments.addExpression(new ConstantExpression(defaultNullable));
final Expression getConstraintsMethodCall = new StaticMethodCallExpression(ClassHelper.make(ValidationSupport.class), "getConstrainedPropertiesForClass", getConstrainedPropertiesForClassArguments);
final Expression initializeConstraintsFieldExpression = new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.EQUALS, 0, 0), getConstraintsMethodCall);
final Statement ifConstraintsPropertyIsNullStatement = new IfStatement(isConstraintsPropertyNull, ifConstraintsPropertyIsNullBlockStatement, new ExpressionStatement(new EmptyExpression()));
ifConstraintsPropertyIsNullBlockStatement.addStatement(new ExpressionStatement(initializeConstraintsFieldExpression));
if (!defaultNullable) {
final Map<String, ClassNode> propertiesToConstrain = getPropertiesToEnsureConstraintsFor(classNode);
for (final Map.Entry<String, ClassNode> entry : propertiesToConstrain.entrySet()) {
final String propertyName = entry.getKey();
final ClassNode propertyType = entry.getValue();
final String cpName = "$" + propertyName + "$constrainedProperty";
final ArgumentListExpression constrainedPropertyConstructorArgumentList = new ArgumentListExpression();
constrainedPropertyConstructorArgumentList.addExpression(new ClassExpression(classNode));
constrainedPropertyConstructorArgumentList.addExpression(new ConstantExpression(propertyName));
constrainedPropertyConstructorArgumentList.addExpression(new ClassExpression(propertyType));
final ConstructorCallExpression constrainedPropertyCtorCallExpression = new ConstructorCallExpression(new ClassNode(ConstrainedProperty.class), constrainedPropertyConstructorArgumentList);
final Expression declareConstrainedPropertyExpression = new DeclarationExpression(new VariableExpression(cpName, ClassHelper.OBJECT_TYPE), Token.newSymbol(Types.EQUALS, 0, 0), constrainedPropertyCtorCallExpression);
final ArgumentListExpression applyConstraintMethodArgumentList = new ArgumentListExpression();
applyConstraintMethodArgumentList.addExpression(new ConstantExpression(ConstrainedProperty.NULLABLE_CONSTRAINT));
applyConstraintMethodArgumentList.addExpression(new ConstantExpression(defaultNullable));
final Expression applyNullableConstraintMethodCallExpression = new MethodCallExpression(new VariableExpression(cpName), "applyConstraint", applyConstraintMethodArgumentList);
final ArgumentListExpression putMethodArgumentList = new ArgumentListExpression();
putMethodArgumentList.addExpression(new ConstantExpression(propertyName));
putMethodArgumentList.addExpression(new VariableExpression(cpName));
final MethodCallExpression addToConstraintsMapExpression = new MethodCallExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), "put", putMethodArgumentList);
final BlockStatement addNullableConstraintBlock = new BlockStatement();
addNullableConstraintBlock.addStatement(new ExpressionStatement(declareConstrainedPropertyExpression));
addNullableConstraintBlock.addStatement(new ExpressionStatement(applyNullableConstraintMethodCallExpression));
addNullableConstraintBlock.addStatement(new ExpressionStatement(addToConstraintsMapExpression));
final Expression constraintsMapContainsKeyExpression = new MethodCallExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME, ClassHelper.make(Map.class)), "containsKey", new ArgumentListExpression(new ConstantExpression(propertyName)));
final BooleanExpression ifPropertyIsAlreadyConstrainedExpression = new BooleanExpression(constraintsMapContainsKeyExpression);
final Statement ifPropertyIsAlreadyConstrainedStatement = new IfStatement(ifPropertyIsAlreadyConstrainedExpression, new ExpressionStatement(new EmptyExpression()), addNullableConstraintBlock);
ifConstraintsPropertyIsNullBlockStatement.addStatement(ifPropertyIsAlreadyConstrainedStatement);
}
}
final BlockStatement methodBlockStatement = new BlockStatement();
methodBlockStatement.addStatement(ifConstraintsPropertyIsNullStatement);
final Statement returnStatement = new ReturnStatement(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME));
methodBlockStatement.addStatement(returnStatement);
final MethodNode methodNode = new MethodNode(getConstraintsMethodName, Modifier.STATIC | Modifier.PUBLIC, new ClassNode(Map.class), ZERO_PARAMETERS, null, methodBlockStatement);
if (classNode.redirect() == null) {
classNode.addMethod(methodNode);
} else {
classNode.redirect().addMethod(methodNode);
}
}
}
use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project groovy-core by groovy.
the class ModuleNode method createStatementsClass.
protected ClassNode createStatementsClass() {
ClassNode classNode = getScriptClassDummy();
if (classNode.getName().endsWith("package-info")) {
return classNode;
}
handleMainMethodIfPresent(methods);
// return new Foo(new ShellContext(args)).run()
classNode.addMethod(new MethodNode("main", ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE.makeArray(), "args") }, ClassNode.EMPTY_ARRAY, new ExpressionStatement(new MethodCallExpression(new ClassExpression(ClassHelper.make(InvokerHelper.class)), "runScript", new ArgumentListExpression(new ClassExpression(classNode), new VariableExpression("args"))))));
MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
methodNode.setIsScriptBody();
classNode.addMethod(methodNode);
classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
Statement stmt;
// (like @BaseScript) that could change this. But this is cautious and anticipates possible compiler changes.
if (classNode.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR) != null) {
stmt = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(new VariableExpression("context"))));
} else {
// Fallback for non-standard base "script" classes with no context (Binding) constructor.
stmt = new ExpressionStatement(new MethodCallExpression(new VariableExpression("super"), "setBinding", new ArgumentListExpression(new VariableExpression("context"))));
}
classNode.addConstructor(ACC_PUBLIC, new Parameter[] { new Parameter(ClassHelper.make(Binding.class), "context") }, ClassNode.EMPTY_ARRAY, stmt);
for (MethodNode node : methods) {
int modifiers = node.getModifiers();
if ((modifiers & ACC_ABSTRACT) != 0) {
throw new RuntimeException("Cannot use abstract methods in a script, they are only available inside classes. Method: " + node.getName());
}
// br: the old logic seems to add static to all def f().... in a script, which makes enclosing
// inner classes (including closures) in a def function difficult. Comment it out.
node.setModifiers(modifiers);
classNode.addMethod(node);
}
return classNode;
}
use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project grails-core by grails.
the class LoggingTransformer method addLogField.
public static void addLogField(ClassNode classNode, String logName) {
FieldNode logVariable = new FieldNode(LOG_PROPERTY, Modifier.STATIC | Modifier.PRIVATE, new ClassNode(Logger.class), classNode, new MethodCallExpression(new ClassExpression(new ClassNode(LoggerFactory.class)), "getLogger", new ArgumentListExpression(new ConstantExpression(logName))));
classNode.addField(logVariable);
}
use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project groovy-core by groovy.
the class ListExpressionTransformer method transformRegularConstructor.
private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
// can be replaced with a direct constructor call
List<Expression> transformedArgs = transformArguments(expr);
ConstructorCallExpression cce = new ConstructorCallExpression(target.getDeclaringClass(), new ArgumentListExpression(transformedArgs));
cce.setSourcePosition(expr);
cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
return cce;
}
Aggregations