Skip to content

Commit

Permalink
catch with/without resources
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Mar 20, 2017
1 parent f157326 commit c108a30
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 98 deletions.
Expand Up @@ -6,6 +6,7 @@
import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters; import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
import com.github.javaparser.ast.stmt.AssertStmt; import com.github.javaparser.ast.stmt.AssertStmt;
import com.github.javaparser.ast.stmt.TryStmt;
import com.github.javaparser.ast.validator.chunks.CommonValidators; import com.github.javaparser.ast.validator.chunks.CommonValidators;
import com.github.javaparser.ast.validator.chunks.ModifierValidator; import com.github.javaparser.ast.validator.chunks.ModifierValidator;


Expand All @@ -30,17 +31,26 @@ public class Java1_0Validator extends Validators {
@Override @Override
public void process(Node node, ProblemReporter reporter) { public void process(Node node, ProblemReporter reporter) {
if (node instanceof NodeWithTypeArguments) { if (node instanceof NodeWithTypeArguments) {
if(((NodeWithTypeArguments<? extends Node>) node).getTypeArguments().isPresent()){ if (((NodeWithTypeArguments<? extends Node>) node).getTypeArguments().isPresent()) {
reporter.report(node, "Generics are not supported."); reporter.report(node, "Generics are not supported.");
} }
} }
if (node instanceof NodeWithTypeParameters) { if (node instanceof NodeWithTypeParameters) {
if(((NodeWithTypeParameters<? extends Node>) node).getTypeParameters().isNonEmpty()){ if (((NodeWithTypeParameters<? extends Node>) node).getTypeParameters().isNonEmpty()) {
reporter.report(node, "Generics are not supported."); reporter.report(node, "Generics are not supported.");
} }
} }
} }
}; };
protected final SingleNodeTypeValidator<TryStmt> tryWithoutResources = new SingleNodeTypeValidator<>(TryStmt.class, (n, reporter) -> {
if (n.getCatchClauses().isEmpty() && !n.getFinallyBlock().isPresent()) {
reporter.report(n, "Try has no finally and no catch.");
}
if (n.getResources().isNonEmpty()) {
reporter.report(n, "Catch with resource is not supported.");
}
});



public Java1_0Validator() { public Java1_0Validator() {
super(new CommonValidators()); super(new CommonValidators());
Expand All @@ -49,13 +59,13 @@ public Java1_0Validator() {
add(noInnerClasses); add(noInnerClasses);
add(noReflection); add(noReflection);
add(noGenerics); add(noGenerics);
add(tryWithoutResources);
// TODO validate "no annotations" // TODO validate "no annotations"
// TODO validate "no enums" // TODO validate "no enums"
// TODO validate "no varargs" // TODO validate "no varargs"
// TODO validate "no for-each" // TODO validate "no for-each"
// TODO validate "no static imports" // TODO validate "no static imports"
// TODO validate "no strings in switch" // TODO validate "no strings in switch"
// TODO validate "no resource management in try statement"
// TODO validate "no binary integer literals" // TODO validate "no binary integer literals"
// TODO validate "no underscores in numeric literals" // TODO validate "no underscores in numeric literals"
// TODO validate "no multi-catch" // TODO validate "no multi-catch"
Expand Down
@@ -1,14 +1,23 @@
package com.github.javaparser.ast.validator; package com.github.javaparser.ast.validator;


import com.github.javaparser.ast.stmt.TryStmt;

/** /**
* This validator validates according to Java 7 syntax rules. * This validator validates according to Java 7 syntax rules.
*/ */
public class Java7Validator extends Java6Validator { 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.")
);

public Java7Validator() { public Java7Validator() {
super(); super();
remove(genericsWithoutDiamondOperator); remove(genericsWithoutDiamondOperator);
replace(tryWithoutResources, tryWithResources);
// TODO validate "strings in switch" // TODO validate "strings in switch"
// TODO validate "resource management in try statement"
// TODO validate "binary integer literals" // TODO validate "binary integer literals"
// TODO validate "underscores in numeric literals" // TODO validate "underscores in numeric literals"
// TODO validate "multi-catch" // TODO validate "multi-catch"
Expand Down
Expand Up @@ -17,12 +17,6 @@
public class CommonValidators extends Validators { public class CommonValidators extends Validators {
public CommonValidators() { public CommonValidators() {
super( super(
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.")
),
new SimpleValidator<>(ClassOrInterfaceDeclaration.class, new SimpleValidator<>(ClassOrInterfaceDeclaration.class,
n -> !n.isInterface() && n.getExtendedTypes().size() > 1, n -> !n.isInterface() && n.getExtendedTypes().size() > 1,
(n, reporter) -> reporter.report(n.getExtendedTypes(1), "A class cannot extend more than one other class.") (n, reporter) -> reporter.report(n.getExtendedTypes(1), "A class cannot extend more than one other class.")
Expand Down
@@ -1,75 +1,80 @@
package com.github.javaparser.ast.validator; package com.github.javaparser.ast.validator;


import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult; import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.stmt.Statement;
import org.junit.Test; import org.junit.Test;


import static com.github.javaparser.ParseStart.*; import static com.github.javaparser.ParseStart.*;
import static com.github.javaparser.Providers.provider; import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.ast.validator.ValidatorTest.javaParser1_0;
import static com.github.javaparser.utils.TestUtils.assertNoProblems; import static com.github.javaparser.utils.TestUtils.assertNoProblems;
import static com.github.javaparser.utils.TestUtils.assertProblems; import static com.github.javaparser.utils.TestUtils.assertProblems;


public class Java1_0ValidatorTest { public class Java1_0ValidatorTest {
public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setValidator(new Java1_0Validator()));

@Test @Test
public void tryWithoutAnything() { public void tryWithoutResources() {
ParseResult<Statement> result = javaParser1_0.parse(STATEMENT, provider("try{}")); ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(X x=new Y()){}"));
assertProblems(result, "(line 1,col 1) Try has no finally, no catch, and no resources."); assertProblems(result,
"(line 1,col 1) Catch with resource is not supported.",
"(line 1,col 1) Try has no finally and no catch.");
} }


@Test @Test
public void classExtendingMoreThanOne() { public void classExtendingMoreThanOne() {
ParseResult<CompilationUnit> result = javaParser1_0.parse(COMPILATION_UNIT, provider("class X extends Y, Z {}")); ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X extends Y, Z {}"));
assertProblems(result, "(line 1,col 20) A class cannot extend more than one other class."); assertProblems(result, "(line 1,col 20) A class cannot extend more than one other class.");
} }


@Test @Test
public void interfaceUsingImplements() { public void interfaceUsingImplements() {
ParseResult<CompilationUnit> result = javaParser1_0.parse(COMPILATION_UNIT, provider("interface X implements Y {}")); ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X implements Y {}"));
assertProblems(result, "(line 1,col 24) An interface cannot implement other interfaces."); assertProblems(result, "(line 1,col 24) An interface cannot implement other interfaces.");
} }


@Test @Test
public void interfaceWithInitializer() { public void interfaceWithInitializer() {
ParseResult<CompilationUnit> result = javaParser1_0.parse(COMPILATION_UNIT, provider("interface X {{}}")); ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X {{}}"));
assertProblems(result, "(line 1,col 14) An interface cannot have initializers."); assertProblems(result, "(line 1,col 14) An interface cannot have initializers.");
} }


@Test @Test
public void defaultInClass() { public void defaultInClass() {
ParseResult<CompilationUnit> result = javaParser1_0.parse(COMPILATION_UNIT, provider("class X {default void a(){};}")); ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X {default void a(){};}"));
assertProblems(result, "(line 1,col 10) 'default' is not allowed here."); assertProblems(result, "(line 1,col 10) 'default' is not allowed here.");
} }


@Test @Test
public void leftHandAssignmentCannotBeAConditional() { public void leftHandAssignmentCannotBeAConditional() {
ParseResult<Expression> result = javaParser1_0.parse(EXPRESSION, provider("(1==2)=3")); ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("(1==2)=3"));
assertProblems(result, "(line 1,col 1) Illegal left hand side of an assignment."); assertProblems(result, "(line 1,col 1) Illegal left hand side of an assignment.");
} }


@Test @Test
public void leftHandAssignmentCannotBeEmptyBraces() { public void leftHandAssignmentCannotBeEmptyBraces() {
ParseResult<Expression> result = javaParser1_0.parse(EXPRESSION, provider("()=3")); ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("()=3"));
assertProblems(result, "(line 1,col 1) Illegal left hand side of an assignment."); assertProblems(result, "(line 1,col 1) Illegal left hand side of an assignment.");
} }


@Test @Test
public void leftHandAssignmentCanBeInBraces() { public void leftHandAssignmentCanBeInBraces() {
ParseResult<Expression> result = javaParser1_0.parse(EXPRESSION, provider("(i) += (i) += 1")); ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("(i) += (i) += 1"));
assertNoProblems(result); assertNoProblems(result);
} }


@Test @Test
public void noInnerClasses() { public void noInnerClasses() {
ParseResult<CompilationUnit> result = javaParser1_0.parse(COMPILATION_UNIT, provider("class X{class Y{}}")); ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{class Y{}}"));
assertProblems(result, "(line 1,col 9) inner classes or interfaces are not supported."); assertProblems(result, "(line 1,col 9) inner classes or interfaces are not supported.");
} }


@Test @Test
public void noReflection() { public void noReflection() {
ParseResult<Expression> result = javaParser1_0.parse(EXPRESSION, provider("Abc.class")); ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("Abc.class"));
assertProblems(result, "(line 1,col 1) Reflection is not supported."); assertProblems(result, "(line 1,col 1) Reflection is not supported.");
} }
} }

0 comments on commit c108a30

Please sign in to comment.