Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absorb JSR-305 annotations (after jakarta namespace change) #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions api/src/main/java/jakarta/annotation/CheckForNull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifierNickname;
import jakarta.annotation.meta.When;

/**
* The annotated element might be null, and uses of the element should check for null.
* <p>
* When this annotation is applied to a method it applies to the method return value.
*/
@Documented
@TypeQualifierNickname
@Nonnull(when = When.MAYBE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckForNull {

}
23 changes: 23 additions & 0 deletions api/src/main/java/jakarta/annotation/CheckForSigned.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifierNickname;
import jakarta.annotation.meta.When;

/**
* Used to annotate a value that may be either negative or nonnegative, and
* indicates that uses of it should check for
* negative values before using it in a way that requires the value to be
* nonnegative, and check for it being nonnegative before using it in a way that
* requires it to be negative.
*/
@Documented
@TypeQualifierNickname
@Nonnegative(when = When.MAYBE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckForSigned {

}
21 changes: 21 additions & 0 deletions api/src/main/java/jakarta/annotation/CheckReturnValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package jakarta.annotation;

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 jakarta.annotation.meta.When;

/**
* This annotation is used to denote a method whose return value should always
* be checked after invoking the method.
*/
@Documented
@Target( { ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE,
ElementType.PACKAGE })
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckReturnValue {
When when() default When.ALWAYS;
}
16 changes: 16 additions & 0 deletions api/src/main/java/jakarta/annotation/Detainted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifierNickname;
import jakarta.annotation.meta.When;

@Documented
@TypeQualifierNickname
@Untainted(when = When.ALWAYS)
@Retention(RetentionPolicy.RUNTIME)
public @interface Detainted {

}
35 changes: 35 additions & 0 deletions api/src/main/java/jakarta/annotation/MatchesPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.regex.Pattern;

import jakarta.annotation.meta.TypeQualifier;
import jakarta.annotation.meta.TypeQualifierValidator;
import jakarta.annotation.meta.When;

/**
* This annotation is used to denote String values that should always match given pattern.
* <p>
* When this annotation is applied to a method it applies to the method return value.
*/
@Documented
@TypeQualifier(applicableTo = String.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MatchesPattern {
@RegEx
String value();

int flags() default 0;

static class Checker implements TypeQualifierValidator<MatchesPattern> {
public When forConstantValue(MatchesPattern annotation, Object value) {
Pattern p = Pattern.compile(annotation.value(), annotation.flags());
if (p.matcher(((String) value)).matches())
return When.ALWAYS;
return When.NEVER;
}

}
}
45 changes: 45 additions & 0 deletions api/src/main/java/jakarta/annotation/Nonnegative.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifier;
import jakarta.annotation.meta.TypeQualifierValidator;
import jakarta.annotation.meta.When;

/**
* This annotation is used to annotate a value that should only contain nonnegative values.
* <p>
* When this annotation is applied to a method it applies to the method return value.
*/
@Documented
@TypeQualifier(applicableTo = Number.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface Nonnegative {
When when() default When.ALWAYS;

class Checker implements TypeQualifierValidator<Nonnegative> {

public When forConstantValue(Nonnegative annotation, Object v) {
if (!(v instanceof Number))
return When.NEVER;
boolean isNegative;
Number value = (Number) v;
if (value instanceof Long)
isNegative = value.longValue() < 0;
else if (value instanceof Double)
isNegative = value.doubleValue() < 0;
else if (value instanceof Float)
isNegative = value.floatValue() < 0;
else
isNegative = value.intValue() < 0;

if (isNegative)
return When.NEVER;
else
return When.ALWAYS;

}
}
}
15 changes: 15 additions & 0 deletions api/src/main/java/jakarta/annotation/Nonnull.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

import jakarta.annotation.meta.TypeQualifier;
import jakarta.annotation.meta.TypeQualifierValidator;
import jakarta.annotation.meta.When;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -32,6 +36,17 @@
* @since 2.0
*/
@Documented
@TypeQualifier
@Retention(RUNTIME)
public @interface Nonnull {
When when() default When.ALWAYS;

class Checker implements TypeQualifierValidator<Nonnull> {

public When forConstantValue(Nonnull qualifierArgument, Object value) {
if (value == null)
return When.NEVER;
return When.ALWAYS;
}
}
}
5 changes: 5 additions & 0 deletions api/src/main/java/jakarta/annotation/Nullable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

import jakarta.annotation.meta.TypeQualifierNickname;
import jakarta.annotation.meta.When;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -38,6 +41,8 @@
* @since 2.0
*/
@Documented
@TypeQualifierNickname
@Nonnull(when = When.UNKNOWN)
@Retention(RUNTIME)
public @interface Nullable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package jakarta.annotation;

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;

/**
* When this annotation is applied to a method, it indicates that if this method
* is overridden in a subclass, the overriding method should invoke this method
* (through method invocation on super).
* <p>
* An example of such method is {@link Object#finalize()}.
*/
@Documented
@Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface OverridingMethodsMustInvokeSuper {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifierDefault;

/**
* This annotation can be applied to a package, class or method to indicate that
* the method parameters in that element are nullable 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 applied to a more tightly nested element.
* </ul>
* <p>This annotation implies the same "nullness" as no annotation. However, it is different
* than having no annotation, as it is inherited and it can override a {@link ParametersAreNonnullByDefault}
* annotation at an outer scope.
*
* @see Nullable
*/
@Documented
@Nullable
@TypeQualifierDefault(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParametersAreNullableByDefault {
}
15 changes: 15 additions & 0 deletions api/src/main/java/jakarta/annotation/PropertyKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifier;
import jakarta.annotation.meta.When;

@Documented
@TypeQualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface PropertyKey {
When when() default When.ALWAYS;
}
43 changes: 43 additions & 0 deletions api/src/main/java/jakarta/annotation/RegEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import jakarta.annotation.meta.TypeQualifierNickname;
import jakarta.annotation.meta.TypeQualifierValidator;
import jakarta.annotation.meta.When;

/**
* This qualifier is used to denote String values that should be a Regular
* expression.
* <p>
* When this annotation is applied to a method it applies to the method return value.
*/
@Documented
@Syntax("RegEx")
@TypeQualifierNickname
@Retention(RetentionPolicy.RUNTIME)
public @interface RegEx {
When when() default When.ALWAYS;

static class Checker implements TypeQualifierValidator<RegEx> {

public When forConstantValue(RegEx annotation, Object value) {
if (!(value instanceof String))
return When.NEVER;

try {
Pattern.compile((String) value);
} catch (PatternSyntaxException e) {
return When.NEVER;
}
return When.ALWAYS;

}

}

}
19 changes: 19 additions & 0 deletions api/src/main/java/jakarta/annotation/Signed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jakarta.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.annotation.meta.TypeQualifierNickname;
import jakarta.annotation.meta.When;

/**
* Used to annotate a value of unknown sign.
*/
@Documented
@TypeQualifierNickname
@Nonnegative(when = When.UNKNOWN)
@Retention(RetentionPolicy.RUNTIME)
public @interface Signed {

}
Loading