Home | History | Annotate | Download | only in validator
      1 package com.github.javaparser.ast.validator;
      2 
      3 import com.github.javaparser.ast.ImportDeclaration;
      4 import com.github.javaparser.ast.Node;
      5 import com.github.javaparser.ast.body.*;
      6 import com.github.javaparser.ast.expr.AnnotationExpr;
      7 import com.github.javaparser.ast.expr.ClassExpr;
      8 import com.github.javaparser.ast.expr.LambdaExpr;
      9 import com.github.javaparser.ast.expr.StringLiteralExpr;
     10 import com.github.javaparser.ast.modules.ModuleDeclaration;
     11 import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
     12 import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
     13 import com.github.javaparser.ast.stmt.AssertStmt;
     14 import com.github.javaparser.ast.stmt.ForeachStmt;
     15 import com.github.javaparser.ast.stmt.SwitchEntryStmt;
     16 import com.github.javaparser.ast.stmt.TryStmt;
     17 import com.github.javaparser.ast.type.UnionType;
     18 import com.github.javaparser.ast.validator.chunks.CommonValidators;
     19 import com.github.javaparser.ast.validator.chunks.ModifierValidator;
     20 import com.github.javaparser.ast.validator.chunks.NoBinaryIntegerLiteralsValidator;
     21 import com.github.javaparser.ast.validator.chunks.NoUnderscoresInIntegerLiteralsValidator;
     22 
     23 /**
     24  * This validator validates according to Java 1.0 syntax rules.
     25  */
     26 public class Java1_0Validator extends Validators {
     27     protected final Validator modifiersWithoutStrictfpAndDefaultAndStaticInterfaceMethodsAndPrivateInterfaceMethods
     28             = new ModifierValidator(false, false, false);
     29     protected final Validator noAssertKeyword = new SimpleValidator<>(AssertStmt.class,
     30             n -> true,
     31             (n, reporter) -> reporter.report(n, "'assert' keyword is not supported.")
     32     );
     33     protected final Validator noInnerClasses = new SimpleValidator<>(ClassOrInterfaceDeclaration.class,
     34             n -> !n.isTopLevelType(),
     35             (n, reporter) -> reporter.report(n, "inner classes or interfaces are not supported.")
     36     );
     37     protected final Validator noReflection = new SimpleValidator<>(ClassExpr.class,
     38             n -> true,
     39             (n, reporter) -> reporter.report(n, "Reflection is not supported.")
     40     );
     41     protected final Validator noGenerics = new TreeVisitorValidator((node, reporter) -> {
     42         if (node instanceof NodeWithTypeArguments) {
     43             if (((NodeWithTypeArguments<? extends Node>) node).getTypeArguments().isPresent()) {
     44                 reporter.report(node, "Generics are not supported.");
     45             }
     46         }
     47         if (node instanceof NodeWithTypeParameters) {
     48             if (((NodeWithTypeParameters<? extends Node>) node).getTypeParameters().isNonEmpty()) {
     49                 reporter.report(node, "Generics are not supported.");
     50             }
     51         }
     52     });
     53     protected final SingleNodeTypeValidator<TryStmt> tryWithoutResources = new SingleNodeTypeValidator<>(TryStmt.class, (n, reporter) -> {
     54         if (n.getCatchClauses().isEmpty() && !n.getFinallyBlock().isPresent()) {
     55             reporter.report(n, "Try has no finally and no catch.");
     56         }
     57         if (n.getResources().isNonEmpty()) {
     58             reporter.report(n, "Catch with resource is not supported.");
     59         }
     60     });
     61     protected final Validator noAnnotations = new TreeVisitorValidator((node, reporter) -> {
     62         if (node instanceof AnnotationExpr || node instanceof AnnotationDeclaration) {
     63             reporter.report(node, "Annotations are not supported.");
     64         }
     65     });
     66     protected final Validator noEnums = new SimpleValidator<>(EnumDeclaration.class,
     67             n -> true,
     68             (n, reporter) -> reporter.report(n, "Enumerations are not supported.")
     69     );
     70     protected final Validator noVarargs = new SimpleValidator<>(Parameter.class,
     71             Parameter::isVarArgs,
     72             (n, reporter) -> reporter.report(n, "Varargs are not supported.")
     73     );
     74     protected final Validator noForEach = new SimpleValidator<>(ForeachStmt.class,
     75             n -> true,
     76             (n, reporter) -> reporter.report(n, "For-each loops are not supported.")
     77     );
     78     protected final Validator noStaticImports = new SimpleValidator<>(ImportDeclaration.class,
     79             ImportDeclaration::isStatic,
     80             (n, reporter) -> reporter.report(n, "Static imports are not supported.")
     81     );
     82     protected final Validator noStringsInSwitch = new SimpleValidator<>(SwitchEntryStmt.class,
     83             n -> n.getLabel().map(l -> l instanceof StringLiteralExpr).orElse(false),
     84             (n, reporter) -> reporter.report(n.getLabel().get(), "Strings in switch statements are not supported.")
     85     );
     86     protected final Validator noBinaryIntegerLiterals = new NoBinaryIntegerLiteralsValidator();
     87     protected final Validator noUnderscoresInIntegerLiterals = new NoUnderscoresInIntegerLiteralsValidator();
     88     protected final Validator noMultiCatch = new SimpleValidator<>(UnionType.class,
     89             n -> true,
     90             (n, reporter) -> reporter.report(n, "Multi-catch is not supported.")
     91     );
     92     protected final Validator noLambdas = new SimpleValidator<>(LambdaExpr.class,
     93             n -> true,
     94             (n, reporter) -> reporter.report(n, "Lambdas are not supported.")
     95     );
     96     protected final Validator noModules = new SimpleValidator<>(ModuleDeclaration.class,
     97             n -> true,
     98             (n, reporter) -> reporter.report(n, "Modules are not supported.")
     99     );
    100 
    101     public Java1_0Validator() {
    102         super(new CommonValidators());
    103         add(modifiersWithoutStrictfpAndDefaultAndStaticInterfaceMethodsAndPrivateInterfaceMethods);
    104         add(noAssertKeyword);
    105         add(noInnerClasses);
    106         add(noReflection);
    107         add(noGenerics);
    108         add(tryWithoutResources);
    109         add(noAnnotations);
    110         add(noEnums);
    111         add(noVarargs);
    112         add(noForEach);
    113         add(noStaticImports);
    114         add(noStringsInSwitch);
    115         add(noBinaryIntegerLiterals);
    116         add(noUnderscoresInIntegerLiterals);
    117         add(noMultiCatch);
    118         add(noLambdas);
    119         add(noModules);
    120     }
    121 }
    122