Skip to content

Commit 844dad7

Browse files
committed
Fixed a Sonarqube finding and added more unit tests to ensure all available classes are loaded
1 parent 6261d06 commit 844dad7

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal/ClassPreLoader.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff 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
}

powertools-common/src/test/java/software/amazon/lambda/powertools/common/internal/ClassPreLoaderTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,29 @@
66

77
class 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
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
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

0 commit comments

Comments
 (0)