Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream' into issue1154
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Sep 28, 2017
2 parents fb149b0 + b6d0231 commit c473cd9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
/**
* <h1>The union type</h1>
* Represents a set of types. A given value of this type has to be assignable to at least one of the element types.
* <h2>Java 1-7</h2>
* <h2>Java 1-6</h2>
* Does not exist.
* <h2>Java 8+</h2>
* As of Java 8 it is used in catch clauses.
* <h2>Java 7+</h2>
* As of Java 7 it is used in catch clauses.
* <pre><code>
* try {
* ...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +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.type.UnionType;

/**
* This validator validates according to Java 7 syntax rules.
Expand All @@ -15,11 +15,17 @@ public class Java7Validator extends Java6Validator {
reporter.report(n, "Try has no finally, no catch, and no resources.");
}
for (Expression resource : n.getResources()) {
if (!(resource instanceof VariableDeclarationExpr)) {
if (!resource.isVariableDeclarationExpr()) {
reporter.report(n, "Try with resources only supports variable declarations.");
}
}
});
protected final SingleNodeTypeValidator<UnionType> multiCatch = new SingleNodeTypeValidator<>(UnionType.class, (n, reporter) -> {
// Case "0 elements" is caught elsewhere.
if (n.getElements().size() == 1) {
reporter.report(n, "Union type (multi catch) must have at least two elements.");
}
});

public Java7Validator() {
super();
Expand All @@ -28,6 +34,6 @@ public Java7Validator() {
remove(noStringsInSwitch);
remove(noBinaryIntegerLiterals);
remove(noUnderscoresInIntegerLiterals);
remove(noMultiCatch);
replace(noMultiCatch, multiCatch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.Problem;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.UnionType;
import org.junit.Test;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static com.github.javaparser.ParseStart.COMPILATION_UNIT;
import static com.github.javaparser.ParseStart.EXPRESSION;
import static com.github.javaparser.ParseStart.STATEMENT;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.TestUtils.assertCollections;
import static com.github.javaparser.utils.TestUtils.assertNoProblems;
import static com.github.javaparser.utils.TestUtils.assertProblems;

Expand Down Expand Up @@ -57,23 +64,44 @@ public void stringsInSwitch() {
}

@Test
public void nobinaryIntegerLiterals() {
public void binaryIntegerLiterals() {
ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("0b01"));
assertNoProblems(result);
}

@Test
public void noUnderscoresInIntegerLiterals() {
public void underscoresInIntegerLiterals() {
ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("1_000_000"));
assertNoProblems(result);
}

@Test
public void noMultiCatch() {
public void multiCatch() {
ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try{}catch(Abc|Def e){}"));
assertNoProblems(result);
}

@Test
public void multiCatchWithoutElements() {
UnionType unionType = new UnionType();

List<Problem> problems = new ArrayList<>();
javaParser.getParserConfiguration().getValidator().accept(unionType, new ProblemReporter(problems));

assertProblems(problems, "UnionType.elements can not be empty.");
}

@Test
public void multiCatchWithOneElement() {
UnionType unionType = new UnionType();
unionType.getElements().add(new ClassOrInterfaceType());

List<Problem> problems = new ArrayList<>();
javaParser.getParserConfiguration().getValidator().accept(unionType, new ProblemReporter(problems));

assertProblems(problems, "Union type (multi catch) must have at least two elements.");
}

@Test
public void noLambdas() {
ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("a(() -> 1);"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -127,7 +124,11 @@ public static void assertCollections(Collection<?> expected, Collection<?> actua
}

public static void assertProblems(ParseResult<?> result, String... expectedArg) {
Set<String> actual = result.getProblems().stream().map(Problem::toString).collect(Collectors.toSet());
assertProblems(result.getProblems(), expectedArg);
}

public static void assertProblems(List<Problem> result, String... expectedArg) {
Set<String> actual = result.stream().map(Problem::toString).collect(Collectors.toSet());
Set<String> expected = new HashSet<>();
expected.addAll(Arrays.asList(expectedArg));
assertCollections(expected, actual);
Expand Down

0 comments on commit c473cd9

Please sign in to comment.