33
33
import java .lang .reflect .Array ;
34
34
import java .lang .reflect .Constructor ;
35
35
import java .lang .reflect .Field ;
36
+ import java .lang .reflect .InaccessibleObjectException ;
36
37
import java .lang .reflect .InvocationTargetException ;
37
38
import java .lang .reflect .Method ;
38
39
import java .lang .reflect .Modifier ;
99
100
import java .util .concurrent .atomic .AtomicLong ;
100
101
import java .util .concurrent .atomic .AtomicLongArray ;
101
102
import java .util .regex .Pattern ;
103
+ import java .util .stream .Collectors ;
102
104
103
- import javax .activation .DataSource ;
104
- import javax .activation .FileDataSource ;
105
+ import jakarta .activation .DataSource ;
106
+ import jakarta .activation .FileDataSource ;
105
107
import javax .sql .rowset .serial .SerialBlob ;
106
108
import javax .sql .rowset .serial .SerialException ;
107
109
import javax .xml .datatype .DatatypeConfigurationException ;
@@ -556,8 +558,10 @@ private static void checkGettersAndSetters(ArrayList<Class<?>> constructedClasse
556
558
throws ClassNotFoundException , InstantiationException , IllegalAccessException , InvocationTargetException {
557
559
558
560
// all public methods are relevant
559
- List <Method > publicMethods = Arrays .asList (dtoClass .getMethods ());
561
+ List <Method > publicMethods = Arrays .stream (dtoClass .getMethods ()). collect ( Collectors . toCollection ( ArrayList :: new ));
560
562
563
+ clearMethods (publicMethods );
564
+
561
565
// all protected methods are relevant
562
566
ArrayList <Method > allMethods = getInheritedProtectedMethods (dtoClass );
563
567
@@ -590,11 +594,11 @@ else if (current.getName().startsWith("is")) {
590
594
}
591
595
}
592
596
}
593
-
597
+
594
598
for (Method method : toRemove ) {
595
599
allMethods .remove (method );
596
600
}
597
-
601
+
598
602
try {
599
603
constructSetMethods (constructedClasses , dtoClass , constructedObjects , allMethods , implOfAbstractClasses ,
600
604
specialValues );
@@ -2188,12 +2192,19 @@ private static ExtractionValue extractValueFromField(Class<?> dtoClass, Method m
2188
2192
2189
2193
// if no value is found in the direct class, check super classes
2190
2194
List <Field > inheritedFields = getInheritedFields (dtoClass );
2191
-
2195
+
2192
2196
for (Field field : inheritedFields ) {
2193
- if (StringUtils .uncapitalize (method .getName ().substring (3 )).equals (field .getName ())) {
2194
- field .setAccessible (true );
2195
- return new ExtractionValue (true , field .get (constructedObject ));
2196
- }
2197
+ if (StringUtils .uncapitalize (method .getName ().substring (3 )).equals (field .getName ())) {
2198
+ try {
2199
+ field .setAccessible (true );
2200
+ return new ExtractionValue (true , field .get (constructedObject ));
2201
+ }
2202
+ catch (InaccessibleObjectException ioe ) {
2203
+ //ignore accessibility problems due to java module system
2204
+ System .err .println ("Can not fully test class " +dtoClass .getName ()+" due to accessability problems of " +field );
2205
+ return new ExtractionValue (false , field .get (constructedObject ));
2206
+ }
2207
+ }
2197
2208
}
2198
2209
2199
2210
return new ExtractionValue (false , null );
@@ -2410,6 +2421,19 @@ private static List<Field> getInheritedFields(Class<?> clazz) {
2410
2421
fields .addAll (Arrays .asList (classToCheck .getSuperclass ().getDeclaredFields ()));
2411
2422
classToCheck = classToCheck .getSuperclass ();
2412
2423
}
2424
+
2425
+ //exclude special problematic fields because access is restricted
2426
+ ArrayList <Field > toBeRemoved = new ArrayList <>();
2427
+ for (Field field : fields ) {
2428
+ if (field .getName ().equals ("stackTrace" )) {
2429
+ toBeRemoved .add (field );
2430
+ } else if (field .getName ().equals ("finalize" )) {
2431
+ toBeRemoved .add (field );
2432
+ } else if (field .getName ().equals ("clone" )) {
2433
+ toBeRemoved .add (field );
2434
+ }
2435
+ }
2436
+ fields .removeAll (toBeRemoved );
2413
2437
2414
2438
return fields ;
2415
2439
}
@@ -2433,12 +2457,20 @@ private static ArrayList<Method> getInheritedProtectedMethods(Class<?> clazz) {
2433
2457
classToCheck = classToCheck .getSuperclass ();
2434
2458
}
2435
2459
2460
+ clearMethods (methods );
2461
+
2436
2462
// clear public and private methods
2437
2463
for (Method method : methods ) {
2438
- if (Modifier .isProtected (method .getModifiers ())) {
2439
- method .setAccessible (true );
2440
- protectedMethods .add (method );
2441
- }
2464
+ if (Modifier .isProtected (method .getModifiers ())) {
2465
+ try {
2466
+ method .setAccessible (true );
2467
+ protectedMethods .add (method );
2468
+ }
2469
+ catch (InaccessibleObjectException ioe ) {
2470
+ //ignore accessibility problems due to java module system
2471
+ System .err .println ("Can not fully test class " +clazz .getName ()+" due to accessability problems: " +method );
2472
+ }
2473
+ }
2442
2474
}
2443
2475
2444
2476
return protectedMethods ;
@@ -2505,8 +2537,7 @@ private static void specialValuesValid(Constructor<?>[] constructors, SpecialVal
2505
2537
}
2506
2538
}
2507
2539
if (!foundMatch && value .getNumberOfArguments () != 0 ) {
2508
- System .err .println ("The following special value " + specialValues
2509
- + " could not be matched to any constructor argument. Check the index and the data type." );
2540
+ System .err .println ("The special value can not be matched to a constructor argument. Check the index and the data type. Special values: " + specialValues );
2510
2541
}
2511
2542
}
2512
2543
}
@@ -2545,6 +2576,21 @@ private static X509Certificate readCertificate() {
2545
2576
2546
2577
return cert ;
2547
2578
}
2579
+
2580
+ private static void clearMethods (List <Method > methods ) {
2581
+ //exclude special problematic methods because access is restricted
2582
+ ArrayList <Method > toBeRemoved = new ArrayList <>();
2583
+ for (Method method : methods ) {
2584
+ if ((method .getName ().equals ("getStackTrace" ) || method .getName ().equals ("setStackTrace" ))) {
2585
+ toBeRemoved .add (method );
2586
+ } else if (method .getName ().equals ("finalize" )) {
2587
+ toBeRemoved .add (method );
2588
+ } else if (method .getName ().equals ("clone" )) {
2589
+ toBeRemoved .add (method );
2590
+ }
2591
+ }
2592
+ methods .removeAll (toBeRemoved );
2593
+ }
2548
2594
2549
2595
static Byte getRandomByte () {
2550
2596
byte [] b = new byte [] {0 };
0 commit comments