JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/validator/Java7Validator.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.ast.validator;
23
24import com.github.javaparser.ast.expr.Expression;
25import com.github.javaparser.ast.stmt.SwitchEntry;
26import com.github.javaparser.ast.stmt.TryStmt;
27import com.github.javaparser.ast.type.UnionType;
28
29/**
30 * This validator validates according to Java 7 syntax rules.
31 */
32public class Java7Validator extends Java6Validator {
33    final SingleNodeTypeValidator<TryStmttryWithLimitedResources = new SingleNodeTypeValidator<>(TryStmt.class, (nreporter) -> {
34        if (n.getCatchClauses().isEmpty()
35                && n.getResources().isEmpty()
36                && !n.getFinallyBlock().isPresent()) {
37            reporter.report(n"Try has no finally, no catch, and no resources.");
38        }
39        for (Expression resource : n.getResources()) {
40            if (!resource.isVariableDeclarationExpr()) {
41                reporter.report(n"Try with resources only supports variable declarations.");
42            }
43        }
44    });
45    private final SingleNodeTypeValidator<UnionTypemultiCatch = new SingleNodeTypeValidator<>(UnionType.class, (nreporter) -> {
46        // Case "0 elements" is caught elsewhere.
47        if (n.getElements().size() == 1) {
48            reporter.report(n"Union type (multi catch) must have at least two elements.");
49        }
50    });
51
52    public Java7Validator() {
53        super();
54        remove(genericsWithoutDiamondOperator);
55        replace(tryWithoutResourcestryWithLimitedResources);
56        remove(noBinaryIntegerLiterals);
57        remove(noUnderscoresInIntegerLiterals);
58        replace(noMultiCatchmultiCatch);
59    }
60}
61
MembersX
Java7Validator:tryWithLimitedResources
Java7Validator:multiCatch
Java7Validator:Java7Validator
Members
X