-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathExecutable.java
789 lines (673 loc) · 35.3 KB
/
Executable.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
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.reflect;
import jdk.internal.misc.SharedSecrets;
import sun.reflect.annotation.AnnotationParser;
import sun.reflect.annotation.AnnotationSupport;
import sun.reflect.annotation.TypeAnnotation;
import sun.reflect.annotation.TypeAnnotationParser;
import sun.reflect.generics.repository.ConstructorRepository;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
/**
* A shared superclass for the common functionality of {@link Method}
* and {@link Constructor}.
*
* @since 1.8
*/
// 可执行元素,比如:Constructor和Method
public abstract class Executable extends AccessibleObject implements Member, GenericDeclaration {
private transient volatile boolean hasRealParameterData;
private transient volatile Parameter[] parameters;
private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
/*▼ 构造方法 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Only grant package-visibility to the constructor.
*/
Executable() {
}
/*▲ 构造方法 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 1. 修饰符 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the Java language {@linkplain Modifier modifiers} for
* the executable represented by this object.
*/
// 获取元素修饰符
public abstract int getModifiers();
/*▲ 1. 修饰符 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 2. 引入的TypeVariable ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns an array of {@code TypeVariable} objects that represent the
* type variables declared by the generic declaration represented by this
* {@code GenericDeclaration} object, in declaration order. Returns an
* array of length 0 if the underlying generic declaration declares no type
* variables.
*
* @return an array of {@code TypeVariable} objects that represent
* the type variables declared by this generic declaration
*
* @throws GenericSignatureFormatError if the generic
* signature of this generic declaration does not conform to
* the format specified in
* <cite>The Java™ Virtual Machine Specification</cite>
*/
// 构造器/方法引入的TypeVariable
public abstract TypeVariable<?>[] getTypeParameters();
/*▲ 2. 引入的TypeVariable ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 3. 返回值 ████████████████████████████████████████████████████████████████████████████████┓ */
/*▲ 3. 返回值 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 4. 名称 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the name of the executable represented by this object.
*/
// 获取元素名称
public abstract String getName();
/*▲ 4. 名称 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 字符串化 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns a string describing this {@code Executable}, including
* any type parameters.
*
* @return a string describing this {@code Executable}, including
* any type parameters
*/
// 获取元素的描述,带着泛型信息
public abstract String toGenericString();
/*▲ 字符串化 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 5. 形参 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the number of formal parameters (whether explicitly
* declared or implicitly declared or neither) for the executable
* represented by this object.
*
* @return The number of formal parameters for the executable this
* object represents
*/
// 获取形参数量
public int getParameterCount() {
throw new AbstractMethodError();
}
/**
* Returns {@code true} if this executable was declared to take a
* variable number of arguments; returns {@code false} otherwise.
*
* @return {@code true} if an only if this executable was declared
* to take a variable number of arguments.
*/
// 是否为可变数量形参
public boolean isVarArgs() {
return (getModifiers() & Modifier.VARARGS) != 0;
}
/**
* Returns an array of {@code Class} objects that represent the formal
* parameter types, in declaration order, of the executable
* represented by this object. Returns an array of length
* 0 if the underlying executable takes no parameters.
*
* @return the parameter types for the executable this object
* represents
*/
// 获取形参类型[类型擦除]
public abstract Class<?>[] getParameterTypes();
/**
* Returns an array of {@code Type} objects that represent the formal
* parameter types, in declaration order, of the executable represented by
* this object. Returns an array of length 0 if the
* underlying executable takes no parameters.
*
* <p>If a formal parameter type is a parameterized type,
* the {@code Type} object returned for it must accurately reflect
* the actual type parameters used in the source code.
*
* <p>If a formal parameter type is a type variable or a parameterized
* type, it is created. Otherwise, it is resolved.
*
* @return an array of {@code Type}s that represent the formal
* parameter types of the underlying executable, in declaration order
*
* @throws GenericSignatureFormatError if the generic method signature does not conform to the format
* specified in
* <cite>The Java™ Virtual Machine Specification</cite>
* @throws TypeNotPresentException if any of the parameter
* types of the underlying executable refers to a non-existent type
* declaration
* @throws MalformedParameterizedTypeException if any of
* the underlying executable's parameter types refer to a parameterized
* type that cannot be instantiated for any reason
*/
// 获取形参类型[支持泛型语义]
public Type[] getGenericParameterTypes() {
if(hasGenericInformation())
return getGenericInfo().getParameterTypes();
else
return getParameterTypes();
}
/**
* Returns an array of {@code Parameter} objects that represent
* all the parameters to the underlying executable represented by
* this object. Returns an array of length 0 if the executable
* has no parameters.
*
* <p>The parameters of the underlying executable do not necessarily
* have unique names, or names that are legal identifiers in the
* Java programming language (JLS 3.8).
*
* @return an array of {@code Parameter} objects representing all
* the parameters to the executable this object represents.
*
* @throws MalformedParametersException if the class file contains
* a MethodParameters attribute that is improperly formatted.
*/
// 获取形参对象
public Parameter[] getParameters() {
// TODO: This may eventually need to be guarded by security
// mechanisms similar to those in Field, Method, etc.
//
// Need to copy the cached array to prevent users from messing
// with it. Since parameters are immutable, we can
// shallow-copy.
return privateGetParameters().clone();
}
/*▲ 5. 形参 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 6. 抛出的异常 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns an array of {@code Class} objects that represent the
* types of exceptions declared to be thrown by the underlying
* executable represented by this object. Returns an array of
* length 0 if the executable declares no exceptions in its {@code
* throws} clause.
*
* @return the exception types declared as being thrown by the
* executable this object represents
*/
// 获取该构造器抛出的异常类型[类型擦除]
public abstract Class<?>[] getExceptionTypes();
/**
* Returns an array of {@code Type} objects that represent the
* exceptions declared to be thrown by this executable object.
* Returns an array of length 0 if the underlying executable declares
* no exceptions in its {@code throws} clause.
*
* <p>If an exception type is a type variable or a parameterized
* type, it is created. Otherwise, it is resolved.
*
* @return an array of Types that represent the exception types
* thrown by the underlying executable
*
* @throws GenericSignatureFormatError if the generic method signature does not conform to the format
* specified in
* <cite>The Java™ Virtual Machine Specification</cite>
* @throws TypeNotPresentException if the underlying executable's
* {@code throws} clause refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if
* the underlying executable's {@code throws} clause refers to a
* parameterized type that cannot be instantiated for any reason
*/
// 获取该方法抛出的异常类型[支持泛型语义]
public Type[] getGenericExceptionTypes() {
Type[] result;
if(hasGenericInformation() && ((result = getGenericInfo().getExceptionTypes()).length>0))
return result;
else
return getExceptionTypes();
}
/*▲ 6. 抛出的异常 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 注解 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* {@inheritDoc}
*
* @throws NullPointerException {@inheritDoc}
*/
// 1-2 返回该元素上指定类型的注解
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return annotationClass.cast(declaredAnnotations().get(annotationClass));
}
/**
* {@inheritDoc}
*
* @throws NullPointerException {@inheritDoc}
*/
// 1-3 返回该元素上指定类型的注解[支持获取@Repeatable类型的注解]
@Override
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
}
/**
* {@inheritDoc}
*/
// 2-1 返回该元素上所有类型的注解
public Annotation[] getDeclaredAnnotations() {
return AnnotationParser.toArray(declaredAnnotations());
}
/**
* Returns an array of arrays of {@code Annotation}s that
* represent the annotations on the formal parameters, in
* declaration order, of the {@code Executable} represented by
* this object. Synthetic and mandated parameters (see
* explanation below), such as the outer "this" parameter to an
* inner class constructor will be represented in the returned
* array. If the executable has no parameters (meaning no formal,
* no synthetic, and no mandated parameters), a zero-length array
* will be returned. If the {@code Executable} has one or more
* parameters, a nested array of length zero is returned for each
* parameter with no annotations. The annotation objects contained
* in the returned arrays are serializable. The caller of this
* method is free to modify the returned arrays; it will have no
* effect on the arrays returned to other callers.
*
* A compiler may add extra parameters that are implicitly
* declared in source ("mandated"), as well as parameters that
* are neither implicitly nor explicitly declared in source
* ("synthetic") to the parameter list for a method. See {@link
* java.lang.reflect.Parameter} for more information.
*
* @return an array of arrays that represent the annotations on
* the formal and implicit parameters, in declaration order, of
* the executable represented by this object
*
* @see java.lang.reflect.Parameter
* @see java.lang.reflect.Parameter#getAnnotations
*/
// 获取形参上的注解
public abstract Annotation[][] getParameterAnnotations();
/**
* Returns an array of {@code AnnotatedType} objects that represent the use
* of types to specify formal parameter types of the method/constructor
* represented by this Executable. The order of the objects in the array
* corresponds to the order of the formal parameter types in the
* declaration of the method/constructor.
*
* Returns an array of length 0 if the method/constructor declares no
* parameters.
*
* @return an array of objects representing the types of the
* formal parameters of the method or constructor represented by this
* {@code Executable}
*/
// 获取形参上的【被注解类型】
public AnnotatedType[] getAnnotatedParameterTypes() {
return TypeAnnotationParser.buildAnnotatedTypes(getTypeAnnotationBytes0(), SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()), this, getDeclaringClass(), getAllGenericParameterTypes(), TypeAnnotation.TypeAnnotationTarget.METHOD_FORMAL_PARAMETER);
}
/**
* Returns an {@code AnnotatedType} object that represents the use of a type to
* specify the return type of the method/constructor represented by this
* Executable.
*
* If this {@code Executable} object represents a constructor, the {@code
* AnnotatedType} object represents the type of the constructed object.
*
* If this {@code Executable} object represents a method, the {@code
* AnnotatedType} object represents the use of a type to specify the return
* type of the method.
*
* @return an object representing the return type of the method
* or constructor represented by this {@code Executable}
*/
// 获取返回类型上的【被注解类型】
public abstract AnnotatedType getAnnotatedReturnType();
/**
* Returns an {@code AnnotatedType} object that represents the use of a
* type to specify the receiver type of the method/constructor represented
* by this {@code Executable} object.
*
* The receiver type of a method/constructor is available only if the
* method/constructor has a receiver parameter (JLS 8.4.1). If this {@code
* Executable} object <em>represents an instance method or represents a
* constructor of an inner member class</em>, and the
* method/constructor <em>either</em> has no receiver parameter or has a
* receiver parameter with no annotations on its type, then the return
* value is an {@code AnnotatedType} object representing an element with no
* annotations.
*
* If this {@code Executable} object represents a static method or
* represents a constructor of a top level, static member, local, or
* anonymous class, then the return value is null.
*
* @return an object representing the receiver type of the method or
* constructor represented by this {@code Executable} or {@code null} if
* this {@code Executable} can not have a receiver parameter
*/
// 获取Receiver Type上的【被注解类型】
public AnnotatedType getAnnotatedReceiverType() {
if(Modifier.isStatic(this.getModifiers()))
return null;
return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(), SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()), this, getDeclaringClass(), getDeclaringClass(), TypeAnnotation.TypeAnnotationTarget.METHOD_RECEIVER);
}
/**
* Returns an array of {@code AnnotatedType} objects that represent the use
* of types to specify the declared exceptions of the method/constructor
* represented by this Executable. The order of the objects in the array
* corresponds to the order of the exception types in the declaration of
* the method/constructor.
*
* Returns an array of length 0 if the method/constructor declares no
* exceptions.
*
* @return an array of objects representing the declared
* exceptions of the method or constructor represented by this {@code
* Executable}
*/
// 获取异常上的【被注解类型】
public AnnotatedType[] getAnnotatedExceptionTypes() {
return TypeAnnotationParser.buildAnnotatedTypes(getTypeAnnotationBytes0(), SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()), this, getDeclaringClass(), getGenericExceptionTypes(), TypeAnnotation.TypeAnnotationTarget.THROWS);
}
/*▲ 注解 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 特征 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Returns the {@code Class} object representing the class or interface
* that declares the executable represented by this object.
*/
// 返回元素所在的类
public abstract Class<?> getDeclaringClass();
/**
* Returns {@code true} if this executable is a synthetic
* construct; returns {@code false} otherwise.
*
* @return true if and only if this executable is a synthetic
* construct as defined by
* <cite>The Java™ Language Specification</cite>.
*
* @jls 13.1 The Form of a Binary
*/
// 是否由编译器引入(非人为定义)
public boolean isSynthetic() {
return Modifier.isSynthetic(getModifiers());
}
/*▲ 特征 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* Accessor method to allow code sharing
*/
abstract byte[] getAnnotationBytes();
/**
* Does the Executable have generic information.
*/
abstract boolean hasGenericInformation();
abstract ConstructorRepository getGenericInfo();
/**
* Generate toString header information specific to a method or
* constructor.
*/
abstract void specificToStringHeader(StringBuilder sb);
/**
* Generate toGenericString header information specific to a
* method or constructor.
*/
abstract void specificToGenericStringHeader(StringBuilder sb);
// returns shared array of parameter types - must never give it out to the untrusted code...
abstract Class<?>[] getSharedParameterTypes();
// returns shared array of exception types - must never give it out to the untrusted code...
abstract Class<?>[] getSharedExceptionTypes();
abstract boolean handleParameterNumberMismatch(int resultLength, int numParameters);
boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
/* Avoid unnecessary cloning */
if(params1.length == params2.length) {
for(int i = 0; i<params1.length; i++) {
if(params1[i] != params2[i])
return false;
}
return true;
}
return false;
}
Annotation[][] parseParameterAnnotations(byte[] parameterAnnotations) {
return AnnotationParser.parseParameterAnnotations(parameterAnnotations, SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()), getDeclaringClass());
}
void printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault) {
int mod = getModifiers() & mask;
if(mod != 0 && !isDefault) {
sb.append(Modifier.toString(mod)).append(' ');
} else {
int access_mod = mod & Modifier.ACCESS_MODIFIERS;
if(access_mod != 0)
sb.append(Modifier.toString(access_mod)).append(' ');
if(isDefault)
sb.append("default ");
mod = (mod & ~Modifier.ACCESS_MODIFIERS);
if(mod != 0)
sb.append(Modifier.toString(mod)).append(' ');
}
}
String sharedToString(int modifierMask, boolean isDefault, Class<?>[] parameterTypes, Class<?>[] exceptionTypes) {
try {
StringBuilder sb = new StringBuilder();
printModifiersIfNonzero(sb, modifierMask, isDefault);
specificToStringHeader(sb);
sb.append('(');
StringJoiner sj = new StringJoiner(",");
for(Class<?> parameterType : parameterTypes) {
sj.add(parameterType.getTypeName());
}
sb.append(sj.toString());
sb.append(')');
if(exceptionTypes.length>0) {
StringJoiner joiner = new StringJoiner(",", " throws ", "");
for(Class<?> exceptionType : exceptionTypes) {
joiner.add(exceptionType.getTypeName());
}
sb.append(joiner.toString());
}
return sb.toString();
} catch(Exception e) {
return "<" + e + ">";
}
}
String sharedToGenericString(int modifierMask, boolean isDefault) {
try {
StringBuilder sb = new StringBuilder();
printModifiersIfNonzero(sb, modifierMask, isDefault);
TypeVariable<?>[] typeparms = getTypeParameters();
if(typeparms.length>0) {
StringJoiner sj = new StringJoiner(",", "<", "> ");
for(TypeVariable<?> typeparm : typeparms) {
sj.add(typeparm.getTypeName());
}
sb.append(sj.toString());
}
specificToGenericStringHeader(sb);
sb.append('(');
StringJoiner sj = new StringJoiner(",");
Type[] params = getGenericParameterTypes();
for(int j = 0; j<params.length; j++) {
String param = params[j].getTypeName();
if(isVarArgs() && (j == params.length - 1)) // replace T[] with T...
param = param.replaceFirst("\\[\\]$", "...");
sj.add(param);
}
sb.append(sj.toString());
sb.append(')');
Type[] exceptionTypes = getGenericExceptionTypes();
if(exceptionTypes.length>0) {
StringJoiner joiner = new StringJoiner(",", " throws ", "");
for(Type exceptionType : exceptionTypes) {
joiner.add(exceptionType.getTypeName());
}
sb.append(joiner.toString());
}
return sb.toString();
} catch(Exception e) {
return "<" + e + ">";
}
}
/**
* Behaves like {@code getGenericParameterTypes}, but returns type
* information for all parameters, including synthetic parameters.
*/
Type[] getAllGenericParameterTypes() {
final boolean genericInfo = hasGenericInformation();
// Easy case: we don't have generic parameter information. In
// this case, we just return the result of
// getParameterTypes().
if(!genericInfo) {
return getParameterTypes();
} else {
final boolean realParamData = hasRealParameterData();
final Type[] genericParamTypes = getGenericParameterTypes();
final Type[] nonGenericParamTypes = getParameterTypes();
final Type[] out = new Type[nonGenericParamTypes.length];
final Parameter[] params = getParameters();
int fromidx = 0;
// If we have real parameter data, then we use the
// synthetic and mandate flags to our advantage.
if(realParamData) {
for(int i = 0; i<out.length; i++) {
final Parameter param = params[i];
if(param.isSynthetic() || param.isImplicit()) {
// If we hit a synthetic or mandated parameter,
// use the non generic parameter info.
out[i] = nonGenericParamTypes[i];
} else {
// Otherwise, use the generic parameter info.
out[i] = genericParamTypes[fromidx];
fromidx++;
}
}
} else {
// Otherwise, use the non-generic parameter data.
// Without method parameter reflection data, we have
// no way to figure out which parameters are
// synthetic/mandated, thus, no way to match up the
// indexes.
return genericParamTypes.length == nonGenericParamTypes.length ? genericParamTypes : nonGenericParamTypes;
}
return out;
}
}
boolean hasRealParameterData() {
// If this somehow gets called before parameters gets
// initialized, force it into existence.
if(parameters == null) {
privateGetParameters();
}
return hasRealParameterData;
}
native byte[] getTypeAnnotationBytes0();
// Needed by reflectaccess
byte[] getTypeAnnotationBytes() {
return getTypeAnnotationBytes0();
}
Annotation[][] sharedGetParameterAnnotations(Class<?>[] parameterTypes, byte[] parameterAnnotations) {
int numParameters = parameterTypes.length;
if(parameterAnnotations == null)
return new Annotation[numParameters][0];
Annotation[][] result = parseParameterAnnotations(parameterAnnotations);
if(result.length != numParameters && handleParameterNumberMismatch(result.length, numParameters)) {
Annotation[][] tmp = new Annotation[result.length + 1][];
// Shift annotations down one to account for an implicit leading parameter
System.arraycopy(result, 0, tmp, 1, result.length);
tmp[0] = new Annotation[0];
result = tmp;
}
return result;
}
/**
* Helper for subclasses of Executable.
*
* Returns an AnnotatedType object that represents the use of a type to
* specify the return type of the method/constructor represented by this
* Executable.
*/
AnnotatedType getAnnotatedReturnType0(Type returnType) {
return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(), SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()), this, getDeclaringClass(), returnType, TypeAnnotation.TypeAnnotationTarget.METHOD_RETURN);
}
private Parameter[] synthesizeAllParams() {
final int realparams = getParameterCount();
final Parameter[] out = new Parameter[realparams];
for(int i = 0; i<realparams; i++)
// TODO: is there a way to synthetically derive the
// modifiers? Probably not in the general case, since
// we'd have no way of knowing about them, but there
// may be specific cases.
out[i] = new Parameter("arg" + i, 0, this, i);
return out;
}
private void verifyParameters(final Parameter[] parameters) {
final int mask = Modifier.FINAL | Modifier.SYNTHETIC | Modifier.MANDATED;
if(getParameterTypes().length != parameters.length)
throw new MalformedParametersException("Wrong number of parameters in MethodParameters attribute");
for(Parameter parameter : parameters) {
final String name = parameter.getRealName();
final int mods = parameter.getModifiers();
if(name != null) {
if(name.isEmpty() || name.indexOf('.') != -1 || name.indexOf(';') != -1 || name.indexOf('[') != -1 || name.indexOf('/') != -1) {
throw new MalformedParametersException("Invalid parameter name \"" + name + "\"");
}
}
if(mods != (mods & mask)) {
throw new MalformedParametersException("Invalid parameter modifiers");
}
}
}
private Parameter[] privateGetParameters() {
// Use tmp to avoid multiple writes to a volatile.
Parameter[] tmp = parameters;
if(tmp == null) {
// Otherwise, go to the JVM to get them
try {
tmp = getParameters0();
} catch(IllegalArgumentException e) {
// Rethrow ClassFormatErrors
throw new MalformedParametersException("Invalid constant pool index");
}
// If we get back nothing, then synthesize parameters
if(tmp == null) {
hasRealParameterData = false;
tmp = synthesizeAllParams();
} else {
hasRealParameterData = true;
verifyParameters(tmp);
}
parameters = tmp;
}
return tmp;
}
private native Parameter[] getParameters0();
private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
Map<Class<? extends Annotation>, Annotation> declAnnos;
if((declAnnos = declaredAnnotations) == null) {
synchronized(this) {
if((declAnnos = declaredAnnotations) == null) {
Executable root = (Executable) getRoot();
if(root != null) {
declAnnos = root.declaredAnnotations();
} else {
declAnnos = AnnotationParser.parseAnnotations(getAnnotationBytes(), SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()), getDeclaringClass());
}
declaredAnnotations = declAnnos;
}
}
}
return declAnnos;
}
}