Skip to content

Commit

Permalink
Workaround for javaparser issue
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
ctubbsii committed Oct 6, 2020
1 parent 91fc770 commit 340d7e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<formatterConfigFile>src/tools/modified-google-style.xml</formatterConfigFile>
<github.site.repositoryName>impsort-maven-plugin</github.site.repositoryName>
<github.site.repositoryOwner>revelc</github.site.repositoryOwner>
<javaparser.version>3.15.21</javaparser.version>
<javaparser.version>3.16.1</javaparser.version>
<maven.compiler.release>8</maven.compiler.release>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down Expand Up @@ -164,12 +164,12 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
<version>2.8.1</version>
</plugin>
<plugin>
<groupId>org.gaul</groupId>
<artifactId>modernizer-maven-plugin</artifactId>
<version>1.8.0</version>
<version>2.1.0</version>
<configuration>
<javaVersion>${maven.compiler.target}</javaVersion>
</configuration>
Expand All @@ -182,7 +182,7 @@
<plugin>
<groupId>com.github.ekryd.sortpom</groupId>
<artifactId>sortpom-maven-plugin</artifactId>
<version>2.10.0</version>
<version>2.12.0</version>
<configuration>
<predefinedSortOrder>recommended_2008_06</predefinedSortOrder>
<createBackupFile>false</createBackupFile>
Expand Down Expand Up @@ -226,7 +226,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<version>3.0.0-M1</version>
<configuration>
<useReleaseProfile>false</useReleaseProfile>
<pushChanges>false</pushChanges>
Expand All @@ -236,7 +236,7 @@
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.11.0</version>
<version>2.13.0</version>
<configuration>
<compilerCompliance>${maven.compiler.source}</compilerCompliance>
<compilerSource>${maven.compiler.source}</compilerSource>
Expand All @@ -254,20 +254,20 @@
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
<configuration>
<groups>java.,javax.,org.,com.</groups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.0.0</version>
<version>4.0.4</version>
<configuration>
<xmlOutput>true</xmlOutput>
<effort>Max</effort>
Expand Down Expand Up @@ -419,7 +419,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.1</version>
</plugin>
</plugins>
</reporting>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/net/revelc/code/impsort/ImpSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Problem> 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());
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/net/revelc/code/impsort/ImpSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 340d7e2

Please sign in to comment.