Skip to content

Commit 0de2ffe

Browse files
authored
Merge pull request #276 from akbarkanso/add-java-21-language-level
Add Java 21 language level support to -ll flag and document it
2 parents 3896ac9 + f6c964c commit 0de2ffe

8 files changed

Lines changed: 195 additions & 1 deletion

File tree

.github/workflows/continuous-integration.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ jobs:
9898
ANNOTATOR_TEST_DISABLE_PARALLEL_PROCESSING: "false"
9999
ANNOTATOR_TEST_DISABLE_CACHING: "false"
100100
run: ./gradlew :annotator-core:test --tests "edu.ucr.cs.riple.core.Java17Test"
101+
parser-configuration-21:
102+
runs-on: ubuntu-latest
103+
steps:
104+
- uses: actions/checkout@v4
105+
- name: Set up JDK 21
106+
uses: actions/setup-java@v4
107+
with:
108+
java-version: 21
109+
distribution: 'temurin'
110+
- name: Build with Gradle
111+
env:
112+
ANNOTATOR_TEST_DISABLE_PARALLEL_PROCESSING: "false"
113+
ANNOTATOR_TEST_DISABLE_CACHING: "false"
114+
run: ./gradlew :annotator-core:test --tests "edu.ucr.cs.riple.core.Java21Test"
101115

OPTIONS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Here are all __optional__ arguments that can alter Annotator's default configura
2020
| `-drdl, --deactivate-region-detection-lombok` | Deactivates region detection for Lombok. |
2121
| `-nna, --nonnull-annotations <arg>` | Adds a list of non-null annotations separated by a comma to be acknowledged by Annotator (e.g., com.example1.Nonnull,com.example2.Nonnull) |
2222
| `eic, enable-impact-cache` | Enables fixes impacts caching for next cycles. |
23+
| `-ll, --language-level <arg>` | Java language level used by the parser when reading source files. Supported values: `11`, `17`, `21`. Defaults to `17`. Use `21` when the target project uses Java 21 features such as record patterns or switch pattern matching. |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Here are some useful __optional__ arguments that can alter the default configura
182182
|--------------------------------------------------------|-------------|
183183
| `-n,--nullable <arg>` | Sets custom `@Nullable` annotation. |
184184
| `-rboserr, --redirect-build-output-stderr` | Redirects build outputs to `STD Err`. |
185+
| `-ll, --language-level <arg>` | Parser language level. Supported values: `11`, `17`, `21`. Defaults to `17`. Required when the target project uses Java 21 syntax (e.g. record patterns, switch patterns). |
185186

186187

187188
To learn more about all the __optional__ arguments, please refer to [OPTIONS.md](./OPTIONS.md)

annotator-core/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ shadowJar {
9696
// Exclude tests not supported by Java 11.
9797
if (JavaVersion.current().isJava11()) {
9898
// exclude Java17Test which is designed to test Java 17 features.
99+
// exclude Java21Test which is designed to test Java 21 features.
99100
test {
100101
filter {
101102
excludeTestsMatching "edu.ucr.cs.riple.core.Java17Test"
103+
excludeTestsMatching "edu.ucr.cs.riple.core.Java21Test"
102104
// temporarily exclude LombokTest until we find a solution for the issue.
103105
excludeTestsMatching "edu.ucr.cs.riple.core.LombokTest"
104106
}

annotator-core/src/main/java/edu/ucr/cs/riple/core/Config.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public Config(String[] args) {
378378
"ll",
379379
"language-level",
380380
true,
381-
"Java language level to use when parsing code. Supported values are 11 and 17. Defaults to 17.");
381+
"Java language level to use when parsing code. Supported values are 11, 17 and 21. Defaults to 17.");
382382
languageLevelOption.setRequired(false);
383383
options.addOption(languageLevelOption);
384384

@@ -502,6 +502,8 @@ private ParserConfiguration.LanguageLevel getLanguageLevel(String languageLevelS
502502
return ParserConfiguration.LanguageLevel.JAVA_11;
503503
case "17":
504504
return ParserConfiguration.LanguageLevel.JAVA_17;
505+
case "21":
506+
return ParserConfiguration.LanguageLevel.JAVA_21;
505507
default:
506508
throw new IllegalArgumentException("Unsupported language level: " + languageLevelString);
507509
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 Nima Karimipour
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package edu.ucr.cs.riple.core;
26+
27+
import com.github.javaparser.ParserConfiguration;
28+
import edu.ucr.cs.riple.core.tools.TReport;
29+
import edu.ucr.cs.riple.injector.location.OnField;
30+
import java.util.Set;
31+
import org.junit.Test;
32+
33+
/**
34+
* Tests in this class are related to Java 21 features. These tests include blocks of code that are
35+
* not syntactically supported by Java 17.
36+
*/
37+
public class Java21Test extends AnnotatorBaseCoreTest {
38+
39+
public Java21Test() {
40+
super("java-21");
41+
}
42+
43+
@Test
44+
public void recordPatternInInstanceofTest() {
45+
coreTestHelper
46+
.onTarget()
47+
.withSourceLines(
48+
"Main.java",
49+
"package test;",
50+
"public class Main {",
51+
" record Point(double x, double y) {}",
52+
" Object f1, f2, f3, f4;",
53+
" void foo() {",
54+
" // record pattern in instanceof - finalized in Java 21 (JEP 440)",
55+
" if (f1 instanceof Point(double x, double y)) { }",
56+
" }",
57+
"}")
58+
.withExpectedReports(
59+
new TReport(new OnField("Main.java", "test.Main", Set.of("f1", "f2", "f3", "f4")), -4))
60+
.withLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_21)
61+
.start();
62+
}
63+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 Nima Karimipour
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
import net.ltgt.gradle.errorprone.CheckSeverity
26+
27+
plugins{
28+
id "net.ltgt.errorprone" version "4.1.0" apply false
29+
}
30+
31+
subprojects {
32+
apply plugin: "java"
33+
apply plugin: "net.ltgt.errorprone"
34+
35+
repositories {
36+
mavenLocal()
37+
mavenCentral()
38+
google()
39+
}
40+
41+
def libraryloader = project.getProperty("library-model-loader-path")
42+
43+
repositories {
44+
mavenLocal()
45+
mavenCentral()
46+
}
47+
48+
dependencies {
49+
if(project.name != "Target"){
50+
compileOnly project(":Target")
51+
annotationProcessor files(libraryloader)
52+
}
53+
annotationProcessor "com.uber.nullaway:nullaway:" + System.getenv('NULLAWAY_TEST_VERSION')
54+
annotationProcessor "edu.ucr.cs.riple.annotator:annotator-scanner:" + System.getenv('ANNOTATOR_VERSION')
55+
56+
// to add @Initializer
57+
compileOnly "com.uber.nullaway:nullaway-annotations:" + System.getenv('NULLAWAY_TEST_VERSION')
58+
// to add jetbrains annotations (testing type use vs type declaration)
59+
compileOnly 'org.jetbrains:annotations:24.0.0'
60+
compileOnly "org.jspecify:jspecify:0.3.0"
61+
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
62+
errorprone "com.google.errorprone:error_prone_core:2.31.0"
63+
errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
64+
}
65+
66+
tasks.withType(JavaCompile) {
67+
// remove the if condition if you want to run NullAway on test code
68+
if (!name.toLowerCase().contains("test")) {
69+
options.errorprone.disableAllChecks = true
70+
options.errorprone.disableAllWarnings = true
71+
options.errorprone {
72+
check("NullAway", CheckSeverity.WARN)
73+
check("AnnotatorScanner", CheckSeverity.WARN)
74+
option("NullAway:AnnotatedPackages", "test")
75+
option("NullAway:SerializeFixMetadata", "true")
76+
option("NullAway:FixSerializationConfigPath", project.getProperty(project.name + "-nullaway-config-path"))
77+
option("NullAway:AcknowledgeLibraryModelsOfAnnotatedCode", "true")
78+
option("NullAway:JSpecifyMode", project.getProperty("jspecify"))
79+
option("AnnotatorScanner:ConfigPath", project.getProperty(project.name + "-scanner-config-path"))
80+
}
81+
}
82+
options.compilerArgs << "-Xmaxerrs" << "100000"
83+
options.compilerArgs << "-Xmaxwarns" << "100000"
84+
options.fork = true
85+
}
86+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 Nima Karimipour
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
rootProject.name = 'java-21'

0 commit comments

Comments
 (0)