/
MethodDescription.java
1508 lines (1314 loc) · 57.6 KB
/
MethodDescription.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.bytebuddy.description.method;
import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.ModifierReviewable;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.enumeration.EnumerationDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeList;
import net.bytebuddy.description.type.generic.TypeVariableSource;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.utility.JavaInstance;
import net.bytebuddy.utility.JavaType;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericSignatureFormatError;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.none;
/**
* Implementations of this interface describe a Java method, i.e. a method or a constructor. Implementations of this
* interface must provide meaningful {@code equal(Object)} and {@code hashCode()} implementations.
*/
public interface MethodDescription extends TypeVariableSource, NamedElement.WithGenericName {
/**
* The internal name of a Java constructor.
*/
String CONSTRUCTOR_INTERNAL_NAME = "<init>";
/**
* The internal name of a Java static initializer.
*/
String TYPE_INITIALIZER_INTERNAL_NAME = "<clinit>";
/**
* The type initializer of any representation of a type initializer.
*/
int TYPE_INITIALIZER_MODIFIER = Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC;
/**
* Represents a non-defined default value of an annotation property.
*/
Object NO_DEFAULT_VALUE = null;
/**
* Returns the return type of the described method.
*
* @return The return type of the described method.
*/
GenericTypeDescription getReturnType();
/**
* Returns a list of this method's parameters.
*
* @return A list of this method's parameters.
*/
ParameterList<?> getParameters();
/**
* Returns the exception types of the described method.
*
* @return The exception types of the described method.
*/
GenericTypeList getExceptionTypes();
/**
* Returns this method modifier but adjusts its state of being abstract.
*
* @param nonAbstract {@code true} if the method should be treated as non-abstract.
* @return The adjusted modifiers.
*/
int getAdjustedModifiers(boolean nonAbstract);
/**
* Checks if this method description represents a constructor.
*
* @return {@code true} if this method description represents a constructor.
*/
boolean isConstructor();
/**
* Checks if this method description represents a method, i.e. not a constructor or a type initializer.
*
* @return {@code true} if this method description represents a method.
*/
boolean isMethod();
/**
* Checks if this method is a type initializer.
*
* @return {@code true} if this method description represents a type initializer.
*/
boolean isTypeInitializer();
/**
* Verifies if a method description represents a given loaded method.
*
* @param method The method to be checked.
* @return {@code true} if this method description represents the given loaded method.
*/
boolean represents(Method method);
/**
* Verifies if a method description represents a given loaded constructor.
*
* @param constructor The constructor to be checked.
* @return {@code true} if this method description represents the given loaded constructor.
*/
boolean represents(Constructor<?> constructor);
/**
* Verifies if this method description represents an overridable method.
*
* @return {@code true} if this method description represents an overridable method.
*/
boolean isOverridable();
/**
* Returns the size of the local variable array that is required for this method, i.e. the size of all parameters
* if they were loaded on the stack including a reference to {@code this} if this method represented a non-static
* method.
*
* @return The size of this method on the operand stack.
*/
int getStackSize();
/**
* Checks if this method represents a Java 8+ default method.
*
* @return {@code true} if this method is a default method.
*/
boolean isDefaultMethod();
/**
* Checks if this method can be called using the {@code INVOKESPECIAL} for a given type.
*
* @param typeDescription The type o
* @return {@code true} if this method can be called using the {@code INVOKESPECIAL} instruction
* using the given type.
*/
boolean isSpecializableFor(TypeDescription typeDescription);
/**
* Returns the default value of this method or {@code null} if no such value exists. The returned values might be
* of a different type than usual:
* <ul>
* <li>{@link java.lang.Class} values are represented as
* {@link TypeDescription}s.</li>
* <li>{@link java.lang.annotation.Annotation} values are represented as
* {@link AnnotationDescription}s</li>
* <li>{@link java.lang.Enum} values are represented as
* {@link net.bytebuddy.description.enumeration.EnumerationDescription}s.</li>
* <li>Arrays of the latter types are represented as arrays of the named wrapper types.</li>
* </ul>
*
* @return The default value of this method or {@code null}.
*/
Object getDefaultValue();
/**
* Returns the default value but casts it to the given type. If the type differs from the value, a
* {@link java.lang.ClassCastException} is thrown.
*
* @param type The type to cast the default value to.
* @param <T> The type to cast the default value to.
* @return The casted default value.
*/
<T> T getDefaultValue(Class<T> type);
/**
* Asserts if this method is invokable on an instance of the given type, i.e. the method is an instance method or
* a constructor and the method is visible to the type and can be invoked on the given instance.
*
* @param typeDescription The type to check.
* @return {@code true} if this method is invokable on an instance of the given type.
*/
boolean isInvokableOn(TypeDescription typeDescription);
/**
* Checks if the method is a bootstrap method.
*
* @return {@code true} if the method is a bootstrap method.
*/
boolean isBootstrap();
/**
* Checks if the method is a bootstrap method that accepts the given arguments.
*
* @param arguments The arguments that the bootstrap method is expected to accept where primitive values
* are to be represented as their wrapper types, loaded types by {@link TypeDescription},
* method handles by {@link net.bytebuddy.utility.JavaInstance.MethodHandle} instances and
* method types by {@link net.bytebuddy.utility.JavaInstance.MethodType} instances.
* @return {@code true} if the method is a bootstrap method that accepts the given arguments.
*/
boolean isBootstrap(List<?> arguments);
/**
* Checks if this method is capable of defining a default annotation value.
*
* @return {@code true} if it is possible to define a default annotation value for this method.
*/
boolean isDefaultValue();
/**
* Checks if the given value can describe a default annotation value for this method.
*
* @param value The value that describes the default annotation value for this method.
* @return {@code true} if the given value can describe a default annotation value for this method.
*/
boolean isDefaultValue(Object value);
/**
* Transforms this method description into a token. For the resulting token, all type variables within the scope
* of this method's types are detached from their declaration context.
*
* @return A token representing this method.
*/
Token asToken();
/**
* Transforms this method description into a token. For the resulting token, all type variables within the scope
* of this method's types are detached from their declaration context.
*
* @param targetTypeMatcher A matcher that is applied to the method's types for replacing any matched
* type by {@link net.bytebuddy.dynamic.TargetType}.
* @return A token representing this method.
*/
Token asToken(ElementMatcher<? super TypeDescription> targetTypeMatcher);
InDeclaredForm asDeclared();
interface InDeclaredForm extends MethodDescription {
@Override
TypeDescription getDeclaringType();
@Override
ParameterList<ParameterDescription.InDeclaredForm> getParameters();
abstract class AbstractBase extends MethodDescription.AbstractBase implements InDeclaredForm {
@Override
public InDeclaredForm asDeclared() {
return this;
}
}
}
/**
* An abstract base implementation of a method description.
*/
abstract class AbstractBase extends ModifierReviewable.AbstractBase implements MethodDescription {
/**
* A merger of all method modifiers that are visible in the Java source code.
*/
private static final int SOURCE_MODIFIERS = Modifier.PUBLIC
| Modifier.PROTECTED
| Modifier.PRIVATE
| Modifier.ABSTRACT
| Modifier.STATIC
| Modifier.FINAL
| Modifier.SYNCHRONIZED
| Modifier.NATIVE;
@Override
public int getStackSize() {
return getParameters().asTypeList().getStackSize() + (isStatic() ? 0 : 1);
}
@Override
public boolean isMethod() {
return !isConstructor() && !isTypeInitializer();
}
@Override
public boolean isConstructor() {
return CONSTRUCTOR_INTERNAL_NAME.equals(getInternalName());
}
@Override
public boolean isTypeInitializer() {
return TYPE_INITIALIZER_INTERNAL_NAME.equals(getInternalName());
}
@Override
public boolean represents(Method method) {
return equals(new ForLoadedMethod(method));
}
@Override
public boolean represents(Constructor<?> constructor) {
return equals(new ForLoadedConstructor(constructor));
}
@Override
public String getName() {
return isMethod()
? getInternalName()
: getDeclaringType().asRawType().getName();
}
@Override
public String getSourceCodeName() {
return isMethod()
? getName()
: EMPTY_NAME;
}
@Override
public String getDescriptor() {
StringBuilder descriptor = new StringBuilder("(");
for (TypeDescription parameterType : getParameters().asTypeList().asRawTypes()) {
descriptor.append(parameterType.getDescriptor());
}
return descriptor.append(")").append(getReturnType().asRawType().getDescriptor()).toString();
}
@Override
public String getGenericSignature() {
try {
SignatureWriter signatureWriter = new SignatureWriter();
boolean generic = false;
for (GenericTypeDescription typeVariable : getTypeVariables()) {
signatureWriter.visitFormalTypeParameter(typeVariable.getSymbol());
boolean classBound = true;
for (GenericTypeDescription upperBound : typeVariable.getUpperBounds()) {
upperBound.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(classBound
? signatureWriter.visitClassBound()
: signatureWriter.visitInterfaceBound()));
classBound = false;
}
generic = true;
}
for (GenericTypeDescription parameterType : getParameters().asTypeList()) {
parameterType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(signatureWriter.visitParameterType()));
generic = generic || !parameterType.getSort().isNonGeneric();
}
GenericTypeDescription returnType = getReturnType();
returnType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(signatureWriter.visitReturnType()));
generic = generic || !returnType.getSort().isNonGeneric();
for (GenericTypeDescription exceptionType : getExceptionTypes()) {
exceptionType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(signatureWriter.visitExceptionType()));
generic = generic || !exceptionType.getSort().isNonGeneric();
}
return generic
? signatureWriter.toString()
: NON_GENERIC_SIGNATURE;
} catch (GenericSignatureFormatError ignored) {
return NON_GENERIC_SIGNATURE;
}
}
@Override
public int getAdjustedModifiers(boolean nonAbstract) {
return nonAbstract
? getModifiers() & ~(Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)
: getModifiers() & ~Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT;
}
@Override
public boolean isVisibleTo(TypeDescription typeDescription) {
if (!getDeclaringType().asRawType().isVisibleTo(typeDescription) || !getReturnType().asRawType().isVisibleTo(typeDescription)) {
return false;
}
for (TypeDescription parameterType : getParameters().asTypeList().asRawTypes()) {
if (!parameterType.isVisibleTo(typeDescription)) {
return false;
}
}
return isPublic()
|| typeDescription.equals(getDeclaringType())
|| (isProtected() && getDeclaringType().asRawType().isAssignableFrom(typeDescription))
|| (!isPrivate() && typeDescription.isSamePackage(getDeclaringType().asRawType()));
}
@Override
public boolean isOverridable() {
return !(isConstructor() || isFinal() || isPrivate() || isStatic());
}
@Override
public boolean isDefaultMethod() {
return !isAbstract() && !isBridge() && getDeclaringType().asRawType().isInterface();
}
@Override
public boolean isSpecializableFor(TypeDescription targetType) {
if (isStatic()) { // Static private methods are never specializable, check static property first
return false;
} else if (isPrivate() || isConstructor()) {
return getDeclaringType().equals(targetType);
} else {
return !isAbstract() && getDeclaringType().asRawType().isAssignableFrom(targetType);
}
}
@Override
public <T> T getDefaultValue(Class<T> type) {
return type.cast(getDefaultValue());
}
@Override
public boolean isInvokableOn(TypeDescription typeDescription) {
return !isStatic()
&& !isTypeInitializer()
&& isVisibleTo(typeDescription)
&& getDeclaringType().asRawType().isAssignableFrom(typeDescription);
}
@Override
public boolean isBootstrap() {
TypeDescription returnType = getReturnType().asRawType();
if ((isMethod() && (!isStatic()
|| !(JavaType.CALL_SITE.getTypeStub().isAssignableFrom(returnType) || JavaType.CALL_SITE.getTypeStub().isAssignableTo(returnType))))
|| (isConstructor() && !JavaType.CALL_SITE.getTypeStub().isAssignableFrom(getDeclaringType().asRawType()))) {
return false;
}
TypeList parameterTypes = getParameters().asTypeList().asRawTypes();
switch (parameterTypes.size()) {
case 0:
return false;
case 1:
return parameterTypes.getOnly().represents(Object[].class);
case 2:
return JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().isAssignableTo(parameterTypes.get(0))
&& parameterTypes.get(1).represents(Object[].class);
case 3:
return JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().isAssignableTo(parameterTypes.get(0))
&& (parameterTypes.get(1).represents(Object.class) || parameterTypes.get(1).represents(String.class))
&& (parameterTypes.get(2).represents(Object[].class) || JavaType.METHOD_TYPE.getTypeStub().isAssignableTo(parameterTypes.get(2)));
default:
if (!(JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().isAssignableTo(parameterTypes.get(0))
&& (parameterTypes.get(1).represents(Object.class) || parameterTypes.get(1).represents(String.class))
&& (JavaType.METHOD_TYPE.getTypeStub().isAssignableTo(parameterTypes.get(2))))) {
return false;
}
int parameterIndex = 4;
for (TypeDescription parameterType : parameterTypes.subList(3, parameterTypes.size())) {
if (!parameterType.represents(Object.class) && !parameterType.isConstantPool()) {
return parameterType.represents(Object[].class) && parameterIndex == parameterTypes.size();
}
parameterIndex++;
}
return true;
}
}
@Override
public boolean isBootstrap(List<?> arguments) {
if (!isBootstrap()) {
return false;
}
for (Object argument : arguments) {
Class<?> argumentType = argument.getClass();
if (!(argumentType == String.class
|| argumentType == Integer.class
|| argumentType == Long.class
|| argumentType == Float.class
|| argumentType == Double.class
|| TypeDescription.class.isAssignableFrom(argumentType)
|| JavaInstance.MethodHandle.class.isAssignableFrom(argumentType)
|| JavaInstance.MethodType.class.isAssignableFrom(argumentType))) {
throw new IllegalArgumentException("Not a bootstrap argument: " + argument);
}
}
TypeList parameterTypes = getParameters().asTypeList().asRawTypes();
// The following assumes that the bootstrap method is a valid one, as checked above.
if (parameterTypes.size() < 4) {
return arguments.isEmpty() || parameterTypes.get(parameterTypes.size() - 1).represents(Object[].class);
} else {
int index = 4;
Iterator<?> argumentIterator = arguments.iterator();
for (TypeDescription parameterType : parameterTypes.subList(3, parameterTypes.size())) {
boolean finalParameterCheck = !argumentIterator.hasNext();
if (!finalParameterCheck) {
Class<?> argumentType = argumentIterator.next().getClass();
finalParameterCheck = !(parameterType.represents(String.class) && argumentType == String.class)
&& !(parameterType.represents(int.class) && argumentType == Integer.class)
&& !(parameterType.represents(long.class) && argumentType == Long.class)
&& !(parameterType.represents(float.class) && argumentType == Float.class)
&& !(parameterType.represents(double.class) && argumentType == Double.class)
&& !(parameterType.represents(Class.class) && TypeDescription.class.isAssignableFrom(argumentType))
&& !(parameterType.isAssignableFrom(JavaType.METHOD_HANDLE.getTypeStub()) && JavaInstance.MethodHandle.class.isAssignableFrom(argumentType))
&& !(parameterType.equals(JavaType.METHOD_TYPE.getTypeStub()) && JavaInstance.MethodType.class.isAssignableFrom(argumentType));
}
if (finalParameterCheck) {
return index == parameterTypes.size() && parameterType.represents(Object[].class);
}
index++;
}
return true;
}
}
@Override
public boolean isDefaultValue() {
return !isConstructor()
&& !isStatic()
&& getReturnType().asRawType().isAnnotationReturnType()
&& getParameters().isEmpty();
}
@Override
public boolean isDefaultValue(Object value) {
if (!isDefaultValue()) {
return false;
}
TypeDescription returnType = getReturnType().asRawType();
return (returnType.represents(boolean.class) && value instanceof Boolean)
|| (returnType.represents(byte.class) && value instanceof Byte)
|| (returnType.represents(char.class) && value instanceof Character)
|| (returnType.represents(short.class) && value instanceof Short)
|| (returnType.represents(int.class) && value instanceof Integer)
|| (returnType.represents(long.class) && value instanceof Long)
|| (returnType.represents(float.class) && value instanceof Float)
|| (returnType.represents(long.class) && value instanceof Long)
|| (returnType.represents(String.class) && value instanceof String)
|| (returnType.isAssignableTo(Enum.class) && value instanceof EnumerationDescription)
|| (returnType.isAssignableTo(Annotation.class) && value instanceof AnnotationDescription)
|| (returnType.represents(Class.class) && value instanceof TypeDescription);
}
@Override
public TypeVariableSource getEnclosingSource() {
return getDeclaringType().asRawType();
}
@Override
public GenericTypeDescription findVariable(String symbol) {
GenericTypeList typeVariables = getTypeVariables().filter(named(symbol));
return typeVariables.isEmpty()
? getEnclosingSource().findVariable(symbol)
: typeVariables.getOnly();
}
@Override
public <T> T accept(TypeVariableSource.Visitor<T> visitor) {
return visitor.onMethod(this);
}
@Override
public Token asToken() {
return asToken(none());
}
@Override
public Token asToken(ElementMatcher<? super TypeDescription> targetTypeMatcher) {
GenericTypeDescription.Visitor<GenericTypeDescription> visitor = new GenericTypeDescription.Visitor.Substitutor.ForDetachment(targetTypeMatcher);
return new Token(getInternalName(),
getModifiers(),
getTypeVariables().accept(visitor),
getReturnType().accept(visitor),
getParameters().asTokenList(targetTypeMatcher),
getExceptionTypes().accept(visitor),
getDeclaredAnnotations(),
getDefaultValue());
}
@Override
public boolean equals(Object other) {
return other == this || other instanceof MethodDescription
&& getInternalName().equals(((MethodDescription) other).getInternalName())
&& getDeclaringType().equals(((MethodDescription) other).getDeclaringType())
&& getReturnType().asRawType().equals(((MethodDescription) other).getReturnType().asRawType())
&& getParameters().asTypeList().asRawTypes().equals(((MethodDescription) other).getParameters().asTypeList().asRawTypes());
}
@Override
public int hashCode() {
int hashCode = getDeclaringType().hashCode();
hashCode = 31 * hashCode + getInternalName().hashCode();
hashCode = 31 * hashCode + getReturnType().asRawType().hashCode();
return 31 * hashCode + getParameters().asTypeList().asRawTypes().hashCode();
}
@Override
public String toGenericString() {
StringBuilder stringBuilder = new StringBuilder();
int modifiers = getModifiers() & SOURCE_MODIFIERS;
if (modifiers != EMPTY_MASK) {
stringBuilder.append(Modifier.toString(modifiers)).append(" ");
}
if (isMethod()) {
stringBuilder.append(getReturnType().getSourceCodeName()).append(" ");
stringBuilder.append(getDeclaringType().asRawType().getSourceCodeName()).append(".");
}
stringBuilder.append(getName()).append("(");
boolean first = true;
for (GenericTypeDescription typeDescription : getParameters().asTypeList()) {
if (!first) {
stringBuilder.append(",");
} else {
first = false;
}
stringBuilder.append(typeDescription.getSourceCodeName());
}
stringBuilder.append(")");
GenericTypeList exceptionTypes = getExceptionTypes();
if (exceptionTypes.size() > 0) {
stringBuilder.append(" throws ");
first = true;
for (GenericTypeDescription typeDescription : exceptionTypes) {
if (!first) {
stringBuilder.append(",");
} else {
first = false;
}
stringBuilder.append(typeDescription.getSourceCodeName());
}
}
return stringBuilder.toString();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
int modifiers = getModifiers() & SOURCE_MODIFIERS;
if (modifiers != EMPTY_MASK) {
stringBuilder.append(Modifier.toString(modifiers)).append(" ");
}
if (isMethod()) {
stringBuilder.append(getReturnType().asRawType().getSourceCodeName()).append(" ");
stringBuilder.append(getDeclaringType().asRawType().getSourceCodeName()).append(".");
}
stringBuilder.append(getName()).append("(");
boolean first = true;
for (TypeDescription typeDescription : getParameters().asTypeList().asRawTypes()) {
if (!first) {
stringBuilder.append(",");
} else {
first = false;
}
stringBuilder.append(typeDescription.getSourceCodeName());
}
stringBuilder.append(")");
TypeList exceptionTypes = getExceptionTypes().asRawTypes();
if (exceptionTypes.size() > 0) {
stringBuilder.append(" throws ");
first = true;
for (TypeDescription typeDescription : exceptionTypes) {
if (!first) {
stringBuilder.append(",");
} else {
first = false;
}
stringBuilder.append(typeDescription.getSourceCodeName());
}
}
return stringBuilder.toString();
}
}
/**
* An implementation of a method description for a loaded constructor.
*/
class ForLoadedConstructor extends InDeclaredForm.AbstractBase {
/**
* The loaded constructor that is represented by this instance.
*/
private final Constructor<?> constructor;
/**
* Creates a new immutable method description for a loaded constructor.
*
* @param constructor The loaded constructor to be represented by this method description.
*/
public ForLoadedConstructor(Constructor<?> constructor) {
this.constructor = constructor;
}
@Override
public TypeDescription getDeclaringType() {
return new TypeDescription.ForLoadedType(constructor.getDeclaringClass());
}
@Override
public GenericTypeDescription getReturnType() {
return TypeDescription.VOID;
}
@Override
public ParameterList<ParameterDescription.InDeclaredForm> getParameters() {
return ParameterList.ForLoadedExecutable.of(constructor);
}
@Override
public GenericTypeList getExceptionTypes() {
return new GenericTypeList.LazyProjection.OfConstructorExceptionTypes(constructor);
}
@Override
public boolean isConstructor() {
return true;
}
@Override
public boolean isTypeInitializer() {
return false;
}
@Override
public boolean represents(Method method) {
return false;
}
@Override
public boolean represents(Constructor<?> constructor) {
return this.constructor.equals(constructor) || equals(new ForLoadedConstructor(constructor));
}
@Override
public String getName() {
return constructor.getName();
}
@Override
public int getModifiers() {
return constructor.getModifiers();
}
@Override
public boolean isSynthetic() {
return constructor.isSynthetic();
}
@Override
public String getInternalName() {
return CONSTRUCTOR_INTERNAL_NAME;
}
@Override
public String getDescriptor() {
return Type.getConstructorDescriptor(constructor);
}
@Override
public Object getDefaultValue() {
return null;
}
@Override
public AnnotationList getDeclaredAnnotations() {
return new AnnotationList.ForLoadedAnnotation(constructor.getDeclaredAnnotations());
}
@Override
public GenericTypeList getTypeVariables() {
return new GenericTypeList.ForLoadedType(constructor.getTypeParameters());
}
}
/**
* An implementation of a method description for a loaded method.
*/
class ForLoadedMethod extends InDeclaredForm.AbstractBase {
/**
* The loaded method that is represented by this instance.
*/
private final Method method;
/**
* Creates a new immutable method description for a loaded method.
*
* @param method The loaded method to be represented by this method description.
*/
public ForLoadedMethod(Method method) {
this.method = method;
}
@Override
public TypeDescription getDeclaringType() {
return new TypeDescription.ForLoadedType(method.getDeclaringClass());
}
@Override
public GenericTypeDescription getReturnType() {
return new GenericTypeDescription.LazyProjection.OfLoadedReturnType(method);
}
@Override
public ParameterList<ParameterDescription.InDeclaredForm> getParameters() {
return ParameterList.ForLoadedExecutable.of(method);
}
@Override
public GenericTypeList getExceptionTypes() {
return new GenericTypeList.LazyProjection.OfMethodExceptionTypes(method);
}
@Override
public boolean isConstructor() {
return false;
}
@Override
public boolean isTypeInitializer() {
return false;
}
@Override
public boolean isBridge() {
return method.isBridge();
}
@Override
public boolean represents(Method method) {
return this.method.equals(method) || equals(new ForLoadedMethod(method));
}
@Override
public boolean represents(Constructor<?> constructor) {
return false;
}
@Override
public String getName() {
return method.getName();
}
@Override
public int getModifiers() {
return method.getModifiers();
}
@Override
public boolean isSynthetic() {
return method.isSynthetic();
}
@Override
public String getInternalName() {
return method.getName();
}
@Override
public String getDescriptor() {
return Type.getMethodDescriptor(method);
}
/**
* Returns the loaded method that is represented by this method description.
*
* @return The loaded method that is represented by this method description.
*/
public Method getLoadedMethod() {
return method;
}
@Override
public AnnotationList getDeclaredAnnotations() {
return new AnnotationList.ForLoadedAnnotation(method.getDeclaredAnnotations());
}
@Override
public Object getDefaultValue() {
Object value = method.getDefaultValue();
return value == null
? null
: AnnotationDescription.ForLoadedAnnotation.describe(value, new TypeDescription.ForLoadedType(method.getReturnType()));
}
@Override
public GenericTypeList getTypeVariables() {
return new GenericTypeList.ForLoadedType(method.getTypeParameters());
}
}
/**
* A latent method description describes a method that is not attached to a declaring
* {@link TypeDescription}.
*/
class Latent extends InDeclaredForm.AbstractBase {
/**
* The type that is declaring this method.
*/
private final TypeDescription declaringType;
/**
* The internal name of this method.
*/
private final String internalName;
/**
* The modifiers of this method.
*/
private final int modifiers;
/**
* The type variables of the described method.
*/
private final List<? extends GenericTypeDescription> typeVariables;
/**
* The return type of this method.
*/
private final GenericTypeDescription returnType;
/**
* The parameter tokens describing this method.
*/
private final List<? extends ParameterDescription.Token> parameterTokens;
/**
* This method's exception types.
*/
private final List<? extends GenericTypeDescription> exceptionTypes;
/**
* The annotations of this method.
*/
private final List<? extends AnnotationDescription> declaredAnnotations;
/**
* The default value of this method or {@code null} if no default annotation value is defined.
*/
private final Object defaultValue;
/**
* Creates a new latent method description. All provided types are attached to this instance before they are returned.
*
* @param declaringType The declaring type of the method.
* @param token A token representing the method's shape.
*/
public Latent(TypeDescription declaringType, Token token) {
this(declaringType,
token.getInternalName(),
token.getModifiers(),
token.getTypeVariables(),
token.getReturnType(),
token.getParameterTokens(),
token.getExceptionTypes(),
token.getAnnotations(),
token.getDefaultValue());
}
/**
* Creates a new latent method description. All provided types are attached to this instance before they are returned.
*
* @param declaringType The type that is declaring this method.
* @param internalName The internal name of this method.
* @param modifiers The modifiers of this method.
* @param typeVariables The type variables of the described method.
* @param returnType The return type of this method.
* @param parameterTokens The parameter tokens describing this method.
* @param exceptionTypes This method's exception types.
* @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.
*/
public Latent(TypeDescription declaringType,
String internalName,
int modifiers,
List<? extends GenericTypeDescription> typeVariables,
GenericTypeDescription returnType,
List<? extends ParameterDescription.Token> parameterTokens,
List<? extends GenericTypeDescription> exceptionTypes,
List<? extends AnnotationDescription> declaredAnnotations,
Object defaultValue) {
this.declaringType = declaringType;
this.internalName = internalName;
this.modifiers = modifiers;
this.typeVariables = typeVariables;
this.returnType = returnType;
this.parameterTokens = parameterTokens;
this.exceptionTypes = exceptionTypes;
this.declaredAnnotations = declaredAnnotations;
this.defaultValue = defaultValue;
}
@Override
public GenericTypeList getTypeVariables() {
return GenericTypeList.ForDetachedTypes.OfTypeVariable.attach(this, typeVariables);
}
@Override
public GenericTypeDescription getReturnType() {
return returnType.accept(GenericTypeDescription.Visitor.Substitutor.ForAttachment.of(this));
}
@Override
public ParameterList<ParameterDescription.InDeclaredForm> getParameters() {
return new ParameterList.ForTokens(this, parameterTokens);