File tree Expand file tree Collapse file tree 3 files changed +36
-6
lines changed
main/java/software/amazon/lambda/powertools/common/internal
java/software/amazon/lambda/powertools/common/internal Expand file tree Collapse file tree 3 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -68,15 +68,24 @@ private static void preloadClassesFromStream(InputStream is) {
6868 }
6969 final String className = line .stripTrailing ();
7070 if (!className .isBlank ()) {
71- try {
72- Class .forName (className , true , ClassPreLoader .class .getClassLoader ());
73- } catch (ClassNotFoundException e ) {
74- // No action is required if a class listed in the file cannot be found
75- }
71+ loadClassIfFound (className );
7672 }
7773 }
7874 } catch (Exception ignored ) {
7975 // No action is required if preloading fails for any reason
8076 }
8177 }
78+
79+ /**
80+ * Initializes the class with given name if found, ignores otherwise
81+ *
82+ * @param className
83+ */
84+ private static void loadClassIfFound (String className ) {
85+ try {
86+ Class .forName (className , true , ClassPreLoader .class .getClassLoader ());
87+ } catch (ClassNotFoundException e ) {
88+ // No action is required if the class with given name cannot be found
89+ }
90+ }
8291}
Original file line number Diff line number Diff line change 66
77class ClassPreLoaderTest {
88
9+ // Making this volatile so the Thread Context doesn't need any special handling
10+ static volatile boolean dummyClassLoaded = false ;
11+
12+ /**
13+ * Dummy class to be loaded by ClassPreLoader in test.
14+ * <b>The class name is referenced in <i>powertools-common/src/test/resources/classesloaded.txt</i></b>
15+ * This class is used to verify that the ClassPreLoader can load valid classes.
16+ * The static block sets a flag to indicate that the class has been loaded.
17+ */
18+ static class DummyClass {
19+ static {
20+ dummyClassLoaded = true ;
21+ }
22+ }
923 @ Test
1024 void preloadClasses_shouldIgnoreInvalidClassesAndLoadValidClasses () {
25+
26+ dummyClassLoaded = false ;
27+ // powertools-common/src/test/resources/classesloaded.txt has a class that does not exist
1128 // Verify that the missing class did not throw any exception
1229 assertDoesNotThrow (ClassPreLoader ::preloadClasses );
30+
31+ // When the classloaded.txt is a mixed bag of valid and invalid classes, Valid class must load
32+ assertTrue (dummyClassLoaded , "DummyClass should be loaded" );
1333 }
1434}
Original file line number Diff line number Diff line change 1- software.amazon.lambda.powertools.common.internal.NonExistingClass
1+ software.amazon.lambda.powertools.common.internal.NonExistingClass
2+ software.amazon.lambda.powertools.common.internal.ClassPreLoaderTest$DummyClass
You can’t perform that action at this time.
0 commit comments