JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/validator/Java1_0Validator.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.ImportDeclaration;
25import com.github.javaparser.ast.Node;
26import com.github.javaparser.ast.body.AnnotationDeclaration;
27import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
28import com.github.javaparser.ast.body.EnumDeclaration;
29import com.github.javaparser.ast.body.Parameter;
30import com.github.javaparser.ast.expr.*;
31import com.github.javaparser.ast.modules.ModuleDeclaration;
32import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
33import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
34import com.github.javaparser.ast.stmt.*;
35import com.github.javaparser.ast.type.UnionType;
36import com.github.javaparser.ast.validator.chunks.CommonValidators;
37import com.github.javaparser.ast.validator.chunks.ModifierValidator;
38import com.github.javaparser.ast.validator.chunks.NoBinaryIntegerLiteralsValidator;
39import com.github.javaparser.ast.validator.chunks.NoUnderscoresInIntegerLiteralsValidator;
40
41/**
42 * This validator validates according to Java 1.0 syntax rules.
43 */
44public class Java1_0Validator extends Validators {
45    final Validator modifiersWithoutStrictfpAndDefaultAndStaticInterfaceMethodsAndPrivateInterfaceMethods
46            = new ModifierValidator(falsefalsefalse);
47    final Validator noAssertKeyword = new SimpleValidator<>(AssertStmt.class,
48            n -> true,
49            (nreporter) -> reporter.report(n"'assert' keyword is not supported.")
50    );
51    final Validator noInnerClasses = new SimpleValidator<>(ClassOrInterfaceDeclaration.class,
52            n -> !n.isTopLevelType(),
53            (nreporter) -> reporter.report(n"inner classes or interfaces are not supported.")
54    );
55    final Validator noReflection = new SimpleValidator<>(ClassExpr.class,
56            n -> true,
57            (nreporter) -> reporter.report(n"Reflection is not supported.")
58    );
59    final Validator noGenerics = new TreeVisitorValidator((nodereporter) -> {
60        if (node instanceof NodeWithTypeArguments) {
61            if (((NodeWithTypeArguments<? extends Node>) node).getTypeArguments().isPresent()) {
62                reporter.report(node"Generics are not supported.");
63            }
64        }
65        if (node instanceof NodeWithTypeParameters) {
66            if (((NodeWithTypeParameters<? extends Node>) node).getTypeParameters().isNonEmpty()) {
67                reporter.report(node"Generics are not supported.");
68            }
69        }
70    });
71    final SingleNodeTypeValidator<TryStmttryWithoutResources = new SingleNodeTypeValidator<>(TryStmt.class, (nreporter) -> {
72        if (n.getCatchClauses().isEmpty() && !n.getFinallyBlock().isPresent()) {
73            reporter.report(n"Try has no finally and no catch.");
74        }
75        if (n.getResources().isNonEmpty()) {
76            reporter.report(n"Catch with resource is not supported.");
77        }
78    });
79    final Validator noAnnotations = new TreeVisitorValidator((nodereporter) -> {
80        if (node instanceof AnnotationExpr || node instanceof AnnotationDeclaration) {
81            reporter.report(node"Annotations are not supported.");
82        }
83    });
84    final Validator noEnums = new SimpleValidator<>(EnumDeclaration.class,
85            n -> true,
86            (nreporter) -> reporter.report(n"Enumerations are not supported.")
87    );
88    final Validator noVarargs = new SimpleValidator<>(Parameter.class,
89            Parameter::isVarArgs,
90            (nreporter) -> reporter.report(n"Varargs are not supported.")
91    );
92    final Validator noForEach = new SimpleValidator<>(ForEachStmt.class,
93            n -> true,
94            (nreporter) -> reporter.report(n"For-each loops are not supported.")
95    );
96    final Validator noStaticImports = new SimpleValidator<>(ImportDeclaration.class,
97            ImportDeclaration::isStatic,
98            (nreporter) -> reporter.report(n"Static imports are not supported.")
99    );
100    final Validator onlyOneLabelInSwitchCase = new SimpleValidator<>(SwitchEntry.class,
101            n -> n.getLabels().size() > 1,
102            (nreporter) -> reporter.report(n.getLabels().getParentNode().get(), "Only one label allowed in a switch-case.")
103    );
104    final Validator noYield = new SimpleValidator<>(YieldStmt.class,
105            n -> true,
106            (nreporter) -> reporter.report(n"Only labels allowed in break statements.")
107    );
108    final Validator noBinaryIntegerLiterals = new NoBinaryIntegerLiteralsValidator();
109    final Validator noUnderscoresInIntegerLiterals = new NoUnderscoresInIntegerLiteralsValidator();
110    final Validator noMultiCatch = new SimpleValidator<>(UnionType.class,
111            n -> true,
112            (nreporter) -> reporter.report(n"Multi-catch is not supported.")
113    );
114    final Validator noLambdas = new SimpleValidator<>(LambdaExpr.class,
115            n -> true,
116            (nreporter) -> reporter.report(n"Lambdas are not supported.")
117    );
118    final Validator noModules = new SimpleValidator<>(ModuleDeclaration.class,
119            n -> true,
120            (nreporter) -> reporter.report(n"Modules are not supported.")
121    );
122    final Validator noSwitchExpressions = new SimpleValidator<>(SwitchExpr.class,
123            n -> true,
124            (nreporter) -> reporter.report(n"Switch expressions are not supported.")
125    );
126
127    public Java1_0Validator() {
128        super(new CommonValidators());
129        add(modifiersWithoutStrictfpAndDefaultAndStaticInterfaceMethodsAndPrivateInterfaceMethods);
130        add(noAssertKeyword);
131        add(noInnerClasses);
132        add(noReflection);
133        add(noGenerics);
134        add(tryWithoutResources);
135        add(noAnnotations);
136        add(noEnums);
137        add(noVarargs);
138        add(noForEach);
139        add(noStaticImports);
140        add(noYield);
141        add(onlyOneLabelInSwitchCase);
142        add(noBinaryIntegerLiterals);
143        add(noUnderscoresInIntegerLiterals);
144        add(noMultiCatch);
145        add(noLambdas);
146        add(noModules);
147        add(noSwitchExpressions);
148    }
149}
150
MembersX
Java1_0Validator:modifiersWithoutStrictfpAndDefaultAndStaticInterfaceMethodsAndPrivateInterfaceMethods
Java1_0Validator:noBinaryIntegerLiterals
Java1_0Validator:noForEach
Java1_0Validator:noModules
Java1_0Validator:noGenerics
Java1_0Validator:noUnderscoresInIntegerLiterals
Java1_0Validator:noLambdas
Java1_0Validator:noInnerClasses
Java1_0Validator:noYield
Java1_0Validator:noReflection
Java1_0Validator:noAssertKeyword
Java1_0Validator:noVarargs
Java1_0Validator:noStaticImports
Java1_0Validator:onlyOneLabelInSwitchCase
Java1_0Validator:tryWithoutResources
Java1_0Validator:Java1_0Validator
Java1_0Validator:noEnums
Java1_0Validator:noAnnotations
Java1_0Validator:noMultiCatch
Java1_0Validator:noSwitchExpressions
Members
X