use of org.codehaus.groovy.ast.MethodNode in project grails-core by grails.
the class ASTValidationErrorsHelper method addGetErrorsMethod.
protected void addGetErrorsMethod(final ClassNode paramTypeClassNode) {
final ASTNode getErrorsMethod = paramTypeClassNode.getMethod(GET_ERRORS_METHOD_NAME, GrailsArtefactClassInjector.ZERO_PARAMETERS);
if (getErrorsMethod == null) {
final BlockStatement getErrorsMethodCode = new BlockStatement();
final Expression initErrorsMethodCallExpression = new MethodCallExpression(new VariableExpression("this"), INIT_ERRORS_METHOD_NAME, EMPTY_TUPLE);
getErrorsMethodCode.addStatement(new ExpressionStatement(initErrorsMethodCallExpression));
final Statement returnStatement = new ReturnStatement(ERRORS_EXPRESSION);
getErrorsMethodCode.addStatement(returnStatement);
paramTypeClassNode.addMethod(new MethodNode(GET_ERRORS_METHOD_NAME, Modifier.PUBLIC, ERRORS_CLASS_NODE, GrailsArtefactClassInjector.ZERO_PARAMETERS, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, getErrorsMethodCode));
}
}
use of org.codehaus.groovy.ast.MethodNode in project grails-core by grails.
the class ASTValidationErrorsHelper method addClearErrorsMethod.
protected void addClearErrorsMethod(final ClassNode paramTypeClassNode) {
final ASTNode clearErrorsMethod = paramTypeClassNode.getMethod(CLEAR_ERRORS_METHOD_NAME, GrailsArtefactClassInjector.ZERO_PARAMETERS);
if (clearErrorsMethod == null) {
final BlockStatement clearErrorsMethodCode = new BlockStatement();
Expression nullOutErrorsFieldExpression = new BinaryExpression(ERRORS_EXPRESSION, EQUALS_SYMBOL, NULL_EXPRESSION);
clearErrorsMethodCode.addStatement(new ExpressionStatement(nullOutErrorsFieldExpression));
paramTypeClassNode.addMethod(new MethodNode(CLEAR_ERRORS_METHOD_NAME, Modifier.PUBLIC, ClassHelper.VOID_TYPE, GrailsArtefactClassInjector.ZERO_PARAMETERS, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, clearErrorsMethodCode));
}
}
use of org.codehaus.groovy.ast.MethodNode in project grails-core by grails.
the class ASTValidationErrorsHelper method addHasErrorsMethod.
protected void addHasErrorsMethod(final ClassNode paramTypeClassNode) {
final ASTNode getErrorsMethod = paramTypeClassNode.getMethod(HAS_ERRORS_METHOD_NAME, GrailsArtefactClassInjector.ZERO_PARAMETERS);
if (getErrorsMethod == null) {
final BlockStatement hasErrorsMethodCode = new BlockStatement();
final Expression initErrorsMethodCallExpression = new MethodCallExpression(new VariableExpression("this"), INIT_ERRORS_METHOD_NAME, EMPTY_TUPLE);
hasErrorsMethodCode.addStatement(new ExpressionStatement(initErrorsMethodCallExpression));
final Statement returnStatement = new ReturnStatement(new BooleanExpression(new MethodCallExpression(ERRORS_EXPRESSION, HAS_ERRORS_METHOD_NAME, EMPTY_TUPLE)));
hasErrorsMethodCode.addStatement(returnStatement);
paramTypeClassNode.addMethod(new MethodNode(HAS_ERRORS_METHOD_NAME, Modifier.PUBLIC, new ClassNode(Boolean.class), GrailsArtefactClassInjector.ZERO_PARAMETERS, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, hasErrorsMethodCode));
}
}
use of org.codehaus.groovy.ast.MethodNode in project grails-core by grails.
the class ASTValidationErrorsHelper method addInitErrorsMethod.
protected void addInitErrorsMethod(final ClassNode paramTypeClassNode) {
final ASTNode initErrorsMethod = paramTypeClassNode.getMethod(INIT_ERRORS_METHOD_NAME, GrailsArtefactClassInjector.ZERO_PARAMETERS);
if (initErrorsMethod == null) {
final BlockStatement initErrorsMethodCode = new BlockStatement();
final BinaryExpression errorsIsNullExpression = new BinaryExpression(ERRORS_EXPRESSION, Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), NULL_EXPRESSION);
Expression beanPropertyBindingResultConstructorArgs = new ArgumentListExpression(new VariableExpression("this"), new ConstantExpression(paramTypeClassNode.getName()));
final Statement newEvaluatorExpression = new ExpressionStatement(new BinaryExpression(ERRORS_EXPRESSION, EQUALS_SYMBOL, new ConstructorCallExpression(new ClassNode(ValidationErrors.class), beanPropertyBindingResultConstructorArgs)));
final Statement initErrorsIfNullStatement = new IfStatement(new BooleanExpression(errorsIsNullExpression), newEvaluatorExpression, new ExpressionStatement(new EmptyExpression()));
initErrorsMethodCode.addStatement(initErrorsIfNullStatement);
paramTypeClassNode.addMethod(new MethodNode(INIT_ERRORS_METHOD_NAME, Modifier.PRIVATE, ClassHelper.VOID_TYPE, GrailsArtefactClassInjector.ZERO_PARAMETERS, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, initErrorsMethodCode));
}
}
use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.
the class VetoableASTTransformation method needsVetoableChangeSupport.
/**
* Snoops through the declaring class and all parents looking for a field
* of type VetoableChangeSupport. Remembers the field and returns false
* if found otherwise returns true to indicate that such support should
* be added.
*
* @param declaringClass the class to search
* @return true if vetoable change support should be added
*/
protected boolean needsVetoableChangeSupport(ClassNode declaringClass, SourceUnit sourceUnit) {
boolean foundAdd = false, foundRemove = false, foundFire = false;
ClassNode consideredClass = declaringClass;
while (consideredClass != null) {
for (MethodNode method : consideredClass.getMethods()) {
// just check length, MOP will match it up
foundAdd = foundAdd || method.getName().equals("addVetoableChangeListener") && method.getParameters().length == 1;
foundRemove = foundRemove || method.getName().equals("removeVetoableChangeListener") && method.getParameters().length == 1;
foundFire = foundFire || method.getName().equals("fireVetoableChange") && method.getParameters().length == 3;
if (foundAdd && foundRemove && foundFire) {
return false;
}
}
consideredClass = consideredClass.getSuperClass();
}
// check if a super class has @Vetoable annotations
consideredClass = declaringClass.getSuperClass();
while (consideredClass != null) {
if (hasVetoableAnnotation(consideredClass))
return false;
for (FieldNode field : consideredClass.getFields()) {
if (hasVetoableAnnotation(field))
return false;
}
consideredClass = consideredClass.getSuperClass();
}
if (foundAdd || foundRemove || foundFire) {
sourceUnit.getErrorCollector().addErrorAndContinue(new SimpleMessage("@Vetoable cannot be processed on " + declaringClass.getName() + " because some but not all of addVetoableChangeListener, removeVetoableChange, and fireVetoableChange were declared in the current or super classes.", sourceUnit));
return false;
}
return true;
}
Aggregations