From 340d7e2940a08e24a10addf75d7786f4333b30bc Mon Sep 17 00:00:00 2001 From: Christopher Tubbs Date: Tue, 6 Oct 2020 15:06:47 -0400 Subject: [PATCH] Workaround for javaparser issue * Format code * Bump versions * Workaround javaparser/javaparser#2820 introduced in #33 by explicitly checking for the problem message and ignoring it (while printing other problems before throwing an exception) --- pom.xml | 20 +++++++++---------- .../java/net/revelc/code/impsort/ImpSort.java | 11 +++++++++- .../net/revelc/code/impsort/ImpSortTest.java | 8 ++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 402f720..ffebabd 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ src/tools/modified-google-style.xml impsort-maven-plugin revelc - 3.15.21 + 3.16.1 8 1.8 1.8 @@ -164,12 +164,12 @@ org.codehaus.mojo versions-maven-plugin - 2.7 + 2.8.1 org.gaul modernizer-maven-plugin - 1.8.0 + 2.1.0 ${maven.compiler.target} @@ -182,7 +182,7 @@ com.github.ekryd.sortpom sortpom-maven-plugin - 2.10.0 + 2.12.0 recommended_2008_06 false @@ -226,7 +226,7 @@ org.apache.maven.plugins maven-release-plugin - 2.5.3 + 3.0.0-M1 false false @@ -236,7 +236,7 @@ net.revelc.code.formatter formatter-maven-plugin - 2.11.0 + 2.13.0 ${maven.compiler.source} ${maven.compiler.source} @@ -254,7 +254,7 @@ net.revelc.code impsort-maven-plugin - 1.4.0 + 1.4.1 java.,javax.,org.,com. @@ -262,12 +262,12 @@ org.apache.maven.plugins maven-checkstyle-plugin - 3.1.0 + 3.1.1 com.github.spotbugs spotbugs-maven-plugin - 4.0.0 + 4.0.4 true Max @@ -419,7 +419,7 @@ org.apache.maven.plugins maven-project-info-reports-plugin - 3.0.0 + 3.1.1 diff --git a/src/main/java/net/revelc/code/impsort/ImpSort.java b/src/main/java/net/revelc/code/impsort/ImpSort.java index 5cbbdc0..4b75d89 100644 --- a/src/main/java/net/revelc/code/impsort/ImpSort.java +++ b/src/main/java/net/revelc/code/impsort/ImpSort.java @@ -39,6 +39,7 @@ import com.github.javaparser.JavaToken; import com.github.javaparser.ParseResult; import com.github.javaparser.Position; +import com.github.javaparser.Problem; import com.github.javaparser.TokenRange; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.ImportDeclaration; @@ -113,7 +114,15 @@ public Result parseFile(final Path path) throws IOException { CompilationUnit unit = parseResult.getResult() .orElseThrow(() -> new ImpSortException(path, Reason.UNABLE_TO_PARSE)); if (!parseResult.isSuccessful()) { - throw new ImpSortException(path, Reason.PARTIAL_PARSE); + List problems = parseResult.getProblems().stream().filter( + // workaround for javaparser/javaparser#2820 + // https://github.com/javaparser/javaparser/issues/2820 + p -> !p.getMessage().contains("Try with resources only supports variable declarations.")) + .collect(Collectors.toList()); + if (!problems.isEmpty()) { + problems.forEach(System.out::println); + throw new ImpSortException(path, Reason.PARTIAL_PARSE); + } } Position packagePosition = unit.getPackageDeclaration().map(p -> p.getEnd().get()).orElse(unit.getBegin().get()); diff --git a/src/test/java/net/revelc/code/impsort/ImpSortTest.java b/src/test/java/net/revelc/code/impsort/ImpSortTest.java index 1f36ec6..c18c6ef 100644 --- a/src/test/java/net/revelc/code/impsort/ImpSortTest.java +++ b/src/test/java/net/revelc/code/impsort/ImpSortTest.java @@ -205,11 +205,11 @@ public void testRemoveSamePackageImports() { @Test public void testResultStartWithComment() throws IOException { - Path p = - Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "FirstImportComment.java"); + Path p = Paths.get(System.getProperty("user.dir"), "src", "test", "resources", + "FirstImportComment.java"); Result result = - new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO) - .parseFile(p); + new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO) + .parseFile(p); Path output = File.createTempFile("impSortComment", null, new File("target")).toPath(); result.saveSorted(output);