1package com.github.javaparser.ast.validator;
2
3import com.github.javaparser.ast.expr.Expression;
4import com.github.javaparser.ast.stmt.TryStmt;
5import com.github.javaparser.ast.type.UnionType;
6
7/**
8 * This validator validates according to Java 7 syntax rules.
9 */
10public class Java7Validator extends Java6Validator {
11    protected final SingleNodeTypeValidator<TryStmt> tryWithLimitedResources = new SingleNodeTypeValidator<>(TryStmt.class, (n, reporter) -> {
12        if (n.getCatchClauses().isEmpty()
13                && n.getResources().isEmpty()
14                && !n.getFinallyBlock().isPresent()) {
15            reporter.report(n, "Try has no finally, no catch, and no resources.");
16        }
17        for (Expression resource : n.getResources()) {
18            if (!resource.isVariableDeclarationExpr()) {
19                reporter.report(n, "Try with resources only supports variable declarations.");
20            }
21        }
22    });
23    protected final SingleNodeTypeValidator<UnionType> multiCatch = new SingleNodeTypeValidator<>(UnionType.class, (n, reporter) -> {
24        // Case "0 elements" is caught elsewhere.
25        if (n.getElements().size() == 1) {
26            reporter.report(n, "Union type (multi catch) must have at least two elements.");
27        }
28    });
29
30    public Java7Validator() {
31        super();
32        remove(genericsWithoutDiamondOperator);
33        replace(tryWithoutResources, tryWithLimitedResources);
34        remove(noStringsInSwitch);
35        remove(noBinaryIntegerLiterals);
36        remove(noUnderscoresInIntegerLiterals);
37        replace(noMultiCatch, multiCatch);
38    }
39}
40