Skip to content

Commit 5910249

Browse files
tomas-langerm0mus
authored andcommitted
Nonnull and Nullable annotations added.
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
1 parent 14e5e35 commit 5910249

4 files changed

Lines changed: 158 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package jakarta.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.Retention;
21+
22+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
23+
24+
/**
25+
* The annotated element must not be null.
26+
* <p>
27+
* Annotated fields must not be null after construction has completed.
28+
* <p>
29+
* When this annotation is applied to a method it applies to the method return value.
30+
*
31+
* @see jakarta.annotation.Nullable
32+
* @since 2.0
33+
*/
34+
@Documented
35+
@Retention(RUNTIME)
36+
public @interface Nonnull {
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package jakarta.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.Retention;
21+
22+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
23+
24+
/**
25+
* The annotated element could be null under some circumstances.
26+
* <p>
27+
* In general, this means developers will have to read the documentation to
28+
* determine when a null value is acceptable and whether it is necessary to
29+
* check for a null value.
30+
* <p>
31+
* This annotation is useful mostly for overriding a {@link Nonnull} annotation.
32+
* Static analysis tools should generally treat the annotated items as though they
33+
* had no annotation, unless they are configured to minimize false negatives.
34+
* <p>
35+
* When this annotation is applied to a method it applies to the method return value.
36+
*
37+
* @see jakarta.annotation.Nonnull
38+
* @since 2.0
39+
*/
40+
@Documented
41+
@Retention(RUNTIME)
42+
public @interface Nullable {
43+
}

spec/src/main/asciidoc/annotations-spec.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2017, 2020 Contributors to the Eclipse Foundation
2+
// Copyright (c) 2017, 2021 Contributors to the Eclipse Foundation
33
//
44

55
= Jakarta Annotations

spec/src/main/asciidoc/spec.adoc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,83 @@ public @interface Priority {
732732
}
733733
----
734734

735+
=== jakarta.annotation.Nonnull
736+
737+
The _Nonnull_ annotation is used to mark
738+
elements that cannot be `null`.
739+
740+
This information can be used for validation by IDEs, static analysis tools, and runtime.
741+
742+
The annotation may be present on any target.
743+
This specification defines behavior on following targets:
744+
745+
- Method - return type will never be `null`
746+
- Parameter - parameter must not be `null`
747+
- Field - field cannot be `null` after construction of the object is completed
748+
749+
[source,java]
750+
----
751+
package jakarta.annotation;
752+
753+
import java.lang.annotation.Documented;
754+
import java.lang.annotation.Retention;
755+
756+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
757+
758+
@Documented
759+
@Retention(RUNTIME)
760+
public @interface Nonnull {
761+
}
762+
----
763+
764+
The following example shows the usage of the annotation defined above:
765+
766+
[source,java]
767+
----
768+
public interface StockQuoteService {
769+
@Nonnull
770+
BigDecimal quote(@Nonnull String marker);
771+
}
772+
----
773+
774+
=== jakarta.annotation.Nullable
775+
776+
The _Nullable_ annotation is used to mark
777+
elements that may be `null`.
778+
779+
This information can be used for validation by IDEs, static analysis tools, and runtime.
780+
781+
The annotation may be present on any target.
782+
This specification defines behavior on following targets:
783+
784+
- Method - return type may be `null`
785+
- Parameter - parameter may be `null`
786+
- Field - field may be `null`
787+
788+
[source,java]
789+
----
790+
package jakarta.annotation;
791+
792+
import java.lang.annotation.Documented;
793+
import java.lang.annotation.Retention;
794+
795+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
796+
797+
@Documented
798+
@Retention(RUNTIME)
799+
public @interface Nullable {
800+
}
801+
----
802+
803+
The following example shows the usage of the annotation defined above:
804+
805+
[source,java]
806+
----
807+
public interface StockQuoteService {
808+
BigDecimal quote(String marker, @Nullable BigDecimal defaultValue);
809+
}
810+
----
811+
735812
=== jakarta.annotation.security.RunAs
736813

737814
The _RunAs_ annotation defines the security

0 commit comments

Comments
 (0)