Skip to content

Commit 70a8094

Browse files
authored
Merge pull request #878 from ascopes/task/GH-564-nullaway
Implement nullability analysis, fix errorprone and nullability issues
2 parents 793aa5c + 327200a commit 70a8094

58 files changed

Lines changed: 464 additions & 143 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ updates:
1313
- ci
1414
- package-ecosystem: maven
1515
directory: /
16-
groups:
17-
# Group all Maven updates into a single PR.
18-
maven:
19-
patterns:
20-
- "*"
2116
open-pull-requests-limit: 99
2217
rebase-strategy: auto
2318
schedule:

.mvn/jvm.config

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
-XX:+TieredCompilation
1+
-Xshare:auto
22
-XX:TieredStopAtLevel=1
3+
-XX:+CrashOnOutOfMemoryError
4+
-XX:+TieredCompilation
5+
-XX:+UseParallelGC
6+
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
7+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
8+
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
9+
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
10+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
11+
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
12+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
13+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
14+
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
15+
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED

java-compiler-testing/pom.xml

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
<groupId>org.apache.maven.plugins</groupId>
116116
<artifactId>maven-compiler-plugin</artifactId>
117117
<configuration>
118-
<compilerArgs>
118+
<annotationProcessorPaths combine.children="append"/>
119+
<compilerArgs combine.children="append">
119120
<!--
120121
Disable warnings about modules. We cannot do much about some of these due to how Maven
121122
works with modules with Surefire.
@@ -214,5 +215,69 @@
214215
</pluginManagement>
215216
</build>
216217
</profile>
218+
219+
<profile>
220+
<!-- ErrorProne only works with Java 21 and newer. If we're on Java 17, we cannot
221+
enable it within the compiler. This is somewhat unfortunate but should be okay
222+
as a workaround for now since we build on multiple JDKs on GitHub. -->
223+
<id>errorprone</id>
224+
<activation>
225+
<jdk>[21,)</jdk>
226+
</activation>
227+
228+
<build>
229+
<plugins>
230+
<plugin>
231+
<groupId>org.apache.maven.plugins</groupId>
232+
<artifactId>maven-compiler-plugin</artifactId>
233+
234+
<configuration>
235+
<annotationProcessorPaths>
236+
<annotationProcessorPath>
237+
<groupId>com.google.errorprone</groupId>
238+
<artifactId>error_prone_core</artifactId>
239+
<version>${errorprone.version}</version>
240+
</annotationProcessorPath>
241+
<annotationProcessorPath>
242+
<!-- Override what Maven is pulling in. This is fine during compilation as our
243+
code doesn't use Guava at all. This is needed for ErrorProne to not crash
244+
on startup. -->
245+
<groupId>com.google.guava</groupId>
246+
<artifactId>guava</artifactId>
247+
<version>[33,)</version>
248+
</annotationProcessorPath>
249+
<annotationProcessorPath>
250+
<groupId>com.uber.nullaway</groupId>
251+
<artifactId>nullaway</artifactId>
252+
<version>${nullaway.version}</version>
253+
</annotationProcessorPath>
254+
</annotationProcessorPaths>
255+
256+
<compilerArgs>
257+
<!--
258+
Disable warnings about modules. We cannot do much about some of these due to how Maven
259+
works with modules with Surefire.
260+
-->
261+
<arg>-Xlint:-module</arg>
262+
<!-- ErrorProne configuration -->
263+
<compilerArg>--should-stop=ifError=FLOW</compilerArg>
264+
<compilerArg>
265+
-Xplugin:ErrorProne
266+
-XepDisableWarningsInGeneratedCode
267+
-XepExcludedPaths:.*/target/generated.*?/.*
268+
-XepOpt:NullAway:AnnotatedPackages=io.github.ascopes.jct
269+
<!-- See https://github.com/uber/NullAway/issues/1319 -->
270+
-XepOpt:NullAway:ExcludedFieldAnnotations=org.junit.jupiter.*,org.mockito.*
271+
-XepOpt:NullAway:KnownInitializers=io.github.ascopes.jct.annotations.Initializer,io.github.ascopes.jct.junit.Managed
272+
</compilerArg>
273+
<!-- Needed for ErrorProne to be able to run -->
274+
<compilerArg>-XDaddTypeAnnotationsToSymbol=true</compilerArg>
275+
<compilerArg>-XDcompilePolicy=simple</compilerArg>
276+
</compilerArgs>
277+
</configuration>
278+
</plugin>
279+
</plugins>
280+
</build>
281+
</profile>
217282
</profiles>
218283
</project>

java-compiler-testing/src/it/google-error-prone/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
<!-- Workaround for https://youtrack.jetbrains.com/issue/IDEA-317391 -->
5050
-DmvnArgLinePropagated=true
5151
</argLine>
52-
53-
<error-prone.version>2.36.0</error-prone.version>
5452
</properties>
5553

5654
<dependencies>
@@ -64,7 +62,7 @@
6462
<dependency>
6563
<groupId>com.google.errorprone</groupId>
6664
<artifactId>error_prone_core</artifactId>
67-
<version>${error-prone.version}</version>
65+
<version>${errorprone.version}</version>
6866
<scope>test</scope>
6967
</dependency>
7068

java-compiler-testing/src/it/google-error-prone/selector.bsh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* limitations under the License.
1515
*/
1616
// Use bsh rather than groovy since groovy does not support arbitrary JVM bytecode versions.
17-
if (Runtime.version().toString().toLowerCase().contains("ea")) {
18-
System.out.println("Error Prone does not support EA releases of the JDK.");
17+
if (Runtime.version().major() < 21) {
18+
System.out.println("Micronaut does not support JVMs before Java 21");
1919
return false;
2020
} else {
21-
System.out.println("Detected a GA release, proceeding");
2221
return true;
2322
}

java-compiler-testing/src/it/google-error-prone/src/test/java/io/github/ascopes/jct/acceptancetests/errorprone/ErrorProneTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void happyPathsWorkAsExpected(JctCompiler compiler) {
5757
// When
5858
var compilation = compiler
5959
.addCompilerOptions(
60+
"-XDaddTypeAnnotationsToSymbol=true",
6061
"-Xplugin:ErrorProne",
6162
"-XDcompilePolicy=simple",
6263
"--should-stop=ifError=FLOW"
@@ -79,6 +80,7 @@ void sadPathsFailAsExpected(JctCompiler compiler) {
7980
// When
8081
var compilation = compiler
8182
.addCompilerOptions(
83+
"-XDaddTypeAnnotationsToSymbol=true",
8284
"-Xplugin:ErrorProne",
8385
"-XDcompilePolicy=simple",
8486
"--should-stop=ifError=FLOW"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2022 Ashley Scopes
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.ascopes.jct.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Marks a method as being known to have uncoverable code-paths by design.
25+
*
26+
* <p>The {@code Generated} suffix tells JaCoCo to ignore coverage for this method.
27+
*
28+
* @author Ashley Scopes
29+
* @since 6.0.1
30+
*/
31+
@Retention(RetentionPolicy.CLASS)
32+
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE})
33+
public @interface DeadCodeGenerated {
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2022 Ashley Scopes
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.ascopes.jct.annotations;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Marks the annotation as being an initialiser annotation, which keeps NullAway happy.
26+
*
27+
* @author Ashley Scopes
28+
* @since 6.0.1
29+
*/
30+
@Documented
31+
@Retention(RetentionPolicy.CLASS)
32+
@Target(ElementType.ANNOTATION_TYPE)
33+
public @interface Initializer {
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (C) 2022 Ashley Scopes
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
* Annotations used for various metadata purposes.
18+
*/
19+
package io.github.ascopes.jct.annotations;

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/AbstractEnumAssert.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ public abstract class AbstractEnumAssert<A extends AbstractEnumAssert<A, E>, E e
3636
extends AbstractAssert<A, E> {
3737

3838
/**
39-
* Initialize this enum assertion.
39+
* Initialise this enum assertion.
4040
*
4141
* @param value the value to assert upon.
4242
* @param selfType the type of this assertion implementation.
4343
*/
44-
@SuppressWarnings("DataFlowIssue")
4544
protected AbstractEnumAssert(@Nullable E value, Class<?> selfType) {
4645
super(value, selfType);
4746
}
@@ -139,6 +138,6 @@ public final A isNoneOfElements(Collection<E> elements) {
139138
}
140139

141140
private String description() {
142-
return String.format("%s enum value <%s>", actual.getClass().getSimpleName(), actual);
141+
return String.format("%s enum value <%s>", actual.getDeclaringClass().getSimpleName(), actual);
143142
}
144143
}

0 commit comments

Comments
 (0)