Exemplo n.º 1
0
 protected void visitListOfExpressions(List<? extends Expression> list) {
   if (list == null) return;
   for (Expression expression : list) {
     if (expression instanceof SpreadExpression) {
       Expression spread = ((SpreadExpression) expression).getExpression();
       spread.visit(this);
     } else {
       expression.visit(this);
     }
   }
 }
 protected void visitListOfExpressions(List<? extends Expression> list) {
   if (list == null) return;
   for (Expression expression : list) {
     if (expression instanceof SpreadExpression) {
       Expression spread = ((SpreadExpression) expression).getExpression();
       spread.visit(this);
     } else {
       // GRECLIPSE: start: could be null for malformed code (GRE290)
       if (expression != null)
         // end
         expression.visit(this);
     }
   }
 }
  private void validateFields(BlockStatement block) {
    Validation.Option mode =
        getEnumMemberValue(
            getAnnotation(annotatedClass, VALIDATION_ANNOTATION),
            "option",
            Validation.Option.class,
            Validation.Option.IGNORE_UNMARKED);
    for (FieldNode fieldNode : annotatedClass.getFields()) {
      if (shouldFieldBeIgnoredForValidation(fieldNode)) continue;

      ClosureExpression validationClosure =
          createGroovyTruthClosureExpression(block.getVariableScope());
      String message = null;

      AnnotationNode validateAnnotation = getAnnotation(fieldNode, VALIDATE_ANNOTATION);
      if (validateAnnotation != null) {
        message =
            getMemberStringValue(
                validateAnnotation, "message", "'" + fieldNode.getName() + "' must be set!");
        Expression member = validateAnnotation.getMember("value");
        if (member instanceof ClassExpression) {
          ClassNode memberType = member.getType();
          if (memberType.equals(ClassHelper.make(Validate.Ignore.class))) continue;
          else if (!memberType.equals(ClassHelper.make(Validate.GroovyTruth.class))) {
            addError(
                "value of Validate must be either Validate.GroovyTruth, Validate.Ignore or a closure.",
                fieldNode);
          }
        } else if (member instanceof ClosureExpression) {
          validationClosure = (ClosureExpression) member;
        }
      }

      if (validateAnnotation != null || mode == Validation.Option.VALIDATE_UNMARKED) {
        block.addStatement(
            new AssertStatement(
                new BooleanExpression(
                    callX(validationClosure, "call", args(varX(fieldNode.getName())))),
                message == null ? ConstantExpression.NULL : new ConstantExpression(message)));
      }
    }
  }