use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project grails-core by grails.
the class ControllerActionTransformer method getCodeToHandleAllowedMethods.
protected BlockStatement getCodeToHandleAllowedMethods(ClassNode controllerClass, String methodName) {
GrailsASTUtils.addEnhancedAnnotation(controllerClass, DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY);
final BlockStatement checkAllowedMethodsBlock = new BlockStatement();
final PropertyExpression requestPropertyExpression = new PropertyExpression(new VariableExpression("this"), "request");
final FieldNode allowedMethodsField = controllerClass.getField(DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY);
if (allowedMethodsField != null) {
final Expression initialAllowedMethodsExpression = allowedMethodsField.getInitialExpression();
if (initialAllowedMethodsExpression instanceof MapExpression) {
boolean actionIsRestricted = false;
final MapExpression allowedMethodsMapExpression = (MapExpression) initialAllowedMethodsExpression;
final List<MapEntryExpression> allowedMethodsMapEntryExpressions = allowedMethodsMapExpression.getMapEntryExpressions();
for (MapEntryExpression allowedMethodsMapEntryExpression : allowedMethodsMapEntryExpressions) {
final Expression allowedMethodsMapEntryKeyExpression = allowedMethodsMapEntryExpression.getKeyExpression();
if (allowedMethodsMapEntryKeyExpression instanceof ConstantExpression) {
final ConstantExpression allowedMethodsMapKeyConstantExpression = (ConstantExpression) allowedMethodsMapEntryKeyExpression;
final Object allowedMethodsMapKeyValue = allowedMethodsMapKeyConstantExpression.getValue();
if (methodName.equals(allowedMethodsMapKeyValue)) {
actionIsRestricted = true;
break;
}
}
}
if (actionIsRestricted) {
final PropertyExpression responsePropertyExpression = new PropertyExpression(new VariableExpression("this"), "response");
final ArgumentListExpression isAllowedArgumentList = new ArgumentListExpression();
isAllowedArgumentList.addExpression(new ConstantExpression(methodName));
isAllowedArgumentList.addExpression(new PropertyExpression(new VariableExpression("this"), "request"));
isAllowedArgumentList.addExpression(new PropertyExpression(new VariableExpression("this"), DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY));
final Expression isAllowedMethodCall = new StaticMethodCallExpression(ClassHelper.make(AllowedMethodsHelper.class), "isAllowed", isAllowedArgumentList);
final BooleanExpression isValidRequestMethod = new BooleanExpression(isAllowedMethodCall);
final MethodCallExpression sendErrorMethodCall = new MethodCallExpression(responsePropertyExpression, "sendError", new ConstantExpression(HttpServletResponse.SC_METHOD_NOT_ALLOWED));
final ReturnStatement returnStatement = new ReturnStatement(new ConstantExpression(null));
final BlockStatement blockToSendError = new BlockStatement();
blockToSendError.addStatement(new ExpressionStatement(sendErrorMethodCall));
blockToSendError.addStatement(returnStatement);
final IfStatement ifIsValidRequestMethodStatement = new IfStatement(isValidRequestMethod, new ExpressionStatement(new EmptyExpression()), blockToSendError);
checkAllowedMethodsBlock.addStatement(ifIsValidRequestMethodStatement);
}
}
}
final ArgumentListExpression argumentListExpression = new ArgumentListExpression();
argumentListExpression.addExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME));
argumentListExpression.addExpression(new ConstantExpression(methodName));
final Expression setAttributeMethodCall = new MethodCallExpression(requestPropertyExpression, "setAttribute", argumentListExpression);
final BlockStatement codeToExecuteIfAttributeIsNotSet = new BlockStatement();
codeToExecuteIfAttributeIsNotSet.addStatement(new ExpressionStatement(setAttributeMethodCall));
codeToExecuteIfAttributeIsNotSet.addStatement(checkAllowedMethodsBlock);
final BooleanExpression attributeIsSetBooleanExpression = new BooleanExpression(new MethodCallExpression(requestPropertyExpression, "getAttribute", new ArgumentListExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME))));
final Statement ifAttributeIsAlreadySetStatement = new IfStatement(attributeIsSetBooleanExpression, new EmptyStatement(), codeToExecuteIfAttributeIsNotSet);
final BlockStatement code = new BlockStatement();
code.addStatement(ifAttributeIsAlreadySetStatement);
return code;
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression 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);
MethodNode methodNode = new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, new Parameter[] { fieldsToValidateParameter }, EMPTY_CLASS_ARRAY, validateMethodCode);
classNode.addMethod(methodNode);
AnnotatedNodeUtils.markAsGenerated(classNode, methodNode);
}
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));
MethodNode methodNode = new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, validateMethodCode);
classNode.addMethod(methodNode);
AnnotatedNodeUtils.markAsGenerated(classNode, methodNode);
}
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression 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);
AnnotatedNodeUtils.markAsGenerated(classNode, methodNode);
} else {
classNode.redirect().addMethod(methodNode);
AnnotatedNodeUtils.markAsGenerated(classNode.redirect(), methodNode);
}
}
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project hale by halestudio.
the class VertexEntityTransformation method buildInitGraphMethod.
/**
* Create a static method that initializes a graph. For an
* {@link OrientGraph} it registers the entity class as a schema type.
*
* @param entityName the entity name
* @param superEntityName the super entity name, may be <code>null</code>
* @return the method
*/
private MethodNode buildInitGraphMethod(Expression entityName, Expression superEntityName) {
// graph (parameter)
VariableExpression graph = new VariableExpression("graph");
if (superEntityName == null) {
superEntityName = new ConstantExpression(null);
}
BlockStatement code = new BlockStatement();
// register class
code.addStatement(new ExpressionStatement(new StaticMethodCallExpression(VE_DELEGATES_CLASS, VertexEntityDelegates.METHOD_REGISTER_CLASS, new ArgumentListExpression(graph, entityName, superEntityName))));
return new MethodNode("initGraph", Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(GRAPH_CLASS, "graph") }, new ClassNode[0], code);
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project hale by halestudio.
the class VertexEntityTransformation method buildFindByMethod.
/**
* Create a static method to retrieve objects by property value.
*
* @param clazz the entity class node
* @param entityName the entity name
* @param typeProperty the name of the property holding the entity name in a
* vertex
* @param propertyName the property name
* @param propertyType the property type
* @return the method
*/
private MethodNode buildFindByMethod(ClassNode clazz, Expression entityName, Expression typeProperty, String propertyName, ClassNode propertyType) {
clazz = ClassHelper.make(clazz.getName());
propertyType = ClassHelper.make(propertyType.getName());
ClassNode returnType = ITERABLE_CLASS.getPlainNodeReference();
// add generic type argument
returnType.setGenericsTypes(new GenericsType[] { new GenericsType(clazz) });
String methodName = "findBy" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
BlockStatement code = new BlockStatement();
// initialize graph
code.addStatement(callInitGraph(clazz, new VariableExpression("graph")));
/*
* def vertices = VertexEntityDelegates.findByDelegate(graph,
* entityName, typeProperty, propertyName, value)
*/
VariableExpression vertices = new VariableExpression("vertices");
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new VariableExpression("graph"));
args.addExpression(entityName);
args.addExpression(typeProperty);
args.addExpression(new ConstantExpression(propertyName));
args.addExpression(new VariableExpression("value"));
code.addStatement(AbstractASTTransformUtil.declStatement(vertices, new StaticMethodCallExpression(VE_DELEGATES_CLASS, VertexEntityDelegates.METHOD_FIND_BY, args)));
/*
* return new IterableDelegate(vertices, EntityClass, graph)
*/
ArgumentListExpression createDelegateArgs = new ArgumentListExpression();
createDelegateArgs.addExpression(vertices);
createDelegateArgs.addExpression(new ClassExpression(clazz));
createDelegateArgs.addExpression(new VariableExpression("graph"));
code.addStatement(new ReturnStatement(new ConstructorCallExpression(VE_ITERABLE_DELEGATE_CLASS, createDelegateArgs)));
return new MethodNode(methodName, Modifier.STATIC | Modifier.PUBLIC, returnType, new Parameter[] { new Parameter(GRAPH_CLASS, "graph"), new Parameter(propertyType, "value") }, new ClassNode[0], code);
}
Aggregations