26
26
package java .lang ;
27
27
28
28
import java .lang .annotation .Annotation ;
29
- import java .lang .classfile .Attribute ;
29
+ import java .lang .classfile .Attributes ;
30
30
import java .lang .classfile .ClassFile ;
31
31
import java .lang .classfile .ClassModel ;
32
32
import java .lang .classfile .MethodModel ;
105
105
import sun .reflect .generics .factory .GenericsFactory ;
106
106
import sun .reflect .generics .repository .ClassRepository ;
107
107
import sun .reflect .generics .repository .MethodRepository ;
108
- import sun .reflect .generics .repository .ConstructorRepository ;
108
+ import sun .reflect .generics .repository .ExecutableRepository ;
109
109
import sun .reflect .generics .scope .ClassScope ;
110
110
import sun .security .util .SecurityConstants ;
111
111
import sun .reflect .annotation .*;
@@ -1737,7 +1737,7 @@ public Constructor<?> getEnclosingConstructor() throws SecurityException {
1737
1737
if (!enclosingInfo .isConstructor ())
1738
1738
return null ;
1739
1739
1740
- ConstructorRepository typeInfo = ConstructorRepository .make (enclosingInfo .getDescriptor (),
1740
+ ExecutableRepository typeInfo = ExecutableRepository .make (enclosingInfo .getDescriptor (),
1741
1741
getFactory ());
1742
1742
Type [] parameterTypes = typeInfo .getParameterTypes ();
1743
1743
Class <?>[] parameterClasses = new Class <?>[parameterTypes .length ];
@@ -2305,8 +2305,8 @@ public Constructor<?>[] getConstructors() throws SecurityException {
2305
2305
}
2306
2306
2307
2307
private Method [] filterOutDeconstructorsFromMethods (Method [] in ) {
2308
- if (this .getClassLoader () != null ) {
2309
- Map <String , Boolean > isNotPattern = new HashMap <>();
2308
+ if (this .getClassLoader () != null ) {
2309
+ Set <String > isPattern = new HashSet <>();
2310
2310
ClassModel cm = null ;
2311
2311
try (InputStream resource = this .getClassLoader ().getResourceAsStream (getName () + ".class" )) {
2312
2312
if (resource == null ) {
@@ -2318,13 +2318,10 @@ private Method[] filterOutDeconstructorsFromMethods(Method[] in) {
2318
2318
throw new RuntimeException (e );
2319
2319
}
2320
2320
for (MethodModel mm : cm .methods ()) {
2321
- PatternAttribute pa = null ;
2322
- for (Attribute <?> attribute : mm .attributes ()) {
2323
- if (attribute instanceof PatternAttribute pap ) pa = pap ;
2324
- }
2325
- isNotPattern .put (mm .methodName ().stringValue (), pa == null );
2321
+ PatternAttribute pa = mm .findAttribute (Attributes .pattern ()).orElse (null );
2322
+ if (pa != null ) isPattern .add (mm .methodName ().stringValue ());
2326
2323
}
2327
- Method [] ret = Arrays .stream (in ).filter (m -> isNotPattern . getOrDefault (m .getName (), true )).toArray (Method []::new );
2324
+ Method [] ret = Arrays .stream (in ).filter (m -> ! isPattern . contains (m .getName ())).toArray (Method []::new );
2328
2325
return ret ;
2329
2326
} else {
2330
2327
return in ;
@@ -2353,12 +2350,12 @@ private Method[] filterOutDeconstructorsFromMethods(Method[] in) {
2353
2350
2354
2351
/**
2355
2352
* Returns a {@code Deconstructor} object that reflects the specified
2356
- * public constructor of the class represented by this {@code Class}
2353
+ * public deconstructor of the class represented by this {@code Class}
2357
2354
* object.
2358
2355
*
2359
2356
* @param bindingTypes the array of the types of the bindings.
2360
- * @return the {@code Constructor } object of the public constructor that
2361
- * matches the specified {@code parameterTypes }
2357
+ * @return the {@code Deconstructor } object of the public deconstructor that
2358
+ * matches the specified {@code bindingTypes }
2362
2359
* @throws NoSuchPatternException if a matching deconstructor is not found.
2363
2360
*
2364
2361
* @throws SecurityException
@@ -2423,19 +2420,13 @@ private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int whi
2423
2420
byte [] bytes = is .readAllBytes ();
2424
2421
ClassModel cm = ClassFile .of ().parse (bytes );
2425
2422
for (MethodModel mm : cm .methods ()) {
2426
- PatternAttribute pa = null ;
2427
- for (Attribute <?> attribute : mm .attributes ()) {
2428
- if (attribute instanceof PatternAttribute pa_ ) pa = pa_ ;
2429
- }
2423
+ PatternAttribute pa = mm .findAttribute (Attributes .pattern ()).orElse (null );
2430
2424
if (pa != null ) {
2431
2425
String descriptorFilter = null ;
2432
2426
2433
2427
// generic signature detection
2434
- SignatureAttribute sa = null ;
2435
- for (Attribute <?> attribute : pa .attributes ()) {
2436
- if (attribute instanceof SignatureAttribute sa_ ) sa = sa_ ;
2437
- }
2438
- List <String > signatures = List .of ();
2428
+ SignatureAttribute sa = pa .findAttribute (Attributes .signature ()).orElse (null );
2429
+ List <String > signatures = null ;
2439
2430
if (sa != null ) {
2440
2431
signatures = sa .asMethodSignature ().arguments ().stream ().map (a -> a .signatureString ()).toList ();
2441
2432
} else {
@@ -2451,17 +2442,8 @@ private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int whi
2451
2442
if ((params .length == 0 || (params .length != 0 && pa .patternTypeSymbol ().descriptorString ().equals (descriptorFilter ))) &&
2452
2443
(which == Member .DECLARED || mm .flags ().has (AccessFlag .PUBLIC ))) {
2453
2444
// binding annotations
2454
- RuntimeVisibleAnnotationsAttribute rva = null ;
2455
- for (Attribute <?> attribute : mm .attributes ()) {
2456
- if (attribute instanceof RuntimeVisibleAnnotationsAttribute rva_ ) rva = rva_ ;
2457
- }
2458
- ByteBuffer assembled_rva = null ;
2459
- if (rva != null ) {
2460
- byte rvaBytes [] = ((BoundAttribute ) rva ).contents (); // returns the full attribute
2461
- int rva_length = ((BoundAttribute ) rva ).payloadLen (); // already comes after the subtraction with 4
2462
- assembled_rva = ByteBuffer .wrap (rvaBytes , 0 , rva_length );
2463
- // TODO: RuntimeInVisibleAnnotationsAttribute
2464
- }
2445
+ RuntimeVisibleAnnotationsAttribute rva = mm .findAttribute (Attributes .runtimeVisibleAnnotations ()).orElse (null );
2446
+ ByteBuffer assembled_rva = getAnnotationContents (rva != null , (BoundAttribute ) rva );
2465
2447
2466
2448
ArrayList <PatternBinding > deconstructorBindings = new ArrayList <>();
2467
2449
Deconstructor <?> currentDeconstructor = new Deconstructor <T >(this ,
@@ -2475,37 +2457,16 @@ private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int whi
2475
2457
2476
2458
// parameter names
2477
2459
var parameterList = pa .patternTypeSymbol ().parameterList ();
2478
- MethodParametersAttribute mp = null ;
2479
- for (Attribute <?> attribute : pa .attributes ()) {
2480
- if (attribute instanceof MethodParametersAttribute mp_ ) mp = mp_ ;
2481
- }
2460
+ MethodParametersAttribute mp = pa .findAttribute (Attributes .methodParameters ()).orElse (null );
2482
2461
List <String > parameterNameList = mp .parameters ().stream ().map (p -> p .name ().get ().stringValue ()).toList ();
2483
2462
2484
2463
// binding annotations
2485
- RuntimeVisibleParameterAnnotationsAttribute rvpa = null ;
2486
- for (Attribute <?> attribute : pa .attributes ()) {
2487
- if (attribute instanceof RuntimeVisibleParameterAnnotationsAttribute rvpa_ ) rvpa = rvpa_ ;
2488
- }
2489
- ByteBuffer assembled_rvpa = null ;
2490
- if (rvpa != null ) {
2491
- byte rvpaBytes [] = ((BoundAttribute ) rvpa ).contents ();
2492
- int rvpa_length = ((BoundAttribute ) rvpa ).payloadLen ();
2493
- assembled_rvpa = ByteBuffer .wrap (rvpaBytes , 0 , rvpa_length );
2494
- // TODO: RuntimeInVisibleParameterAnnotationsAttribute
2495
- }
2464
+ RuntimeVisibleParameterAnnotationsAttribute rvpa = pa .findAttribute (Attributes .runtimeVisibleParameterAnnotations ()).orElse (null );
2465
+ ByteBuffer assembled_rvpa = getAnnotationContents (rvpa != null , (BoundAttribute ) rvpa );
2496
2466
2497
2467
// binding type annotations
2498
- RuntimeVisibleTypeAnnotationsAttribute rvta = null ;
2499
- for (Attribute <?> attribute : pa .attributes ()) {
2500
- if (attribute instanceof RuntimeVisibleTypeAnnotationsAttribute rvta_ ) rvta = rvta_ ;
2501
- }
2502
- ByteBuffer assembled_rvta = null ;
2503
- if (rvpa != null ) {
2504
- byte rvtaBytes [] = ((BoundAttribute ) rvpa ).contents ();
2505
- int rvta_length = ((BoundAttribute ) rvpa ).payloadLen ();
2506
- assembled_rvta = ByteBuffer .wrap (rvtaBytes , 0 , rvta_length );
2507
- // TODO: RuntimeInVisibleTypeAnnotationsAttribute
2508
- }
2468
+ RuntimeVisibleTypeAnnotationsAttribute rvta = pa .findAttribute (Attributes .runtimeVisibleTypeAnnotations ()).orElse (null );
2469
+ ByteBuffer assembled_rvta = getAnnotationContents (rvta != null , (BoundAttribute ) rvta );
2509
2470
2510
2471
for (int i = 0 ; i < parameterList .size (); i ++) {
2511
2472
Class <?> bindingClass = parameterList .get (i ).resolveConstantDesc (MethodHandles .lookup ());
@@ -2531,6 +2492,15 @@ private Deconstructor<?>[] getDeclaredDeconstructors0(Class<?>[] params, int whi
2531
2492
return decs .toArray (new Deconstructor <?>[decs .size ()]);
2532
2493
}
2533
2494
2495
+ private static ByteBuffer getAnnotationContents (boolean exists , BoundAttribute <?> boundAttribute ) {
2496
+ if (exists ) {
2497
+ byte rvpaBytes [] = boundAttribute .contents ();
2498
+ int rvpa_length = boundAttribute .payloadLen ();
2499
+ return ByteBuffer .wrap (rvpaBytes , 0 , rvpa_length );
2500
+ }
2501
+ return null ;
2502
+ }
2503
+
2534
2504
/**
2535
2505
* Returns a {@code Deconstructor} object that reflects the specified
2536
2506
* deconstructor of the class represented by this
0 commit comments