Repository navigation
Read Spoon compliance level from the project's pom.xml #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+114
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
liquidjava-example/src/main/java/testSuite/CorrectModernJavaSyntax.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package testSuite; | ||
|
|
||
| import java.io.StringReader; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class CorrectModernJavaSyntax { | ||
|
|
||
| // local variable type inference (Java 10) | ||
| void localVar() { | ||
| var n = 5; | ||
| @Refinement("_ > 0") | ||
| int p = n; | ||
| } | ||
|
|
||
| // resource reference in try-with-resources (Java 9) | ||
| void resourceReference() throws Exception { | ||
| StringReader reader = new StringReader("a"); | ||
| try (reader) { | ||
| reader.read(); | ||
| } | ||
| } | ||
|
|
||
| // switch expression (Java 14) | ||
| int switchExpression(int k) { | ||
| return switch (k) { | ||
| case 0 -> 1; | ||
| default -> 2; | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
liquidjava-verifier/src/main/java/liquidjava/api/ComplianceLevel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package liquidjava.api; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import java.util.Properties; | ||
|
|
||
| import org.apache.maven.model.io.xpp3.MavenXpp3Reader; | ||
|
|
||
| /** | ||
| * Java compliance level used by Spoon, read from the {@code maven.compiler.release} or {@code maven.compiler.source} | ||
| * property of the nearest {@code pom.xml} of the verified paths | ||
| */ | ||
| public final class ComplianceLevel { | ||
|
|
||
| /** Highest level accepted by Spoon 10.4.2 (JDT 3.33), also used when no pom declares one */ | ||
| public static final int MAX_SUPPORTED = 19; | ||
|
|
||
| public static int resolve(String... paths) { | ||
| return Arrays.stream(paths).map(path -> fromPom(new File(path).getAbsoluteFile())).flatMap(Optional::stream) | ||
| .max(Integer::compare).map(level -> Math.min(level, MAX_SUPPORTED)).orElse(MAX_SUPPORTED); | ||
| } | ||
|
|
||
| private static Optional<Integer> fromPom(File path) { | ||
| for (File dir = path; dir != null; dir = dir.getParentFile()) { | ||
| File pom = new File(dir, "pom.xml"); | ||
| if (!pom.isFile()) | ||
| continue; | ||
| try (FileReader reader = new FileReader(pom)) { | ||
| Properties properties = new MavenXpp3Reader().read(reader).getProperties(); | ||
| String version = properties.getProperty("maven.compiler.release", | ||
| properties.getProperty("maven.compiler.source")); | ||
| if (version != null) | ||
| return Optional.of(Integer.parseInt(version.trim().replaceFirst("^1\\.", ""))); | ||
| } catch (Exception ignored) { | ||
| // unreadable pom or non-numeric version: keep searching enclosing poms | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
liquidjava-verifier/src/test/java/liquidjava/api/tests/TestComplianceLevel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package liquidjava.api.tests; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import liquidjava.api.ComplianceLevel; | ||
|
|
||
| class TestComplianceLevel { | ||
|
|
||
| @TempDir | ||
| Path project; | ||
|
|
||
| private int resolveWithRelease(String release) throws IOException { | ||
| Files.writeString(project.resolve("pom.xml"), | ||
| "<project><modelVersion>4.0.0</modelVersion><properties><maven.compiler.release>" + release | ||
| + "</maven.compiler.release></properties></project>"); | ||
| return ComplianceLevel.resolve(Files.createDirectories(project.resolve("src")).toString()); | ||
| } | ||
|
|
||
| @Test | ||
| void readsLevelFromPom() throws IOException { | ||
| assertEquals(8, resolveWithRelease("1.8")); | ||
| } | ||
|
|
||
| @Test | ||
| void capsAtMaxSupported() throws IOException { | ||
| assertEquals(ComplianceLevel.MAX_SUPPORTED, resolveWithRelease("30")); | ||
| } | ||
| } | ||
|
CatarinaGamboa marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.