Skip to content

Commit

Permalink
Annotated AnnotatedElement and its implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
cpovirk authored Aug 12, 2020
1 parent 9f1bd75 commit aadd76c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -3742,7 +3742,7 @@ public Annotation[] getAnnotations() {
*/
@Override
@SuppressWarnings("unchecked")
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);

return (A) annotationData().declaredAnnotations.get(annotationClass);
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/lang/Package.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public Annotation[] getAnnotations() {
* @since 1.8
*/
@Override
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationClass) {
return getPackageInfo().getDeclaredAnnotation(annotationClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

package java.lang.reflect;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.AnnotatedFor;
import org.checkerframework.framework.qual.CFComment;

import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.security.AccessController;
Expand Down Expand Up @@ -72,6 +76,7 @@
* @revised 9
* @spec JPMS
*/
@AnnotatedFor({"nullness"})
public class AccessibleObject implements AnnotatedElement {

static void checkPermission() {
Expand Down Expand Up @@ -444,6 +449,7 @@ public boolean isAccessible() {
* @see #setAccessible(boolean)
*/
@CallerSensitive
@CFComment("Sometimes null is forbidden; other times, it is required")
public final boolean canAccess(Object obj) {
if (!Member.class.isInstance(this)) {
return override;
Expand Down Expand Up @@ -503,7 +509,7 @@ protected AccessibleObject() {}
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
throw new AssertionError("All subclasses should override this method");
}

Expand Down Expand Up @@ -538,7 +544,7 @@ public Annotation[] getAnnotations() {
* @since 1.8
*/
@Override
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
public <T extends Annotation> @Nullable T getDeclaredAnnotation(Class<T> annotationClass) {
// Only annotations on classes are inherited, for all other
// objects getDeclaredAnnotation is the same as
// getAnnotation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ default boolean isAnnotationPresent(@GuardSatisfied AnnotatedElement this, Class
* @throws NullPointerException if the given annotation class is null
* @since 1.5
*/
<T extends @Nullable Annotation> @Nullable T getAnnotation(Class<T> annotationClass);
<T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass);

/**
* Returns annotations that are <em>present</em> on this element.
Expand Down
8 changes: 6 additions & 2 deletions src/java.base/share/classes/java/lang/reflect/Executable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

package java.lang.reflect;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.AnnotatedFor;

import java.lang.annotation.*;
import java.util.Map;
import java.util.Objects;
Expand All @@ -43,6 +46,7 @@
*
* @since 1.8
*/
@AnnotatedFor({"nullness"})
public abstract class Executable extends AccessibleObject
implements Member, GenericDeclaration {
/*
Expand Down Expand Up @@ -567,7 +571,7 @@ Annotation[][] sharedGetParameterAnnotations(Class<?>[] parameterTypes,
* {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return annotationClass.cast(declaredAnnotations().get(annotationClass));
}
Expand Down Expand Up @@ -670,7 +674,7 @@ AnnotatedType getAnnotatedReturnType0(Type returnType) {
* constructor represented by this {@code Executable} or {@code null} if
* this {@code Executable} can not have a receiver parameter
*/
public AnnotatedType getAnnotatedReceiverType() {
public @Nullable AnnotatedType getAnnotatedReceiverType() {
if (Modifier.isStatic(this.getModifiers()))
return null;
return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/lang/reflect/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ Field getRoot() {
* @since 1.5
*/
@SideEffectFree
public <T extends @Nullable Annotation> @Nullable T getAnnotation(@GuardSatisfied Field this, Class<@NonNull T> annotationClass) {
public <T extends Annotation> @Nullable T getAnnotation(@GuardSatisfied Field this, Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return annotationClass.cast(declaredAnnotations().get(annotationClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

package java.lang.reflect;

import org.checkerframework.framework.qual.AnnotatedFor;

/**
* A common interface for all entities that declare type variables.
*
* @since 1.5
*/
@AnnotatedFor({"nullness"})
public interface GenericDeclaration extends AnnotatedElement {
/**
* Returns an array of {@code TypeVariable} objects that
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/lang/reflect/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ void setMethodAccessor(MethodAccessor accessor) {
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
public <T extends @Nullable Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
return super.getAnnotation(annotationClass);
}

Expand Down
6 changes: 4 additions & 2 deletions src/java.base/share/classes/java/lang/reflect/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.dataflow.qual.SideEffectFree;
import org.checkerframework.framework.qual.AnnotatedFor;

import java.lang.annotation.*;
import java.util.HashMap;
Expand All @@ -45,6 +46,7 @@
*
* @since 1.8
*/
@AnnotatedFor({"nullness"})
public final class Parameter implements AnnotatedElement {

private final String name;
Expand Down Expand Up @@ -290,7 +292,7 @@ public boolean isVarArgs() {
* {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
public <T extends Annotation> @Nullable T getAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return annotationClass.cast(declaredAnnotations().get(annotationClass));
}
Expand All @@ -316,7 +318,7 @@ public Annotation[] getDeclaredAnnotations() {
/**
* @throws NullPointerException {@inheritDoc}
*/
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
public <T extends Annotation> @Nullable T getDeclaredAnnotation(Class<T> annotationClass) {
// Only annotations on classes are inherited, for all other
// objects getDeclaredAnnotation is the same as
// getAnnotation.
Expand Down

0 comments on commit aadd76c

Please sign in to comment.