| 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 | |
| 22 | package com.github.javaparser.ast.validator; |
| 23 | |
| 24 | import com.github.javaparser.ast.ImportDeclaration; |
| 25 | import com.github.javaparser.ast.Node; |
| 26 | import com.github.javaparser.ast.body.AnnotationDeclaration; |
| 27 | import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
| 28 | import com.github.javaparser.ast.body.EnumDeclaration; |
| 29 | import com.github.javaparser.ast.body.Parameter; |
| 30 | import com.github.javaparser.ast.expr.*; |
| 31 | import com.github.javaparser.ast.modules.ModuleDeclaration; |
| 32 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; |
| 33 | import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters; |
| 34 | import com.github.javaparser.ast.stmt.*; |
| 35 | import com.github.javaparser.ast.type.UnionType; |
| 36 | import com.github.javaparser.ast.validator.chunks.CommonValidators; |
| 37 | import com.github.javaparser.ast.validator.chunks.ModifierValidator; |
| 38 | import com.github.javaparser.ast.validator.chunks.NoBinaryIntegerLiteralsValidator; |
| 39 | import com.github.javaparser.ast.validator.chunks.NoUnderscoresInIntegerLiteralsValidator; |
| 40 | |
| 41 | /** |
| 42 | * This validator validates according to Java 1.0 syntax rules. |
| 43 | */ |
| 44 | public class Java1_0Validator extends Validators { |
| 45 | final Validator modifiersWithoutStrictfpAndDefaultAndStaticInterfaceMethodsAndPrivateInterfaceMethods |
| 46 | = new ModifierValidator(false, false, false); |
| 47 | final Validator noAssertKeyword = new SimpleValidator<>(AssertStmt.class, |
| 48 | n -> true, |
| 49 | (n, reporter) -> reporter.report(n, "'assert' keyword is not supported.") |
| 50 | ); |
| 51 | final Validator noInnerClasses = new SimpleValidator<>(ClassOrInterfaceDeclaration.class, |
| 52 | n -> !n.isTopLevelType(), |
| 53 | (n, reporter) -> reporter.report(n, "inner classes or interfaces are not supported.") |
| 54 | ); |
| 55 | final Validator noReflection = new SimpleValidator<>(ClassExpr.class, |
| 56 | n -> true, |
| 57 | (n, reporter) -> reporter.report(n, "Reflection is not supported.") |
| 58 | ); |
| 59 | final Validator noGenerics = new TreeVisitorValidator((node, reporter) -> { |
| 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<TryStmt> tryWithoutResources = new SingleNodeTypeValidator<>(TryStmt.class, (n, reporter) -> { |
| 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((node, reporter) -> { |
| 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 | (n, reporter) -> reporter.report(n, "Enumerations are not supported.") |
| 87 | ); |
| 88 | final Validator noVarargs = new SimpleValidator<>(Parameter.class, |
| 89 | Parameter::isVarArgs, |
| 90 | (n, reporter) -> reporter.report(n, "Varargs are not supported.") |
| 91 | ); |
| 92 | final Validator noForEach = new SimpleValidator<>(ForEachStmt.class, |
| 93 | n -> true, |
| 94 | (n, reporter) -> reporter.report(n, "For-each loops are not supported.") |
| 95 | ); |
| 96 | final Validator noStaticImports = new SimpleValidator<>(ImportDeclaration.class, |
| 97 | ImportDeclaration::isStatic, |
| 98 | (n, reporter) -> reporter.report(n, "Static imports are not supported.") |
| 99 | ); |
| 100 | final Validator onlyOneLabelInSwitchCase = new SimpleValidator<>(SwitchEntry.class, |
| 101 | n -> n.getLabels().size() > 1, |
| 102 | (n, reporter) -> 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 | (n, reporter) -> 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 | (n, reporter) -> reporter.report(n, "Multi-catch is not supported.") |
| 113 | ); |
| 114 | final Validator noLambdas = new SimpleValidator<>(LambdaExpr.class, |
| 115 | n -> true, |
| 116 | (n, reporter) -> reporter.report(n, "Lambdas are not supported.") |
| 117 | ); |
| 118 | final Validator noModules = new SimpleValidator<>(ModuleDeclaration.class, |
| 119 | n -> true, |
| 120 | (n, reporter) -> reporter.report(n, "Modules are not supported.") |
| 121 | ); |
| 122 | final Validator noSwitchExpressions = new SimpleValidator<>(SwitchExpr.class, |
| 123 | n -> true, |
| 124 | (n, reporter) -> 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 |
Members