@Override public void visit(BlockStatement statement) { List<Statement> statements = processSequence(statement.getBody()); eliminateRedundantBreaks(statements, statement); BlockCountVisitor usageCounter = new BlockCountVisitor(statement); usageCounter.visit(statements); if (usageCounter.getCount() == 0) { SequentialStatement result = new SequentialStatement(); result.getSequence().addAll(statements); resultStmt = result; } else { statement.getBody().clear(); statement.getBody().addAll(statements); resultStmt = statement; } }
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))); } } }
private void eliminateRedundantBreaks(List<Statement> statements, IdentifiedStatement exit) { if (statements.isEmpty()) { return; } Statement last = statements.get(statements.size() - 1); if (last instanceof BreakStatement && exit != null) { IdentifiedStatement target = ((BreakStatement) last).getTarget(); if (exit == target) { statements.remove(statements.size() - 1); } } if (statements.isEmpty()) { return; } for (int i = 0; i < statements.size(); ++i) { Statement stmt = statements.get(i); if (stmt instanceof ConditionalStatement) { ConditionalStatement cond = (ConditionalStatement) stmt; check_conditional: { last = cond.getConsequent().isEmpty() ? null : cond.getConsequent().get(cond.getConsequent().size() - 1); if (last instanceof BreakStatement) { BreakStatement breakStmt = (BreakStatement) last; if (exit != null && exit == breakStmt.getTarget()) { cond.getConsequent().remove(cond.getConsequent().size() - 1); List<Statement> remaining = statements.subList(i + 1, statements.size()); cond.getAlternative().addAll(remaining); remaining.clear(); break check_conditional; } } last = cond.getAlternative().isEmpty() ? null : cond.getAlternative().get(cond.getAlternative().size() - 1); if (last instanceof BreakStatement) { BreakStatement breakStmt = (BreakStatement) last; if (exit != null && exit == breakStmt.getTarget()) { cond.getAlternative().remove(cond.getAlternative().size() - 1); List<Statement> remaining = statements.subList(i + 1, statements.size()); cond.getConsequent().addAll(remaining); remaining.clear(); } } } if (i == statements.size() - 1) { eliminateRedundantBreaks(cond.getConsequent(), exit); eliminateRedundantBreaks(cond.getAlternative(), exit); } normalizeConditional(cond); if (cond.getConsequent().size() == 1 && cond.getConsequent().get(0) instanceof ConditionalStatement) { ConditionalStatement innerCond = (ConditionalStatement) cond.getConsequent().get(0); if (innerCond.getAlternative().isEmpty()) { if (cond.getAlternative().isEmpty()) { cond.getConsequent().clear(); cond.getConsequent().addAll(innerCond.getConsequent()); cond.setCondition( Expr.binary( BinaryOperation.AND, cond.getCondition(), innerCond.getCondition(), cond.getCondition().getLocation())); --i; } else if (cond.getAlternative().size() != 1 || !(cond.getAlternative().get(0) instanceof ConditionalStatement)) { cond.setCondition(ExprOptimizer.invert(cond.getCondition())); cond.getConsequent().clear(); cond.getConsequent().addAll(cond.getAlternative()); cond.getAlternative().clear(); cond.getAlternative().add(innerCond); --i; } } } } else if (stmt instanceof BlockStatement) { BlockStatement nestedBlock = (BlockStatement) stmt; eliminateRedundantBreaks(nestedBlock.getBody(), nestedBlock); } else if (stmt instanceof WhileStatement) { WhileStatement whileStmt = (WhileStatement) stmt; eliminateRedundantBreaks(whileStmt.getBody(), null); } else if (stmt instanceof SwitchStatement) { SwitchStatement switchStmt = (SwitchStatement) stmt; for (SwitchClause clause : switchStmt.getClauses()) { eliminateRedundantBreaks(clause.getBody(), null); } eliminateRedundantBreaks(switchStmt.getDefaultClause(), null); } } }
private void validateCustomMethods(BlockStatement block) { if (!annotatedClass.hasMethod("doValidate", Parameter.EMPTY_ARRAY)) return; block.addStatement(stmt(callX(varX("this"), "doValidate"))); }