1 package com.github.javaparser.ast.validator; 2 3 import com.github.javaparser.ast.Node; 4 import com.github.javaparser.ast.NodeList; 5 import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; 6 import com.github.javaparser.ast.type.PrimitiveType; 7 import com.github.javaparser.ast.type.Type; 8 9 import java.util.Optional; 10 11 /** 12 * This validator validates according to Java 5 syntax rules. 13 */ 14 public class Java5Validator extends Java1_4Validator { 15 Validator genericsWithoutDiamondOperator = new TreeVisitorValidator((node, reporter) -> { 16 if (node instanceof NodeWithTypeArguments) { 17 Optional<NodeList<Type>> typeArguments = ((NodeWithTypeArguments<? extends Node>) node).getTypeArguments(); 18 if (typeArguments.isPresent() && typeArguments.get().isEmpty()) { 19 reporter.report(node, "The diamond operator is not supported."); 20 } 21 } 22 }); 23 24 protected Validator noPrimitiveGenericArguments = new TreeVisitorValidator((node, reporter) -> { 25 if (node instanceof NodeWithTypeArguments) { 26 Optional<NodeList<Type>> typeArguments = ((NodeWithTypeArguments<? extends Node>) node).getTypeArguments(); 27 typeArguments.ifPresent(types -> types.forEach(ty -> { 28 if (ty instanceof PrimitiveType) { 29 reporter.report(node, "Type arguments may not be primitive."); 30 } 31 })); 32 } 33 }); 34 35 protected final Validator enumNotAllowed = new ReservedKeywordValidator("enum"); 36 37 public Java5Validator() { 38 super(); 39 replace(noGenerics, genericsWithoutDiamondOperator); 40 add(noPrimitiveGenericArguments); 41 add(enumNotAllowed); 42 43 // TODO validate annotations on classes, fields and methods but nowhere else 44 // The following is probably too simple. 45 remove(noAnnotations); 46 47 remove(noEnums); 48 remove(noVarargs); 49 remove(noForEach); 50 remove(noStaticImports); 51 } 52 } 53