Skip to content

Commit

Permalink
Set language level to 17 in java parser and upgrade the dependency (#223
Browse files Browse the repository at this point in the history
)

Fixes #88

This PR sets parser configuration to `17` to enable Annotator to work on source code written in java 17.

To support java 17 sources, we are only required to update java parser configuration, no change in `core` implementation is required. 

Our default java version for publishing/compiling/testing is 11. This PR adds a CI job (named `parser-coinfiguration-17`) to specifically execute tests in `Java17Test` with java version 17. 


Please note this PR also upgrades `javaparser` dependency to `3.25.7`.
  • Loading branch information
nimakarimipour authored Feb 1, 2024
1 parent 67e2ded commit 5c95d42
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 54 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,18 @@ jobs:
ANNOTATOR_TEST_DISABLE_PARALLEL_PROCESSING: "true"
ANNOTATOR_TEST_DISABLE_CACHING: "true"
run: ./gradlew build --rerun-tasks --scan
parser-coinfiguration-17:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: 17
distribution: 'temurin'
- name: Build with Gradle
env:
ANNOTATOR_TEST_DISABLE_PARALLEL_PROCESSING: "false"
ANNOTATOR_TEST_DISABLE_CACHING: "false"
run: ./gradlew :annotator-core:test --tests "edu.ucr.cs.riple.core.Java17Test"

10 changes: 10 additions & 0 deletions annotator-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ jar {
shadowJar {
archiveClassifier = null
}

// Exclude tests not supported by Java 11.
if(JavaVersion.current().isJava11()){
// exclude Java17Test which is designed to test Java 17 features.
test {
filter {
excludeTestsMatching "edu.ucr.cs.riple.core.Java17Test"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package edu.ucr.cs.riple.core.registries.field;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.BodyDeclaration;
Expand All @@ -36,13 +35,12 @@
import edu.ucr.cs.riple.core.module.ModuleConfiguration;
import edu.ucr.cs.riple.core.registries.Registry;
import edu.ucr.cs.riple.injector.Helper;
import edu.ucr.cs.riple.injector.Injector;
import edu.ucr.cs.riple.injector.exceptions.TargetClassNotFound;
import edu.ucr.cs.riple.injector.location.Location;
import edu.ucr.cs.riple.injector.location.OnClass;
import edu.ucr.cs.riple.injector.location.OnField;
import edu.ucr.cs.riple.scanner.Serializer;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Optional;
Expand Down Expand Up @@ -99,45 +97,41 @@ protected Builder<ClassFieldRecord> getBuilder() {
String clazz = values[0];
// Path to class.
Path path = Helper.deserializePath(values[1]);
CompilationUnit tree;
CompilationUnit tree = Injector.parse(path);
if (tree == null) {
return null;
}
NodeList<BodyDeclaration<?>> members;
try {
tree = StaticJavaParser.parse(path);
NodeList<BodyDeclaration<?>> members;
try {
members = Helper.getTypeDeclarationMembersByFlatName(tree, clazz);
} catch (TargetClassNotFound notFound) {
System.err.println(notFound.getMessage());
return null;
}
ClassFieldRecord record = new ClassFieldRecord(path, clazz);
members.forEach(
bodyDeclaration ->
bodyDeclaration.ifFieldDeclaration(
fieldDeclaration -> {
NodeList<VariableDeclarator> vars = fieldDeclaration.getVariables();
if (vars.getFirst().isEmpty()) {
// unexpected but just in case.
return;
}
record.addFieldDeclaration(fieldDeclaration);
// Collect uninitialized fields at declaration.
vars.forEach(
variableDeclarator -> {
String fieldName = variableDeclarator.getNameAsString();
if (variableDeclarator.getInitializer().isEmpty()) {
uninitializedFields.put(clazz, fieldName);
}
});
}));
// We still want to keep the information about the class even if it has no field
// declarations, so we can retrieve tha path to the file from the given class flat name.
// This information is used in adding suppression annotations on class level.
return record;
} catch (FileNotFoundException e) {
members = Helper.getTypeDeclarationMembersByFlatName(tree, clazz);
} catch (TargetClassNotFound notFound) {
System.err.println(notFound.getMessage());
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
ClassFieldRecord record = new ClassFieldRecord(path, clazz);
members.forEach(
bodyDeclaration ->
bodyDeclaration.ifFieldDeclaration(
fieldDeclaration -> {
NodeList<VariableDeclarator> vars = fieldDeclaration.getVariables();
if (vars.getFirst().isEmpty()) {
// unexpected but just in case.
return;
}
record.addFieldDeclaration(fieldDeclaration);
// Collect uninitialized fields at declaration.
vars.forEach(
variableDeclarator -> {
String fieldName = variableDeclarator.getNameAsString();
if (variableDeclarator.getInitializer().isEmpty()) {
uninitializedFields.put(clazz, fieldName);
}
});
}));
// We still want to keep the information about the class even if it has no field
// declarations, so we can retrieve tha path to the file from the given class flat name.
// This information is used in adding suppression annotations on class level.
return record;
};
}

Expand Down
60 changes: 60 additions & 0 deletions annotator-core/src/test/java/edu/ucr/cs/riple/core/Java17Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* MIT License
*
* Copyright (c) 2023 Nima Karimipour
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package edu.ucr.cs.riple.core;

import edu.ucr.cs.riple.core.tools.TReport;
import edu.ucr.cs.riple.injector.location.OnField;
import java.util.Set;
import org.junit.Test;

/**
* Tests in this class are related to Java 17 features. These tests include blocks of code that are
* not syntactically supported by java 11.
*/
public class Java17Test extends AnnotatorBaseCoreTest {

public Java17Test() {
super("java-17");
}

@Test
public void patternMatchingInJava17Test() {
coreTestHelper
.onTarget()
.withSourceLines(
"Main.java",
"package test;",
"public class Main {",
" Object f1, f2, f3, f4;",
" void foo() {",
" // this block is not supported by java 11",
" if(f1 instanceof String s) { }",
" }",
"}")
.withExpectedReports(
new TReport(new OnField("Main.java", "test.Main", Set.of("f1", "f2", "f3", "f4")), -4))
.start();
}
}
83 changes: 83 additions & 0 deletions annotator-core/src/test/resources/templates/java-17/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2023 Nima Karimipour
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

import net.ltgt.gradle.errorprone.CheckSeverity

plugins{
id "net.ltgt.errorprone" version "2.0.1" apply false
}

subprojects {
apply plugin: "java"
apply plugin: "net.ltgt.errorprone"

repositories {
mavenLocal()
mavenCentral()
google()
}

def libraryloader = project.getProperty("library-model-loader-path")

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
if(project.name != "Target"){
compileOnly project(":Target")
annotationProcessor files(libraryloader)
}
annotationProcessor "com.uber.nullaway:nullaway:" + System.getenv('NULLAWAY_TEST_VERSION')
annotationProcessor "edu.ucr.cs.riple.annotator:annotator-scanner:" + System.getenv('ANNOTATOR_VERSION')

// to add @Initializer
compileOnly 'com.uber.nullaway:nullaway-annotations:0.10.10'
// to add jetbrains annotations (testing type use vs type declaration)
compileOnly 'org.jetbrains:annotations:24.0.0'
compileOnly "org.jspecify:jspecify:0.3.0"
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
errorprone "com.google.errorprone:error_prone_core:2.3.2"
errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
}

tasks.withType(JavaCompile) {
// remove the if condition if you want to run NullAway on test code
if (!name.toLowerCase().contains("test")) {
options.errorprone.disableAllChecks = true
options.errorprone.disableAllWarnings = true
options.errorprone {
check("NullAway", CheckSeverity.WARN)
check("AnnotatorScanner", CheckSeverity.WARN)
option("NullAway:AnnotatedPackages", "test")
option("NullAway:SerializeFixMetadata", "true")
option("NullAway:FixSerializationConfigPath", project.getProperty(project.name + "-nullaway-config-path"))
option("NullAway:AcknowledgeLibraryModelsOfAnnotatedCode", "true")
option("AnnotatorScanner:ConfigPath", project.getProperty(project.name + "-scanner-config-path"))
}
}
options.compilerArgs << "-Xmaxerrs" << "100000"
options.compilerArgs << "-Xmaxwarns" << "100000"
options.fork = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* MIT License
*
* Copyright (c) 2023 Nima Karimipour
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

rootProject.name = 'java-17'
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ subprojects { proj ->
}
}
}
options.fork = true
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def versions = [
errorProne : defaultErrorProneVersion,
errorProneApi : project.hasProperty("epApiVersion") ? epApiVersion : oldestErrorProneApi,
autoService : "1.0-rc7",
javaparser : "3.24.0",
javaparser : "3.25.7",
json : "1.1.1",
guava : "31.0.1-jre",
cli : "1.5.0",
Expand Down
17 changes: 7 additions & 10 deletions injector/src/main/java/edu/ucr/cs/riple/injector/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package edu.ucr.cs.riple.injector;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
Expand All @@ -46,7 +45,6 @@
import com.github.javaparser.ast.type.Type;
import com.google.common.base.Preconditions;
import edu.ucr.cs.riple.injector.exceptions.TargetClassNotFound;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -543,15 +541,14 @@ public static Path deserializePath(String serializedPath) {
* @return true if src has a package declaration and starts with root.
*/
public static boolean srcIsUnderClassClassPath(Path path, String rootPackage) {
try {
CompilationUnit cu = StaticJavaParser.parse(path.toFile());
Optional<PackageDeclaration> packageDeclaration = cu.getPackageDeclaration();
return packageDeclaration
.map(declaration -> declaration.getNameAsString().startsWith(rootPackage))
.orElse(false);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("File not found: " + path, e);
CompilationUnit cu = Injector.parse(path);
if (cu == null) {
return false;
}
Optional<PackageDeclaration> packageDeclaration = cu.getPackageDeclaration();
return packageDeclaration
.map(declaration -> declaration.getNameAsString().startsWith(rootPackage))
.orElse(false);
}

/**
Expand Down
Loading

0 comments on commit 5c95d42

Please sign in to comment.