Skip to content

Commit c260d74

Browse files
committed
[#6] Add JSR 305 @nullable and @nonnull annotations
To limit the amound of noise introduced, a new @NonNullByDefault annotation is added. It is used as a package-level annotation. Other changes: - Updated dependencies and plugins - Workaround for a bug in FindBugs: http://sourceforge.net/p/findbugs/bugs/1385 - External annotations for IntelliJ in order to avoid warnings about overriding unannotated parameters with @nonnull
1 parent 5db2686 commit c260d74

32 files changed

+436
-187
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ plus concrete realization backed by core Java Reflection API, akin to
4343
and
4444
[`VariableElement`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/VariableElement.html))
4545
not currently implemented
46+
- Code makes use of JSR 305 annotations (e.g., `@Nullable`)
4647

4748
## License
4849

@@ -56,7 +57,7 @@ Published releases (compiled for Java 7 and up) are available on Maven Central.
5657
<dependency>
5758
<groupId>net.florianschoppmann.java</groupId>
5859
<artifactId>java-types</artifactId>
59-
<version>1.0.0</version>
60+
<version>1.0.1</version>
6061
</dependency>
6162
```
6263

pom.xml

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
Java Reflection API, akin to JEP 119.
1414

1515
This module implements those methods in interface javax.lang.model.util.Types that pertain to the Java type
16-
system, plus it provides several methods of its own. This includes, among others, determining the subtype
17-
relationship between two types or resolving actual type arguments. These methods have no equivalent in the Java
18-
Reflection API.
16+
system, plus it provides a method for resolving formal type parameters to actual type arguments. These methods
17+
have no equivalent in the Java Reflection API.
1918

2019
The abstract skeletal implementation in this module is meant to be specialized for a particular javax.lang.model
2120
implementation. The abstraction makes this module well-suited to be used in projects that contain
@@ -62,32 +61,33 @@
6261

6362
<properties>
6463
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
65-
<codehaus.findbugs.version>3.0.0</codehaus.findbugs.version>
66-
<github.maven.version>0.10</github.maven.version>
67-
<jacoco.version>0.7.2.201409121644</jacoco.version>
64+
<findbugs.annotations.version>3.0.0</findbugs.annotations.version>
65+
<findbugs.plugin.version>3.0.1</findbugs.plugin.version>
66+
<github.maven.version>0.11</github.maven.version>
67+
<jacoco.version>0.7.4.201502262128</jacoco.version>
6868
<java.version>1.7</java.version>
69-
<maven.compiler.version>3.2</maven.compiler.version>
70-
<maven.checkstyle.version>2.13</maven.checkstyle.version>
71-
<!-- Pending resolution of https://jira.codehaus.org/browse/MJAVADOC-414, we use an old version of the Javadoc
72-
plugin. -->
73-
<maven.javadoc.version>2.9.1</maven.javadoc.version>
74-
<maven.jar.version>2.5</maven.jar.version>
69+
<jsr305.version>3.0.0</jsr305.version>
70+
<maven.compiler.version>3.3</maven.compiler.version>
71+
<maven.checkstyle.version>2.15</maven.checkstyle.version>
72+
<maven.javadoc.version>2.10.3</maven.javadoc.version>
73+
<maven.jar.version>2.6</maven.jar.version>
7574
<maven.jxr.version>2.5</maven.jxr.version>
76-
<maven.pmd.version>3.3</maven.pmd.version>
77-
<maven.gpg.version>1.5</maven.gpg.version>
78-
<maven.project-info-reports.version>2.7</maven.project-info-reports.version>
79-
<maven.release.version>2.5.1</maven.release.version>
75+
<maven.pmd.version>3.4</maven.pmd.version>
76+
<maven.gpg.version>1.6</maven.gpg.version>
77+
<maven.project-info-reports.version>2.8</maven.project-info-reports.version>
78+
<maven.release.version>2.5.2</maven.release.version>
8079
<maven.source.version>2.4</maven.source.version>
8180
<nexus.staging.version>1.6.5</nexus.staging.version>
82-
<testng.version>6.8.13</testng.version>
81+
<testng.version>6.9.4</testng.version>
8382
</properties>
8483

8584
<dependencies>
8685
<dependency>
8786
<groupId>com.google.code.findbugs</groupId>
88-
<artifactId>annotations</artifactId>
89-
<version>${codehaus.findbugs.version}</version>
90-
<scope>provided</scope>
87+
<artifactId>jsr305</artifactId>
88+
<version>${jsr305.version}</version>
89+
<!-- Needed only for annotations -->
90+
<optional>true</optional>
9191
</dependency>
9292
<dependency>
9393
<groupId>org.testng</groupId>
@@ -138,7 +138,7 @@
138138
<plugin>
139139
<groupId>org.codehaus.mojo</groupId>
140140
<artifactId>findbugs-maven-plugin</artifactId>
141-
<version>${codehaus.findbugs.version}</version>
141+
<version>${findbugs.plugin.version}</version>
142142
</plugin>
143143
</plugins>
144144
</pluginManagement>
@@ -214,6 +214,7 @@
214214

215215
<reporting>
216216
<plugins>
217+
<!-- Maven 3.2.1 seems not to honor the configured versions from build -> pluginManagement. -->
217218
<plugin>
218219
<groupId>org.apache.maven.plugins</groupId>
219220
<artifactId>maven-project-info-reports-plugin</artifactId>
@@ -227,10 +228,12 @@
227228
<plugin>
228229
<groupId>org.apache.maven.plugins</groupId>
229230
<artifactId>maven-jxr-plugin</artifactId>
231+
<version>${maven.jxr.version}</version>
230232
</plugin>
231233
<plugin>
232234
<groupId>org.apache.maven.plugins</groupId>
233235
<artifactId>maven-checkstyle-plugin</artifactId>
236+
<version>${maven.checkstyle.version}</version>
234237
<reportSets>
235238
<reportSet>
236239
<reports>
@@ -246,6 +249,7 @@
246249
<plugin>
247250
<groupId>org.apache.maven.plugins</groupId>
248251
<artifactId>maven-pmd-plugin</artifactId>
252+
<version>${maven.pmd.version}</version>
249253
<configuration>
250254
<printFailingErrors>true</printFailingErrors>
251255
<targetJdk>${java.version}</targetJdk>
@@ -257,14 +261,17 @@
257261
<plugin>
258262
<groupId>org.codehaus.mojo</groupId>
259263
<artifactId>findbugs-maven-plugin</artifactId>
264+
<version>${findbugs.annotations.version}</version>
260265
<configuration>
261266
<effort>Max</effort>
262267
<xmlOutput>true</xmlOutput>
268+
<excludeFilterFile>${project.basedir}/src/main/config/findbugs-exclude.xml</excludeFilterFile>
263269
</configuration>
264270
</plugin>
265271
<plugin>
266272
<groupId>org.jacoco</groupId>
267273
<artifactId>jacoco-maven-plugin</artifactId>
274+
<version>${jacoco.version}</version>
268275
</plugin>
269276
</plugins>
270277
</reporting>

src/main/config/findbugs-exclude.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<FindBugsFilter>
2+
<!-- Due to http://sourceforge.net/p/findbugs/bugs/1385/ exclude warnings about tightening nullness annotation on
3+
parameter. -->
4+
<Match>
5+
<Method name="equals" params="java.lang.Object" returns="boolean" />
6+
<Bug pattern="NP_METHOD_PARAMETER_TIGHTENS_ANNOTATION" />
7+
</Match>
8+
</FindBugsFilter>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<root>
2+
<item name='java.lang.reflect.InvocationHandler java.lang.Object invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) 0'>
3+
<annotation name='javax.annotation.Nonnull'/>
4+
</item>
5+
<item name='java.lang.reflect.InvocationHandler java.lang.Object invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) 1'>
6+
<annotation name='javax.annotation.Nonnull'/>
7+
</item>
8+
<item name='java.lang.reflect.InvocationHandler java.lang.Object invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) 2'>
9+
<annotation name='javax.annotation.Nonnull'/>
10+
</item>
11+
</root>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<root>
2+
<item name='javax.lang.model.element.Element R accept(javax.lang.model.element.ElementVisitor&lt;R,P&gt;, P) 0'>
3+
<annotation name='javax.annotation.Nonnull'/>
4+
</item>
5+
<item name='javax.lang.model.element.ElementVisitor R visitExecutable(javax.lang.model.element.ExecutableElement, P) 0'>
6+
<annotation name='javax.annotation.Nonnull'/>
7+
</item>
8+
<item name='javax.lang.model.element.ElementVisitor R visitPackage(javax.lang.model.element.PackageElement, P) 0'>
9+
<annotation name='javax.annotation.Nonnull'/>
10+
</item>
11+
<item name='javax.lang.model.element.ElementVisitor R visitType(javax.lang.model.element.TypeElement, P) 0'>
12+
<annotation name='javax.annotation.Nonnull'/>
13+
</item>
14+
<item name='javax.lang.model.element.ElementVisitor R visitTypeParameter(javax.lang.model.element.TypeParameterElement, P) 0'>
15+
<annotation name='javax.annotation.Nonnull'/>
16+
</item>
17+
<item name='javax.lang.model.element.ElementVisitor R visitVariable(javax.lang.model.element.VariableElement, P) 0'>
18+
<annotation name='javax.annotation.Nonnull'/>
19+
</item>
20+
<item name='javax.lang.model.element.Name boolean contentEquals(java.lang.CharSequence) 0'>
21+
<annotation name='javax.annotation.Nonnull'/>
22+
</item>
23+
</root>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<root>
2+
<item name='javax.lang.model.type.TypeMirror R accept(javax.lang.model.type.TypeVisitor&lt;R,P&gt;, P) 0'>
3+
<annotation name='javax.annotation.Nonnull'/>
4+
</item>
5+
</root>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<root>
2+
<item name='javax.lang.model.util.AbstractTypeVisitor6 R visitUnknown(javax.lang.model.type.TypeMirror, P) 0'>
3+
<annotation name='javax.annotation.Nonnull'/>
4+
</item>
5+
<item name='javax.lang.model.util.SimpleTypeVisitor6 R defaultAction(javax.lang.model.type.TypeMirror, P) 0'>
6+
<annotation name='javax.annotation.Nonnull'/>
7+
</item>
8+
<item name='javax.lang.model.util.SimpleTypeVisitor6 R visitArray(javax.lang.model.type.ArrayType, P) 0'>
9+
<annotation name='javax.annotation.Nonnull'/>
10+
</item>
11+
<item name='javax.lang.model.util.SimpleTypeVisitor6 R visitDeclared(javax.lang.model.type.DeclaredType, P) 0'>
12+
<annotation name='javax.annotation.Nonnull'/>
13+
</item>
14+
<item name='javax.lang.model.util.SimpleTypeVisitor6 R visitNull(javax.lang.model.type.NullType, P) 0'>
15+
<annotation name='javax.annotation.Nonnull'/>
16+
</item>
17+
<item name='javax.lang.model.util.SimpleTypeVisitor6 R visitTypeVariable(javax.lang.model.type.TypeVariable, P) 0'>
18+
<annotation name='javax.annotation.Nonnull'/>
19+
</item>
20+
<item name='javax.lang.model.util.SimpleTypeVisitor6 R visitWildcard(javax.lang.model.type.WildcardType, P) 0'>
21+
<annotation name='javax.annotation.Nonnull'/>
22+
</item>
23+
<item name='javax.lang.model.util.Types boolean contains(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 0'>
24+
<annotation name='javax.annotation.Nonnull'/>
25+
</item>
26+
<item name='javax.lang.model.util.Types boolean contains(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 1'>
27+
<annotation name='javax.annotation.Nonnull'/>
28+
</item>
29+
<item name='javax.lang.model.util.Types boolean isAssignable(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 0'>
30+
<annotation name='javax.annotation.Nonnull'/>
31+
</item>
32+
<item name='javax.lang.model.util.Types boolean isAssignable(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 1'>
33+
<annotation name='javax.annotation.Nonnull'/>
34+
</item>
35+
<item name='javax.lang.model.util.Types boolean isSameType(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 0'>
36+
<annotation name='javax.annotation.Nonnull'/>
37+
</item>
38+
<item name='javax.lang.model.util.Types boolean isSameType(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 1'>
39+
<annotation name='javax.annotation.Nonnull'/>
40+
</item>
41+
<item name='javax.lang.model.util.Types boolean isSubsignature(javax.lang.model.type.ExecutableType, javax.lang.model.type.ExecutableType) 0'>
42+
<annotation name='javax.annotation.Nonnull'/>
43+
</item>
44+
<item name='javax.lang.model.util.Types boolean isSubsignature(javax.lang.model.type.ExecutableType, javax.lang.model.type.ExecutableType) 1'>
45+
<annotation name='javax.annotation.Nonnull'/>
46+
</item>
47+
<item name='javax.lang.model.util.Types boolean isSubtype(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 0'>
48+
<annotation name='javax.annotation.Nonnull'/>
49+
</item>
50+
<item name='javax.lang.model.util.Types boolean isSubtype(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror) 1'>
51+
<annotation name='javax.annotation.Nonnull'/>
52+
</item>
53+
<item name='javax.lang.model.util.Types java.util.List&lt;? extends javax.lang.model.type.TypeMirror&gt; directSupertypes(javax.lang.model.type.TypeMirror) 0'>
54+
<annotation name='javax.annotation.Nonnull'/>
55+
</item>
56+
<item name='javax.lang.model.util.Types javax.lang.model.element.Element asElement(javax.lang.model.type.TypeMirror) 0'>
57+
<annotation name='javax.annotation.Nonnull'/>
58+
</item>
59+
<item name='javax.lang.model.util.Types javax.lang.model.element.TypeElement boxedClass(javax.lang.model.type.PrimitiveType) 0'>
60+
<annotation name='javax.annotation.Nonnull'/>
61+
</item>
62+
<item name='javax.lang.model.util.Types javax.lang.model.type.ArrayType getArrayType(javax.lang.model.type.TypeMirror) 0'>
63+
<annotation name='javax.annotation.Nonnull'/>
64+
</item>
65+
<item name='javax.lang.model.util.Types javax.lang.model.type.DeclaredType getDeclaredType(javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror...) 0'>
66+
<annotation name='javax.annotation.Nonnull'/>
67+
</item>
68+
<item name='javax.lang.model.util.Types javax.lang.model.type.DeclaredType getDeclaredType(javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror...) 1'>
69+
<annotation name='javax.annotation.Nonnull'/>
70+
</item>
71+
<item name='javax.lang.model.util.Types javax.lang.model.type.DeclaredType getDeclaredType(javax.lang.model.type.DeclaredType, javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror...) 1'>
72+
<annotation name='javax.annotation.Nonnull'/>
73+
</item>
74+
<item name='javax.lang.model.util.Types javax.lang.model.type.DeclaredType getDeclaredType(javax.lang.model.type.DeclaredType, javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror...) 2'>
75+
<annotation name='javax.annotation.Nonnull'/>
76+
</item>
77+
<item name='javax.lang.model.util.Types javax.lang.model.type.NoType getNoType(javax.lang.model.type.TypeKind) 0'>
78+
<annotation name='javax.annotation.Nonnull'/>
79+
</item>
80+
<item name='javax.lang.model.util.Types javax.lang.model.type.PrimitiveType getPrimitiveType(javax.lang.model.type.TypeKind) 0'>
81+
<annotation name='javax.annotation.Nonnull'/>
82+
</item>
83+
<item name='javax.lang.model.util.Types javax.lang.model.type.PrimitiveType unboxedType(javax.lang.model.type.TypeMirror) 0'>
84+
<annotation name='javax.annotation.Nonnull'/>
85+
</item>
86+
<item name='javax.lang.model.util.Types javax.lang.model.type.TypeMirror asMemberOf(javax.lang.model.type.DeclaredType, javax.lang.model.element.Element) 0'>
87+
<annotation name='javax.annotation.Nonnull'/>
88+
</item>
89+
<item name='javax.lang.model.util.Types javax.lang.model.type.TypeMirror asMemberOf(javax.lang.model.type.DeclaredType, javax.lang.model.element.Element) 1'>
90+
<annotation name='javax.annotation.Nonnull'/>
91+
</item>
92+
<item name='javax.lang.model.util.Types javax.lang.model.type.TypeMirror capture(javax.lang.model.type.TypeMirror) 0'>
93+
<annotation name='javax.annotation.Nonnull'/>
94+
</item>
95+
<item name='javax.lang.model.util.Types javax.lang.model.type.TypeMirror erasure(javax.lang.model.type.TypeMirror) 0'>
96+
<annotation name='javax.annotation.Nonnull'/>
97+
</item>
98+
</root>

src/main/java/net/florianschoppmann/java/reflect/ArrayTypeImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.florianschoppmann.java.reflect;
22

3+
import javax.annotation.Nullable;
34
import javax.lang.model.type.ArrayType;
45
import javax.lang.model.type.TypeKind;
56
import javax.lang.model.type.TypeVisitor;
@@ -15,7 +16,7 @@ final class ArrayTypeImpl extends AnnotatedConstructImpl implements ReflectionTy
1516
}
1617

1718
@Override
18-
public boolean equals(Object otherObject) {
19+
public boolean equals(@Nullable Object otherObject) {
1920
if (this == otherObject) {
2021
return true;
2122
} else if (otherObject == null || getClass() != otherObject.getClass()) {
@@ -38,7 +39,7 @@ public String toString() {
3839
}
3940

4041
@Override
41-
public <R, P> R accept(TypeVisitor<R, P> visitor, P parameter) {
42+
public <R, P> R accept(TypeVisitor<R, P> visitor, @Nullable P parameter) {
4243
return visitor.visitArray(this, parameter);
4344
}
4445

src/main/java/net/florianschoppmann/java/reflect/DeclaredTypeImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.florianschoppmann.java.reflect;
22

3+
import javax.annotation.Nullable;
34
import javax.lang.model.type.DeclaredType;
45
import javax.lang.model.type.TypeKind;
56
import javax.lang.model.type.TypeVisitor;
@@ -23,7 +24,7 @@ final class DeclaredTypeImpl extends AnnotatedConstructImpl implements Reflectio
2324
}
2425

2526
@Override
26-
public boolean equals(Object otherObject) {
27+
public boolean equals(@Nullable Object otherObject) {
2728
if (this == otherObject) {
2829
return true;
2930
} else if (otherObject == null || getClass() != otherObject.getClass()) {
@@ -47,7 +48,7 @@ public String toString() {
4748
}
4849

4950
@Override
50-
public <R, P> R accept(TypeVisitor<R, P> visitor, P parameter) {
51+
public <R, P> R accept(TypeVisitor<R, P> visitor, @Nullable P parameter) {
5152
return visitor.visitDeclared(this, parameter);
5253
}
5354

src/main/java/net/florianschoppmann/java/reflect/ImmutableList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.florianschoppmann.java.reflect;
22

3+
import javax.annotation.Nullable;
34
import java.util.AbstractList;
45
import java.util.List;
56

@@ -9,8 +10,6 @@ final class ImmutableList<E> extends AbstractList<E> {
910
private final Object[] array;
1011

1112
private ImmutableList(Object[] array) {
12-
assert array != null;
13-
1413
this.array = array;
1514
}
1615

@@ -28,6 +27,7 @@ static <E> ImmutableList<E> emptyList() {
2827
}
2928

3029
@Override
30+
@Nullable
3131
@SuppressWarnings("unchecked")
3232
public E get(int index) {
3333
return (E) array[index];

src/main/java/net/florianschoppmann/java/reflect/IntersectionTypeImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.florianschoppmann.java.type.IntersectionType;
44

5+
import javax.annotation.Nullable;
56
import javax.lang.model.type.TypeKind;
67
import javax.lang.model.type.TypeMirror;
78
import javax.lang.model.type.TypeVisitor;
@@ -18,7 +19,7 @@ final class IntersectionTypeImpl extends AnnotatedConstructImpl implements Inter
1819
}
1920

2021
@Override
21-
public boolean equals(Object otherObject) {
22+
public boolean equals(@Nullable Object otherObject) {
2223
if (this == otherObject) {
2324
return true;
2425
} else if (otherObject == null || getClass() != otherObject.getClass()) {
@@ -41,7 +42,7 @@ public String toString() {
4142
}
4243

4344
@Override
44-
public <R, P> R accept(TypeVisitor<R, P> visitor, P parameter) {
45+
public <R, P> R accept(TypeVisitor<R, P> visitor, @Nullable P parameter) {
4546
return visitor.visitUnknown(this, parameter);
4647
}
4748

0 commit comments

Comments
 (0)