Search in sources :

Example 16 with StaticMethodCallExpression

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;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) AllowedMethodsHelper(grails.artefact.controller.support.AllowedMethodsHelper) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 17 with StaticMethodCallExpression

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);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 18 with StaticMethodCallExpression

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);
        }
    }
}
Also used : ValidationSupport(org.grails.web.plugins.support.ValidationSupport) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ConstrainedProperty(grails.gorm.validation.ConstrainedProperty) ClassNode(org.codehaus.groovy.ast.ClassNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with StaticMethodCallExpression

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);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 20 with StaticMethodCallExpression

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);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) GenericsType(org.codehaus.groovy.ast.GenericsType) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)30 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)20 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)20 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)18 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)15 Expression (org.codehaus.groovy.ast.expr.Expression)15 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)13 Statement (org.codehaus.groovy.ast.stmt.Statement)13 MethodNode (org.codehaus.groovy.ast.MethodNode)12 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)12 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)11 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)11 ClassNode (org.codehaus.groovy.ast.ClassNode)10 Parameter (org.codehaus.groovy.ast.Parameter)10 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)10 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)7 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)7 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)7 FieldNode (org.codehaus.groovy.ast.FieldNode)6