Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make validator library and put the first validation in it.
- Loading branch information
Showing
13 changed files
with
190 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
javaparser-core/src/main/java/com/github/javaparser/ast/validator/DefaultValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.ast.stmt.TryStmt; | ||
|
||
/** | ||
* The validator that is used by default by the static JavaParser methods. | ||
*/ | ||
public class DefaultValidator extends Validators { | ||
public DefaultValidator() { | ||
super(new VisitorValidator() { | ||
@Override | ||
public void visit(TryStmt n, ProblemReporter reporter) { | ||
if (n.getCatchClauses().isEmpty() | ||
&& n.getResources().isEmpty() | ||
&& !n.getFinallyBlock().isPresent()) { | ||
reporter.report("Try has no finally, no catch, and no resources", n); | ||
} | ||
} | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
javaparser-core/src/main/java/com/github/javaparser/ast/validator/NoProblemsValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.ast.Node; | ||
|
||
/** | ||
* Stub validator for when no validation is wanted. | ||
*/ | ||
public class NoProblemsValidator implements Validator { | ||
@Override | ||
public void validate(Node node, ProblemReporter problemReporter) { | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
javaparser-core/src/main/java/com/github/javaparser/ast/validator/ProblemReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.Problem; | ||
import com.github.javaparser.Range; | ||
import com.github.javaparser.ast.Node; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A simple interface where validators can report found problems. | ||
*/ | ||
public class ProblemReporter { | ||
private final List<Problem> problems; | ||
|
||
public ProblemReporter(List<Problem> problems) { | ||
this.problems = problems; | ||
} | ||
|
||
/** | ||
* Report a problem. | ||
* | ||
* @param message description of the problem | ||
* @param node the node in which the problem occurred, used to find the Range of the problem. | ||
*/ | ||
public void report(String message, Node node) { | ||
report(message, node.getRange().orElse(null)); | ||
} | ||
|
||
public void report(String message, Range range) { | ||
problems.add(new Problem(message, range, null)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
javaparser-core/src/main/java/com/github/javaparser/ast/validator/Validator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.ast.Node; | ||
|
||
/** | ||
* A validator that can be run on a node to check for semantic errors. | ||
* It is fully up to the implementor how to do this. | ||
*/ | ||
public interface Validator { | ||
/** | ||
* @param node the node that wants to be validated | ||
* @param problemReporter when found, validation errors can be reported here | ||
*/ | ||
void validate(Node node, ProblemReporter problemReporter); | ||
} |
27 changes: 27 additions & 0 deletions
27
javaparser-core/src/main/java/com/github/javaparser/ast/validator/Validators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.ast.Node; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* A validator that will call a collection of validators. | ||
*/ | ||
public class Validators implements Validator { | ||
private final List<Validator> validators = new ArrayList<>(); | ||
|
||
public Validators(Validator... validators) { | ||
this.validators.addAll(Arrays.asList(validators)); | ||
} | ||
|
||
public List<Validator> getValidators() { | ||
return validators; | ||
} | ||
|
||
@Override | ||
public void validate(Node node, ProblemReporter problemReporter) { | ||
validators.forEach(v -> v.validate(node, problemReporter)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
javaparser-core/src/main/java/com/github/javaparser/ast/validator/VisitorValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
/** | ||
* A validator that uses a visitor for validation. | ||
* This class is the visitor too. | ||
* Implement the "visit" methods you want to use for validation. | ||
*/ | ||
public abstract class VisitorValidator extends VoidVisitorAdapter<ProblemReporter> implements Validator { | ||
@Override | ||
public void validate(Node node, ProblemReporter problemReporter) { | ||
node.accept(this, problemReporter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...arser-testing/src/test/java/com/github/javaparser/ast/validator/DefaultValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.JavaParser; | ||
import com.github.javaparser.ParseResult; | ||
import com.github.javaparser.ParseStart; | ||
import com.github.javaparser.ast.stmt.Statement; | ||
import org.junit.Test; | ||
|
||
import static com.github.javaparser.Providers.provider; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class DefaultValidatorTest { | ||
@Test | ||
public void tryWithoutAnything() { | ||
ParseResult<Statement> result = new JavaParser().parse(ParseStart.STATEMENT, provider("try{}"), new DefaultValidator()); | ||
assertEquals("[(line 1,col 1) Try has no finally, no catch, and no resources]", result.getProblems().toString()); | ||
} | ||
@Test | ||
public void classExtendingMoreThanOne() { | ||
ParseResult<Statement> result = new JavaParser().parse(ParseStart.STATEMENT, provider("try{}"), new DefaultValidator()); | ||
assertEquals("[(line 1,col 1) Try has no finally, no catch, and no resources]", result.getProblems().toString()); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
javaparser-testing/src/test/java/com/github/javaparser/ast/validator/ValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.javaparser.ast.validator; | ||
|
||
import com.github.javaparser.JavaParser; | ||
import com.github.javaparser.ParseResult; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import org.junit.Test; | ||
|
||
import static com.github.javaparser.ParseStart.COMPILATION_UNIT; | ||
import static com.github.javaparser.Providers.provider; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ValidatorTest { | ||
@Test | ||
public void noProblemsHere() { | ||
ParseResult<CompilationUnit> result = new JavaParser().parse(COMPILATION_UNIT, provider("class X{}"), new NoProblemsValidator()); | ||
assertEquals(true, result.isSuccessful()); | ||
} | ||
|
||
} |