Support TypeQualifierDefault from JSR 305 for nullability qualifiers
#KT-10942 Fixed
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
package javax.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.annotation.meta.TypeQualifierNickname;
|
||||
import javax.annotation.meta.When;
|
||||
|
||||
@Documented
|
||||
@TypeQualifierNickname
|
||||
@Nonnull(when = When.MAYBE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CheckForNull {
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package javax.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.annotation.meta.TypeQualifier;
|
||||
import javax.annotation.meta.TypeQualifierValidator;
|
||||
import javax.annotation.meta.When;
|
||||
|
||||
@Documented
|
||||
@TypeQualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Nonnull {
|
||||
When when() default When.ALWAYS;
|
||||
|
||||
static class Checker implements TypeQualifierValidator<Nonnull> {
|
||||
|
||||
public When forConstantValue(Nonnull qualifierqualifierArgument,
|
||||
Object value) {
|
||||
if (value == null)
|
||||
return When.NEVER;
|
||||
return When.ALWAYS;
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package javax.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.annotation.meta.TypeQualifierNickname;
|
||||
import javax.annotation.meta.When;
|
||||
|
||||
@Documented
|
||||
@TypeQualifierNickname
|
||||
@Nonnull(when = When.UNKNOWN)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Nullable {
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package javax.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.annotation.meta.TypeQualifierDefault;
|
||||
|
||||
/**
|
||||
* This annotation can be applied to a package, class or method to indicate that
|
||||
* the method parameters in that element are nonnull by default unless there is:
|
||||
* <ul>
|
||||
* <li>An explicit nullness annotation
|
||||
* <li>The method overrides a method in a superclass (in which case the
|
||||
* annotation of the corresponding parameter in the superclass applies)
|
||||
* <li>There is a default parameter annotation (like {@link ParametersAreNullableByDefault})
|
||||
* applied to a more tightly nested element.
|
||||
* </ul>
|
||||
*
|
||||
* @see Nonnull
|
||||
*/
|
||||
@Documented
|
||||
@Nonnull
|
||||
@TypeQualifierDefault(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ParametersAreNonnullByDefault {
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
package javax.annotation.meta;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This qualifier is applied to an annotation to denote that the annotation
|
||||
* should be treated as a type qualifier.
|
||||
*/
|
||||
|
||||
@Documented
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TypeQualifier {
|
||||
|
||||
/**
|
||||
* Describes the kinds of values the qualifier can be applied to. If a
|
||||
* numeric class is provided (e.g., Number.class or Integer.class) then the
|
||||
* annotation can also be applied to the corresponding primitive numeric
|
||||
* types.
|
||||
*/
|
||||
Class<?> applicableTo() default Object.class;
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package javax.annotation.meta;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This qualifier is applied to an annotation to denote that the annotation
|
||||
* defines a default type qualifier that is visible within the scope of the
|
||||
* element it is applied to.
|
||||
*/
|
||||
|
||||
@Documented
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TypeQualifierDefault {
|
||||
ElementType[] value() default {};
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package javax.annotation.meta;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* This annotation is applied to a annotation, and marks the annotation as being
|
||||
* a qualifier nickname. Applying a nickname annotation X to a element Y should
|
||||
* be interpreted as having the same meaning as applying all of annotations of X
|
||||
* (other than QualifierNickname) to Y.
|
||||
*
|
||||
* <p>
|
||||
* Thus, you might define a qualifier SocialSecurityNumber as follows:
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* <code>
|
||||
@Documented
|
||||
@TypeQualifierNickname @Pattern("[0-9]{3}-[0-9]{2}-[0-9]{4}")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface SocialSecurityNumber {
|
||||
}
|
||||
</code>
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Documented
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
public @interface TypeQualifierNickname {
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package javax.annotation.meta;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public interface TypeQualifierValidator<A extends Annotation> {
|
||||
/**
|
||||
* Given a type qualifier, check to see if a known specific constant value
|
||||
* is an instance of the set of values denoted by the qualifier.
|
||||
*
|
||||
* @param annotation
|
||||
* the type qualifier
|
||||
* @param value
|
||||
* the value to check
|
||||
* @return a value indicating whether or not the value is an member of the
|
||||
* values denoted by the type qualifier
|
||||
*/
|
||||
public @Nonnull
|
||||
When forConstantValue(@Nonnull A annotation, Object value);
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package javax.annotation.meta;
|
||||
|
||||
/**
|
||||
* Used to describe the relationship between a qualifier T and the set of values
|
||||
* S possible on an annotated element.
|
||||
*
|
||||
* In particular, an issues should be reported if an ALWAYS or MAYBE value is
|
||||
* used where a NEVER value is required, or if a NEVER or MAYBE value is used
|
||||
* where an ALWAYS value is required.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public enum When {
|
||||
/** S is a subset of T */
|
||||
ALWAYS,
|
||||
/** nothing definitive is known about the relation between S and T */
|
||||
UNKNOWN,
|
||||
/** S intersection T is non empty and S - T is nonempty */
|
||||
MAYBE,
|
||||
/** S intersection T is empty */
|
||||
NEVER;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
|
||||
|
||||
// FILE: spr/Nullable.java
|
||||
|
||||
package spr;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.meta.TypeQualifierNickname;
|
||||
import javax.annotation.meta.When;
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Nonnull(when = When.MAYBE)
|
||||
@TypeQualifierNickname
|
||||
public @interface Nullable {
|
||||
}
|
||||
|
||||
// FILE: spr/NonNullApi.java
|
||||
|
||||
package spr;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.meta.TypeQualifierDefault;
|
||||
|
||||
@Target(ElementType.CLASS)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Nonnull
|
||||
@TypeQualifierDefault({ElementType.TYPE_USE})
|
||||
public @interface NonNullApi {
|
||||
}
|
||||
|
||||
// FILE: A.java
|
||||
|
||||
import spr.*;
|
||||
|
||||
@NonNullApi
|
||||
public class A {
|
||||
public String field = null;
|
||||
|
||||
public String foo(String x, @Nullable CharSequence y) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String bar() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public java.util.List<String> baz() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
|
||||
fun main(a: A) {
|
||||
a.foo("", null)<!UNNECESSARY_SAFE_CALL!>?.<!>length
|
||||
a.foo("", null).length
|
||||
a.foo(<!NULL_FOR_NONNULL_TYPE!>null<!>, "").length
|
||||
|
||||
a.bar().length
|
||||
a.bar()<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.length
|
||||
|
||||
a.field<!UNNECESSARY_SAFE_CALL!>?.<!>length
|
||||
a.field.length
|
||||
|
||||
a.baz()<!UNSAFE_CALL!>.<!>get(0)
|
||||
a.baz()!!.get(0).get(0)
|
||||
a.baz()!!.get(0)?.get(0)
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package
|
||||
|
||||
public fun main(/*0*/ a: A): kotlin.Unit
|
||||
|
||||
@spr.NonNullApi public open class A {
|
||||
public constructor A()
|
||||
public final var field: kotlin.String
|
||||
public open fun bar(): kotlin.String
|
||||
@spr.Nullable public open fun baz(): kotlin.collections.(Mutable)List<kotlin.String!>?
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open fun foo(/*0*/ x: kotlin.String, /*1*/ @spr.Nullable y: kotlin.CharSequence?): kotlin.String
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
Reference in New Issue
Block a user