Skip to content

Commit 4db0bcf

Browse files
author
Rafael Winterhalter
committed
Added support for receiver type annotations.
1 parent 86e6328 commit 4db0bcf

13 files changed

Lines changed: 230 additions & 99 deletions

File tree

byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodDescription.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,11 @@ class Latent extends InDefinedShape.AbstractBase {
10131013
*/
10141014
private final Object defaultValue;
10151015

1016+
/**
1017+
* The receiver type of this method or {@code null} if the receiver type is defined implicitly.
1018+
*/
1019+
private final TypeDescription.Generic receiverType;
1020+
10161021
/**
10171022
* Creates a new latent method description. All provided types are attached to this instance before they are returned.
10181023
*
@@ -1028,7 +1033,8 @@ public Latent(TypeDescription declaringType, MethodDescription.Token token) {
10281033
token.getParameterTokens(),
10291034
token.getExceptionTypes(),
10301035
token.getAnnotations(),
1031-
token.getDefaultValue());
1036+
token.getDefaultValue(),
1037+
token.getReceiverType());
10321038
}
10331039

10341040
/**
@@ -1043,6 +1049,7 @@ public Latent(TypeDescription declaringType, MethodDescription.Token token) {
10431049
* @param exceptionTypes This method's exception types.
10441050
* @param declaredAnnotations The annotations of this method.
10451051
* @param defaultValue The default value of this method or {@code null} if no default annotation value is defined.
1052+
* @param receiverType The receiver type of this method or {@code null} if the receiver type is defined implicitly.
10461053
*/
10471054
public Latent(TypeDescription declaringType,
10481055
String internalName,
@@ -1052,7 +1059,8 @@ public Latent(TypeDescription declaringType,
10521059
List<? extends ParameterDescription.Token> parameterTokens,
10531060
List<? extends TypeDescription.Generic> exceptionTypes,
10541061
List<? extends AnnotationDescription> declaredAnnotations,
1055-
Object defaultValue) {
1062+
Object defaultValue,
1063+
TypeDescription.Generic receiverType) {
10561064
this.declaringType = declaringType;
10571065
this.internalName = internalName;
10581066
this.modifiers = modifiers;
@@ -1062,6 +1070,7 @@ public Latent(TypeDescription declaringType,
10621070
this.exceptionTypes = exceptionTypes;
10631071
this.declaredAnnotations = declaredAnnotations;
10641072
this.defaultValue = defaultValue;
1073+
this.receiverType = receiverType;
10651074
}
10661075

10671076
@Override
@@ -1109,6 +1118,13 @@ public Object getDefaultValue() {
11091118
return defaultValue;
11101119
}
11111120

1121+
@Override
1122+
public TypeDescription.Generic getReceiverType() {
1123+
return receiverType == null
1124+
? super.getReceiverType()
1125+
: receiverType.accept(TypeDescription.Generic.Visitor.Substitutor.ForAttachment.of(this));
1126+
}
1127+
11121128
/**
11131129
* A method description that represents the type initializer.
11141130
*/

byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,7 @@ interface Dispatcher {
25392539
* @param type The type to represent.
25402540
* @return A suitable annotation reader.
25412541
*/
2542-
AnnotationReader resolveSuperClass(Class<?> type);
2542+
AnnotationReader resolveSuperClassType(Class<?> type);
25432543

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

25532553
/**
25542554
* Resolves a loaded field's type's type annotations.
25552555
*
25562556
* @param field The field to represent.
25572557
* @return A suitable annotation reader.
25582558
*/
2559-
AnnotationReader resolve(Field field);
2559+
AnnotationReader resolveFieldType(Field field);
25602560

25612561
/**
25622562
* Resolves a loaded method's return type's type annotations.
@@ -2593,6 +2593,14 @@ interface Dispatcher {
25932593
*/
25942594
Generic resolveReceiverType(AccessibleObject executable);
25952595

2596+
/**
2597+
* Resolves the annotated type as generic type description.
2598+
*
2599+
* @param annotatedType The loaded annotated type.
2600+
* @return A description of the supplied annotated type.
2601+
*/
2602+
Generic resolve(AnnotatedElement annotatedType);
2603+
25962604
/**
25972605
* A dispatcher for {@link AnnotationReader}s on a legacy VM that does not support type annotations.
25982606
*/
@@ -2609,17 +2617,17 @@ public AnnotationReader resolveTypeVariable(TypeVariable<?> typeVariable) {
26092617
}
26102618

26112619
@Override
2612-
public AnnotationReader resolveSuperClass(Class<?> type) {
2620+
public AnnotationReader resolveSuperClassType(Class<?> type) {
26132621
return NoOp.INSTANCE;
26142622
}
26152623

26162624
@Override
2617-
public AnnotationReader resolveInterface(Class<?> type, int index) {
2625+
public AnnotationReader resolveInterfaceType(Class<?> type, int index) {
26182626
return NoOp.INSTANCE;
26192627
}
26202628

26212629
@Override
2622-
public AnnotationReader resolve(Field field) {
2630+
public AnnotationReader resolveFieldType(Field field) {
26232631
return NoOp.INSTANCE;
26242632
}
26252633

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

2654+
@Override
2655+
public Generic resolve(AnnotatedElement annotatedType) {
2656+
throw new IllegalStateException("Loaded annotated type cannot be represented on this VM");
2657+
}
2658+
26462659
@Override
26472660
public String toString() {
26482661
return "TypeDescription.Generic.AnnotationReader.Dispatcher.ForLegacyVm." + name();
@@ -2753,17 +2766,17 @@ public AnnotationReader resolveTypeVariable(TypeVariable<?> typeVariable) {
27532766
}
27542767

27552768
@Override
2756-
public AnnotationReader resolveSuperClass(Class<?> type) {
2769+
public AnnotationReader resolveSuperClassType(Class<?> type) {
27572770
return new AnnotatedSuperClass(type);
27582771
}
27592772

27602773
@Override
2761-
public AnnotationReader resolveInterface(Class<?> type, int index) {
2774+
public AnnotationReader resolveInterfaceType(Class<?> type, int index) {
27622775
return new AnnotatedInterfaceType(type, index);
27632776
}
27642777

27652778
@Override
2766-
public AnnotationReader resolve(Field field) {
2779+
public AnnotationReader resolveFieldType(Field field) {
27672780
return new AnnotatedFieldType(field);
27682781
}
27692782

@@ -2785,14 +2798,24 @@ public AnnotationReader resolveExceptionType(AccessibleObject executable, int in
27852798
@Override
27862799
public Generic resolveReceiverType(AccessibleObject executable) {
27872800
try {
2788-
AnnotatedElement annotatedReceiver = (AnnotatedElement) getAnnotatedReceiverType.invoke(executable);
2789-
return annotatedReceiver == null
2790-
? UNDEFINED
2791-
: Sort.describe((java.lang.reflect.Type) getType.invoke(annotatedReceiver), new Resolved(annotatedReceiver));
2801+
return resolve((AnnotatedElement) getAnnotatedReceiverType.invoke(executable));
2802+
} catch (IllegalAccessException exception) {
2803+
throw new IllegalStateException("Cannot access java.lang.reflect.Executable#getAnnotatedReceiverType", exception);
27922804
} catch (InvocationTargetException exception) {
2793-
throw new IllegalStateException("Could not access receiver type", exception);
2805+
throw new IllegalStateException("Error invoking java.lang.reflect.Executable#getAnnotatedReceiverType", exception.getCause());
2806+
}
2807+
}
2808+
2809+
@Override
2810+
public Generic resolve(AnnotatedElement annotatedType) {
2811+
try {
2812+
return annotatedType == null
2813+
? UNDEFINED
2814+
: Sort.describe((java.lang.reflect.Type) getType.invoke(annotatedType), new Resolved(annotatedType));
27942815
} catch (IllegalAccessException exception) {
2795-
throw new IllegalStateException("Error when accessing receiver type", exception.getCause());
2816+
throw new IllegalStateException("Cannot access java.lang.reflect.AnnotatedType#getType", exception);
2817+
} catch (InvocationTargetException exception) {
2818+
throw new IllegalStateException("Error invoking java.lang.reflect.AnnotatedType#getType", exception.getCause());
27962819
}
27972820
}
27982821

@@ -5857,7 +5880,7 @@ public TypeDescription asErasure() {
58575880

58585881
@Override
58595882
protected AnnotationReader getAnnotationReader() {
5860-
return AnnotationReader.DISPATCHER.resolveSuperClass(type);
5883+
return AnnotationReader.DISPATCHER.resolveSuperClassType(type);
58615884
}
58625885
}
58635886

@@ -5892,7 +5915,7 @@ public TypeDescription asErasure() {
58925915

58935916
@Override
58945917
protected AnnotationReader getAnnotationReader() {
5895-
return AnnotationReader.DISPATCHER.resolve(field);
5918+
return AnnotationReader.DISPATCHER.resolveFieldType(field);
58965919
}
58975920
}
58985921

byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ public TypeDescription asErasure() {
704704

705705
@Override
706706
protected AnnotationReader getAnnotationReader() {
707-
return AnnotationReader.DISPATCHER.resolveInterface(type, index);
707+
return AnnotationReader.DISPATCHER.resolveInterfaceType(type, index);
708708
}
709709
}
710710
}

0 commit comments

Comments
 (0)