Skip to content

Commit aadd76c

Browse files
authored
Annotated AnnotatedElement and its implementations
1 parent 9f1bd75 commit aadd76c

File tree

9 files changed

+26
-11
lines changed

9 files changed

+26
-11
lines changed

src/java.base/share/classes/java/lang/Class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3742,7 +3742,7 @@ public Annotation[] getAnnotations() {
37423742
*/
37433743
@Override
37443744
@SuppressWarnings("unchecked")
3745-
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
3745+
public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationClass) {
37463746
Objects.requireNonNull(annotationClass);
37473747

37483748
return (A) annotationData().declaredAnnotations.get(annotationClass);

src/java.base/share/classes/java/lang/Package.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public Annotation[] getAnnotations() {
489489
* @since 1.8
490490
*/
491491
@Override
492-
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
492+
public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationClass) {
493493
return getPackageInfo().getDeclaredAnnotation(annotationClass);
494494
}
495495

src/java.base/share/classes/java/lang/reflect/AccessibleObject.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
package java.lang.reflect;
2727

28+
import org.checkerframework.checker.nullness.qual.Nullable;
29+
import org.checkerframework.framework.qual.AnnotatedFor;
30+
import org.checkerframework.framework.qual.CFComment;
31+
2832
import java.lang.annotation.Annotation;
2933
import java.lang.invoke.MethodHandle;
3034
import java.security.AccessController;
@@ -72,6 +76,7 @@
7276
* @revised 9
7377
* @spec JPMS
7478
*/
79+
@AnnotatedFor({"nullness"})
7580
public class AccessibleObject implements AnnotatedElement {
7681

7782
static void checkPermission() {
@@ -444,6 +449,7 @@ public boolean isAccessible() {
444449
* @see #setAccessible(boolean)
445450
*/
446451
@CallerSensitive
452+
@CFComment("Sometimes null is forbidden; other times, it is required")
447453
public final boolean canAccess(Object obj) {
448454
if (!Member.class.isInstance(this)) {
449455
return override;
@@ -503,7 +509,7 @@ protected AccessibleObject() {}
503509
* @throws NullPointerException {@inheritDoc}
504510
* @since 1.5
505511
*/
506-
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
512+
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
507513
throw new AssertionError("All subclasses should override this method");
508514
}
509515

@@ -538,7 +544,7 @@ public Annotation[] getAnnotations() {
538544
* @since 1.8
539545
*/
540546
@Override
541-
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
547+
public <T extends Annotation> @Nullable T getDeclaredAnnotation(Class<T> annotationClass) {
542548
// Only annotations on classes are inherited, for all other
543549
// objects getDeclaredAnnotation is the same as
544550
// getAnnotation.

src/java.base/share/classes/java/lang/reflect/AnnotatedElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ default boolean isAnnotationPresent(@GuardSatisfied AnnotatedElement this, Class
293293
* @throws NullPointerException if the given annotation class is null
294294
* @since 1.5
295295
*/
296-
<T extends @Nullable Annotation> @Nullable T getAnnotation(Class<T> annotationClass);
296+
<T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass);
297297

298298
/**
299299
* Returns annotations that are <em>present</em> on this element.

src/java.base/share/classes/java/lang/reflect/Executable.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
package java.lang.reflect;
2727

28+
import org.checkerframework.checker.nullness.qual.Nullable;
29+
import org.checkerframework.framework.qual.AnnotatedFor;
30+
2831
import java.lang.annotation.*;
2932
import java.util.Map;
3033
import java.util.Objects;
@@ -43,6 +46,7 @@
4346
*
4447
* @since 1.8
4548
*/
49+
@AnnotatedFor({"nullness"})
4650
public abstract class Executable extends AccessibleObject
4751
implements Member, GenericDeclaration {
4852
/*
@@ -567,7 +571,7 @@ Annotation[][] sharedGetParameterAnnotations(Class<?>[] parameterTypes,
567571
* {@inheritDoc}
568572
* @throws NullPointerException {@inheritDoc}
569573
*/
570-
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
574+
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
571575
Objects.requireNonNull(annotationClass);
572576
return annotationClass.cast(declaredAnnotations().get(annotationClass));
573577
}
@@ -670,7 +674,7 @@ AnnotatedType getAnnotatedReturnType0(Type returnType) {
670674
* constructor represented by this {@code Executable} or {@code null} if
671675
* this {@code Executable} can not have a receiver parameter
672676
*/
673-
public AnnotatedType getAnnotatedReceiverType() {
677+
public @Nullable AnnotatedType getAnnotatedReceiverType() {
674678
if (Modifier.isStatic(this.getModifiers()))
675679
return null;
676680
return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),

src/java.base/share/classes/java/lang/reflect/Field.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ Field getRoot() {
11771177
* @since 1.5
11781178
*/
11791179
@SideEffectFree
1180-
public <T extends @Nullable Annotation> @Nullable T getAnnotation(@GuardSatisfied Field this, Class<@NonNull T> annotationClass) {
1180+
public <T extends Annotation> @Nullable T getAnnotation(@GuardSatisfied Field this, Class<T> annotationClass) {
11811181
Objects.requireNonNull(annotationClass);
11821182
return annotationClass.cast(declaredAnnotations().get(annotationClass));
11831183
}

src/java.base/share/classes/java/lang/reflect/GenericDeclaration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525

2626
package java.lang.reflect;
2727

28+
import org.checkerframework.framework.qual.AnnotatedFor;
29+
2830
/**
2931
* A common interface for all entities that declare type variables.
3032
*
3133
* @since 1.5
3234
*/
35+
@AnnotatedFor({"nullness"})
3336
public interface GenericDeclaration extends AnnotatedElement {
3437
/**
3538
* Returns an array of {@code TypeVariable} objects that

src/java.base/share/classes/java/lang/reflect/Method.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ void setMethodAccessor(MethodAccessor accessor) {
714714
* @throws NullPointerException {@inheritDoc}
715715
* @since 1.5
716716
*/
717-
public <T extends @Nullable Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
717+
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
718718
return super.getAnnotation(annotationClass);
719719
}
720720

src/java.base/share/classes/java/lang/reflect/Parameter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.checkerframework.checker.nullness.qual.Nullable;
3030
import org.checkerframework.dataflow.qual.Pure;
3131
import org.checkerframework.dataflow.qual.SideEffectFree;
32+
import org.checkerframework.framework.qual.AnnotatedFor;
3233

3334
import java.lang.annotation.*;
3435
import java.util.HashMap;
@@ -45,6 +46,7 @@
4546
*
4647
* @since 1.8
4748
*/
49+
@AnnotatedFor({"nullness"})
4850
public final class Parameter implements AnnotatedElement {
4951

5052
private final String name;
@@ -290,7 +292,7 @@ public boolean isVarArgs() {
290292
* {@inheritDoc}
291293
* @throws NullPointerException {@inheritDoc}
292294
*/
293-
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
295+
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
294296
Objects.requireNonNull(annotationClass);
295297
return annotationClass.cast(declaredAnnotations().get(annotationClass));
296298
}
@@ -316,7 +318,7 @@ public Annotation[] getDeclaredAnnotations() {
316318
/**
317319
* @throws NullPointerException {@inheritDoc}
318320
*/
319-
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
321+
public <T extends Annotation> @Nullable T getDeclaredAnnotation(Class<T> annotationClass) {
320322
// Only annotations on classes are inherited, for all other
321323
// objects getDeclaredAnnotation is the same as
322324
// getAnnotation.

0 commit comments

Comments
 (0)