Java1_0Validator.java revision fedf7a51744fef11532541bcb5b38188d555ddcb
1package com.github.javaparser.ast.validator;
2
3import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
4import com.github.javaparser.ast.body.InitializerDeclaration;
5import com.github.javaparser.ast.expr.*;
6import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt;
7import com.github.javaparser.ast.stmt.TryStmt;
8
9import java.util.Optional;
10
11/**
12 * Contains validations that are valid for every Java version.
13 * Used by default by the static JavaParser methods.
14 */
15public class Java1_0Validator extends Validators {
16    public Java1_0Validator() {
17        super(
18                new VisitorValidator() {
19                    @Override
20                    public void visit(TryStmt n, ProblemReporter reporter) {
21                        if (n.getCatchClauses().isEmpty()
22                                && n.getResources().isEmpty()
23                                && !n.getFinallyBlock().isPresent()) {
24                            reporter.report(n, "Try has no finally, no catch, and no resources.");
25                        }
26                        super.visit(n, reporter);
27                    }
28                },
29                new VisitorValidator() {
30                    @Override
31                    public void visit(ClassOrInterfaceDeclaration n, ProblemReporter reporter) {
32                        if (!n.isInterface() && n.getExtendedTypes().size() > 1) {
33                            reporter.report(n.getExtendedTypes(1), "A class cannot extend more than one other class.");
34                        }
35                        super.visit(n, reporter);
36                    }
37                },
38                new VisitorValidator() {
39                    @Override
40                    public void visit(ClassOrInterfaceDeclaration n, ProblemReporter reporter) {
41                        if (n.isInterface() && !n.getImplementedTypes().isEmpty()) {
42                            reporter.report(n.getImplementedTypes(0), "An interface cannot implement other interfaces.");
43                        }
44                        super.visit(n, reporter);
45                    }
46                },
47                new VisitorValidator() {
48                    @Override
49                    public void visit(ClassOrInterfaceDeclaration n, ProblemReporter reporter) {
50                        n.getParentNode().ifPresent(p -> {
51                            if (p instanceof LocalClassDeclarationStmt && n.isInterface())
52                                reporter.report(n, "There is no such thing as a local interface.");
53                        });
54                        super.visit(n, reporter);
55                    }
56                },
57                new VisitorValidator() {
58                    @Override
59                    public void visit(ClassOrInterfaceDeclaration n, ProblemReporter reporter) {
60                        if (n.isInterface()) {
61                            n.getMembers().forEach(mem -> {
62                                if (mem instanceof InitializerDeclaration) {
63                                    reporter.report(mem, "An interface cannot have initializers.");
64                                }
65                            });
66                        }
67                        super.visit(n, reporter);
68                    }
69                },
70                new VisitorValidator() {
71                    @Override
72                    public void visit(ClassOrInterfaceDeclaration n, ProblemReporter reporter) {
73                        if (n.isInterface()) {
74                            n.getMethods().forEach(m -> {
75                                if (m.isDefault() && !m.getBody().isPresent()) {
76                                    reporter.report(m, "'default' methods must have a body.");
77                                }
78                            });
79                        }
80                        super.visit(n, reporter);
81                    }
82                },
83                new VisitorValidator() {
84                    @Override
85                    public void visit(AssignExpr n, ProblemReporter reporter) {
86                        // https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26
87                        Expression target = n.getTarget();
88                        while (target instanceof EnclosedExpr) {
89                            Optional<Expression> inner = ((EnclosedExpr) target).getInner();
90                            if (!inner.isPresent()) {
91                                reporter.report(n.getTarget(), "Illegal left hand side of an assignment.");
92                                super.visit(n, reporter);
93                                return;
94                            }
95                            target = inner.get();
96                        }
97                        if (target instanceof NameExpr
98                                || target instanceof ArrayAccessExpr
99                                || target instanceof FieldAccessExpr) {
100                            return;
101                        }
102                        reporter.report(n.getTarget(), "Illegal left hand side of an assignment.");
103                        super.visit(n, reporter);
104                    }
105                },
106                new Java1_0ModifierValidator()
107        );
108    }
109}
110