Skip to content

Commit

Permalink
Added support for receiver type annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Feb 5, 2016
1 parent 86e6328 commit 4db0bcf
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 99 deletions.
Expand Up @@ -1013,6 +1013,11 @@ class Latent extends InDefinedShape.AbstractBase {
*/ */
private final Object defaultValue; private final Object defaultValue;


/**
* The receiver type of this method or {@code null} if the receiver type is defined implicitly.
*/
private final TypeDescription.Generic receiverType;

/** /**
* Creates a new latent method description. All provided types are attached to this instance before they are returned. * Creates a new latent method description. All provided types are attached to this instance before they are returned.
* *
Expand All @@ -1028,7 +1033,8 @@ public Latent(TypeDescription declaringType, MethodDescription.Token token) {
token.getParameterTokens(), token.getParameterTokens(),
token.getExceptionTypes(), token.getExceptionTypes(),
token.getAnnotations(), token.getAnnotations(),
token.getDefaultValue()); token.getDefaultValue(),
token.getReceiverType());
} }


/** /**
Expand All @@ -1043,6 +1049,7 @@ public Latent(TypeDescription declaringType, MethodDescription.Token token) {
* @param exceptionTypes This method's exception types. * @param exceptionTypes This method's exception types.
* @param declaredAnnotations The annotations of this method. * @param declaredAnnotations The annotations of this method.
* @param defaultValue The default value of this method or {@code null} if no default annotation value is defined. * @param defaultValue The default value of this method or {@code null} if no default annotation value is defined.
* @param receiverType The receiver type of this method or {@code null} if the receiver type is defined implicitly.
*/ */
public Latent(TypeDescription declaringType, public Latent(TypeDescription declaringType,
String internalName, String internalName,
Expand All @@ -1052,7 +1059,8 @@ public Latent(TypeDescription declaringType,
List<? extends ParameterDescription.Token> parameterTokens, List<? extends ParameterDescription.Token> parameterTokens,
List<? extends TypeDescription.Generic> exceptionTypes, List<? extends TypeDescription.Generic> exceptionTypes,
List<? extends AnnotationDescription> declaredAnnotations, List<? extends AnnotationDescription> declaredAnnotations,
Object defaultValue) { Object defaultValue,
TypeDescription.Generic receiverType) {
this.declaringType = declaringType; this.declaringType = declaringType;
this.internalName = internalName; this.internalName = internalName;
this.modifiers = modifiers; this.modifiers = modifiers;
Expand All @@ -1062,6 +1070,7 @@ public Latent(TypeDescription declaringType,
this.exceptionTypes = exceptionTypes; this.exceptionTypes = exceptionTypes;
this.declaredAnnotations = declaredAnnotations; this.declaredAnnotations = declaredAnnotations;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.receiverType = receiverType;
} }


@Override @Override
Expand Down Expand Up @@ -1109,6 +1118,13 @@ public Object getDefaultValue() {
return defaultValue; return defaultValue;
} }


@Override
public TypeDescription.Generic getReceiverType() {
return receiverType == null
? super.getReceiverType()
: receiverType.accept(TypeDescription.Generic.Visitor.Substitutor.ForAttachment.of(this));
}

/** /**
* A method description that represents the type initializer. * A method description that represents the type initializer.
*/ */
Expand Down
Expand Up @@ -2539,7 +2539,7 @@ interface Dispatcher {
* @param type The type to represent. * @param type The type to represent.
* @return A suitable annotation reader. * @return A suitable annotation reader.
*/ */
AnnotationReader resolveSuperClass(Class<?> type); AnnotationReader resolveSuperClassType(Class<?> type);


/** /**
* Resolves a loaded type's interface type's type annotations. * Resolves a loaded type's interface type's type annotations.
Expand All @@ -2548,15 +2548,15 @@ interface Dispatcher {
* @param index The index of the interface. * @param index The index of the interface.
* @return A suitable annotation reader. * @return A suitable annotation reader.
*/ */
AnnotationReader resolveInterface(Class<?> type, int index); AnnotationReader resolveInterfaceType(Class<?> type, int index);


/** /**
* Resolves a loaded field's type's type annotations. * Resolves a loaded field's type's type annotations.
* *
* @param field The field to represent. * @param field The field to represent.
* @return A suitable annotation reader. * @return A suitable annotation reader.
*/ */
AnnotationReader resolve(Field field); AnnotationReader resolveFieldType(Field field);


/** /**
* Resolves a loaded method's return type's type annotations. * Resolves a loaded method's return type's type annotations.
Expand Down Expand Up @@ -2593,6 +2593,14 @@ interface Dispatcher {
*/ */
Generic resolveReceiverType(AccessibleObject executable); Generic resolveReceiverType(AccessibleObject executable);


/**
* Resolves the annotated type as generic type description.
*
* @param annotatedType The loaded annotated type.
* @return A description of the supplied annotated type.
*/
Generic resolve(AnnotatedElement annotatedType);

/** /**
* A dispatcher for {@link AnnotationReader}s on a legacy VM that does not support type annotations. * A dispatcher for {@link AnnotationReader}s on a legacy VM that does not support type annotations.
*/ */
Expand All @@ -2609,17 +2617,17 @@ public AnnotationReader resolveTypeVariable(TypeVariable<?> typeVariable) {
} }


@Override @Override
public AnnotationReader resolveSuperClass(Class<?> type) { public AnnotationReader resolveSuperClassType(Class<?> type) {
return NoOp.INSTANCE; return NoOp.INSTANCE;
} }


@Override @Override
public AnnotationReader resolveInterface(Class<?> type, int index) { public AnnotationReader resolveInterfaceType(Class<?> type, int index) {
return NoOp.INSTANCE; return NoOp.INSTANCE;
} }


@Override @Override
public AnnotationReader resolve(Field field) { public AnnotationReader resolveFieldType(Field field) {
return NoOp.INSTANCE; return NoOp.INSTANCE;
} }


Expand All @@ -2643,6 +2651,11 @@ public Generic resolveReceiverType(AccessibleObject executable) {
return UNDEFINED; return UNDEFINED;
} }


@Override
public Generic resolve(AnnotatedElement annotatedType) {
throw new IllegalStateException("Loaded annotated type cannot be represented on this VM");
}

@Override @Override
public String toString() { public String toString() {
return "TypeDescription.Generic.AnnotationReader.Dispatcher.ForLegacyVm." + name(); return "TypeDescription.Generic.AnnotationReader.Dispatcher.ForLegacyVm." + name();
Expand Down Expand Up @@ -2753,17 +2766,17 @@ public AnnotationReader resolveTypeVariable(TypeVariable<?> typeVariable) {
} }


@Override @Override
public AnnotationReader resolveSuperClass(Class<?> type) { public AnnotationReader resolveSuperClassType(Class<?> type) {
return new AnnotatedSuperClass(type); return new AnnotatedSuperClass(type);
} }


@Override @Override
public AnnotationReader resolveInterface(Class<?> type, int index) { public AnnotationReader resolveInterfaceType(Class<?> type, int index) {
return new AnnotatedInterfaceType(type, index); return new AnnotatedInterfaceType(type, index);
} }


@Override @Override
public AnnotationReader resolve(Field field) { public AnnotationReader resolveFieldType(Field field) {
return new AnnotatedFieldType(field); return new AnnotatedFieldType(field);
} }


Expand All @@ -2785,14 +2798,24 @@ public AnnotationReader resolveExceptionType(AccessibleObject executable, int in
@Override @Override
public Generic resolveReceiverType(AccessibleObject executable) { public Generic resolveReceiverType(AccessibleObject executable) {
try { try {
AnnotatedElement annotatedReceiver = (AnnotatedElement) getAnnotatedReceiverType.invoke(executable); return resolve((AnnotatedElement) getAnnotatedReceiverType.invoke(executable));
return annotatedReceiver == null } catch (IllegalAccessException exception) {
? UNDEFINED throw new IllegalStateException("Cannot access java.lang.reflect.Executable#getAnnotatedReceiverType", exception);
: Sort.describe((java.lang.reflect.Type) getType.invoke(annotatedReceiver), new Resolved(annotatedReceiver));
} catch (InvocationTargetException exception) { } catch (InvocationTargetException exception) {
throw new IllegalStateException("Could not access receiver type", exception); throw new IllegalStateException("Error invoking java.lang.reflect.Executable#getAnnotatedReceiverType", exception.getCause());
}
}

@Override
public Generic resolve(AnnotatedElement annotatedType) {
try {
return annotatedType == null
? UNDEFINED
: Sort.describe((java.lang.reflect.Type) getType.invoke(annotatedType), new Resolved(annotatedType));
} catch (IllegalAccessException exception) { } catch (IllegalAccessException exception) {
throw new IllegalStateException("Error when accessing receiver type", exception.getCause()); throw new IllegalStateException("Cannot access java.lang.reflect.AnnotatedType#getType", exception);
} catch (InvocationTargetException exception) {
throw new IllegalStateException("Error invoking java.lang.reflect.AnnotatedType#getType", exception.getCause());
} }
} }


Expand Down Expand Up @@ -5857,7 +5880,7 @@ public TypeDescription asErasure() {


@Override @Override
protected AnnotationReader getAnnotationReader() { protected AnnotationReader getAnnotationReader() {
return AnnotationReader.DISPATCHER.resolveSuperClass(type); return AnnotationReader.DISPATCHER.resolveSuperClassType(type);
} }
} }


Expand Down Expand Up @@ -5892,7 +5915,7 @@ public TypeDescription asErasure() {


@Override @Override
protected AnnotationReader getAnnotationReader() { protected AnnotationReader getAnnotationReader() {
return AnnotationReader.DISPATCHER.resolve(field); return AnnotationReader.DISPATCHER.resolveFieldType(field);
} }
} }


Expand Down
Expand Up @@ -704,7 +704,7 @@ public TypeDescription asErasure() {


@Override @Override
protected AnnotationReader getAnnotationReader() { protected AnnotationReader getAnnotationReader() {
return AnnotationReader.DISPATCHER.resolveInterface(type, index); return AnnotationReader.DISPATCHER.resolveInterfaceType(type, index);
} }
} }
} }
Expand Down

0 comments on commit 4db0bcf

Please sign in to comment.