Skip to content

Commit a4528f0

Browse files
author
Clint Checketts
committed
Initial @nullable annoations
1 parent 2c57b2b commit a4528f0

File tree

7 files changed

+201
-1
lines changed

7 files changed

+201
-1
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
<scope>provided</scope>
7474
<optional>true</optional>
7575
</dependency>
76+
<dependency>
77+
<!--JSR-305 only used for non-required meta-annotations-->
78+
<groupId>com.google.code.findbugs</groupId>
79+
<artifactId>jsr305</artifactId>
80+
<version>3.0.2</version>
81+
<scope>provided</scope>
82+
<optional>true</optional>
83+
</dependency>
7684
<dependency>
7785
<groupId>org.hamcrest</groupId>
7886
<artifactId>hamcrest-core</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2019 the original author or authors.
12+
*/
13+
package org.assertj.core.annotations;
14+
15+
import javax.annotation.Nonnull;
16+
import javax.annotation.meta.TypeQualifierNickname;
17+
import java.lang.annotation.Documented;
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+
* A common annotation to declare that annotated elements cannot be {@code null}.
25+
* Leverages JSR 305 meta-annotations to indicate nullability in Java to common tools with
26+
* JSR 305 support and used by Kotlin to infer nullability of the API.
27+
* <p>
28+
* <p>Should be used at parameter, return value, and field level. Method overrides should
29+
* repeat parent {@code @NonNull} annotations unless they behave differently.
30+
* <p>
31+
* <p>Use {@code @NonNullApi} (scope = parameters + return values) and/or {@code @NonNullFields}
32+
* (scope = fields) to set the default behavior to non-nullable in order to avoid annotating
33+
* your whole codebase with {@code @NonNull}.
34+
*
35+
* @see NonNullApi
36+
* @see NonNullFields
37+
* @see Nullable
38+
*/
39+
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
40+
@Retention(RetentionPolicy.RUNTIME)
41+
@Documented
42+
@Nonnull
43+
@TypeQualifierNickname
44+
public @interface NonNull {
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2019 the original author or authors.
12+
*/
13+
package org.assertj.core.annotations;
14+
15+
import javax.annotation.Nonnull;
16+
import javax.annotation.meta.TypeQualifierDefault;
17+
import java.lang.annotation.Documented;
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+
* A common annotation to declare that parameters and return values
25+
* are to be considered as non-nullable by default for a given package.
26+
* <p>
27+
* <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
28+
* tools with JSR-305 support and used by Kotlin to infer nullability of the API.
29+
* <p>
30+
* <p>Should be used at package level in association with {@link Nullable}
31+
* annotations at parameter and return value level.
32+
*
33+
* @see NonNullFields
34+
* @see Nullable
35+
* @see NonNull
36+
*/
37+
@Target({ElementType.PACKAGE, ElementType.TYPE})
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Nonnull
41+
@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER})
42+
public @interface NonNullApi {
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2019 the original author or authors.
12+
*/
13+
package org.assertj.core.annotations;
14+
15+
import javax.annotation.Nonnull;
16+
import javax.annotation.meta.TypeQualifierDefault;
17+
import java.lang.annotation.Documented;
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+
* A common annotation to declare that fields are to be considered as
25+
* non-nullable by default for a given package.
26+
*
27+
* <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
28+
* tools with JSR-305 support and used by Kotlin to infer nullability of the API.
29+
*
30+
* <p>Should be used at package level in association with {@link Nullable}
31+
* annotations at field level.
32+
*
33+
* @see NonNullFields
34+
* @see Nullable
35+
* @see NonNull
36+
*/
37+
@Target({ElementType.PACKAGE, ElementType.TYPE})
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Nonnull
41+
@TypeQualifierDefault(ElementType.FIELD)
42+
public @interface NonNullFields {
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2019 the original author or authors.
12+
*/
13+
package org.assertj.core.annotations;
14+
15+
import javax.annotation.Nonnull;
16+
import javax.annotation.meta.TypeQualifierNickname;
17+
import javax.annotation.meta.When;
18+
import java.lang.annotation.*;
19+
20+
/**
21+
* A common annotation to declare that annotated elements can be {@code null} under
22+
* some circumstance. Leverages JSR 305 meta-annotations to indicate nullability in Java
23+
* to common tools with JSR 305 support and used by Kotlin to infer nullability of the API.
24+
* <p>
25+
* <p>Should be used at parameter, return value, and field level. Methods override should
26+
* repeat parent {@code @Nullable} annotations unless they behave differently.
27+
* <p>
28+
* <p>Can be used in association with {@code NonNullApi} or {@code @NonNullFields} to
29+
* override the default non-nullable semantic to nullable.
30+
*
31+
* @see NonNullApi
32+
* @see NonNullFields
33+
* @see NonNull
34+
*/
35+
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@Documented
38+
@Nonnull(when = When.MAYBE)
39+
@TypeQualifierNickname
40+
public @interface Nullable {
41+
}

src/main/java/org/assertj/core/api/exception/PathsException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*/
1313
package org.assertj.core.api.exception;
1414

15+
import org.assertj.core.annotations.Nullable;
16+
1517
public final class PathsException extends RuntimeException {
1618
private static final long serialVersionUID = 1L;
1719

18-
public PathsException(final String message, final Throwable cause) {
20+
public PathsException(final String message, @Nullable final Throwable cause) {
1921
super(message, cause);
2022
}
2123
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2019 the original author or authors.
12+
*/
13+
@NonNullApi
14+
@NonNullFields
15+
package org.assertj.core.api.exception;
16+
17+
import org.assertj.core.annotations.NonNullApi;
18+
import org.assertj.core.annotations.NonNullFields;

0 commit comments

Comments
 (0)