Skip to content

Commit

Permalink
Add validators to check that field access resources are only availabl…
Browse files Browse the repository at this point in the history
…e in Java 9
  • Loading branch information
matozoid committed Sep 11, 2017
1 parent 9b583d9 commit c6e3de4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
@@ -1,22 +1,30 @@
package com.github.javaparser.ast.validator;

import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.TryStmt;

/**
* This validator validates according to Java 7 syntax rules.
*/
public class Java7Validator extends Java6Validator {
protected final Validator tryWithResources = new SimpleValidator<>(TryStmt.class,
n -> n.getCatchClauses().isEmpty()
&& n.getResources().isEmpty()
&& !n.getFinallyBlock().isPresent(),
(n, reporter) -> reporter.report(n, "Try has no finally, no catch, and no resources.")
);
protected final SingleNodeTypeValidator<TryStmt> tryWithLimitedResources = new SingleNodeTypeValidator<>(TryStmt.class, (n, reporter) -> {
if (n.getCatchClauses().isEmpty()
&& n.getResources().isEmpty()
&& !n.getFinallyBlock().isPresent()) {
reporter.report(n, "Try has no finally, no catch, and no resources.");
}
for (Expression resource : n.getResources()) {
if (!(resource instanceof VariableDeclarationExpr)) {
reporter.report(n, "Try with resources only supports variable declarations.");
}
}
});

public Java7Validator() {
super();
remove(genericsWithoutDiamondOperator);
replace(tryWithoutResources, tryWithResources);
replace(tryWithoutResources, tryWithLimitedResources);
remove(noStringsInSwitch);
remove(noBinaryIntegerLiterals);
remove(noUnderscoresInIntegerLiterals);
Expand Down
@@ -1,5 +1,8 @@
package com.github.javaparser.ast.validator;

import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.TryStmt;
import com.github.javaparser.ast.validator.chunks.ModifierValidator;
import com.github.javaparser.ast.validator.chunks.UnderscoreKeywordValidator;

Expand All @@ -9,11 +12,19 @@
public class Java9Validator extends Java8Validator {
protected final Validator underscoreKeywordValidator = new UnderscoreKeywordValidator();
protected final Validator modifiers = new ModifierValidator(true, true, true);
protected final SingleNodeTypeValidator<TryStmt> tryWithResources = new SingleNodeTypeValidator<>(TryStmt.class, (n, reporter) -> {
if (n.getCatchClauses().isEmpty()
&& n.getResources().isEmpty()
&& !n.getFinallyBlock().isPresent()) {
reporter.report(n, "Try has no finally, no catch, and no resources.");
}
});

public Java9Validator() {
super();
add(underscoreKeywordValidator);
remove(noModules);
replace(modifiersWithoutPrivateInterfaceMethods, modifiers);
replace(tryWithLimitedResources, tryWithResources);
}
}
Expand Up @@ -21,7 +21,9 @@ public List<Validator> getValidators() {
}

public Validators remove(Validator validator) {
validators.remove(validator);
if (!validators.remove(validator)) {
throw new AssertionError("Trying to remove a validator that isn't there.");
}
return this;
}

Expand Down
Expand Up @@ -38,6 +38,18 @@ public void tryWithoutAnything() {
assertProblems(result, "(line 1,col 1) Try has no finally, no catch, and no resources.");
}

@Test
public void tryWithResourceVariableDeclaration() {
ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(Reader r = new Reader()){}"));
assertNoProblems(result);
}

@Test
public void tryWithResourceReference() {
ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(a.b.c){}"));
assertProblems(result, "(line 1,col 1) Try with resources only supports variable declarations.");
}

@Test
public void stringsInSwitch() {
ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("switch(x){case \"abc\": ;}"));
Expand Down
Expand Up @@ -68,4 +68,11 @@ public void modules() {
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("open module x {}"));
assertNoProblems(result);
}

@Test
public void tryWithResourceReference() {
ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(a.b.c){}"));
assertNoProblems(result);
}

}

0 comments on commit c6e3de4

Please sign in to comment.