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