Skip to content

Commit 233e6dd

Browse files
committed
Guard null in candidate class detection
Prevent a potential NullPointerException by returning false when `isCandidateClass` receives `null`. Added a focused unit test for `isCandidateClass` covering null, primitive/wrapper, enum, `java.lang` type, and a positive custom-class case.
1 parent 8f14522 commit 233e6dd

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

microsphere-spring-boot-core/src/main/java/io/microsphere/spring/boot/context/properties/bind/ConfigurationPropertiesBeanContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ static boolean isCandidateProperty(PropertyDescriptor descriptor) {
562562
* @return {@code true} if the class is a binding candidate, {@code false} otherwise
563563
*/
564564
static boolean isCandidateClass(Class<?> beanClass) {
565+
if (beanClass == null) {
566+
return false;
567+
}
565568
if (isPrimitiveOrWrapper(beanClass) || beanClass.isEnum()) {
566569
return false;
567570
}

microsphere-spring-boot-core/src/test/java/io/microsphere/spring/boot/context/properties/bind/ConfigurationPropertiesBeanContextTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import org.springframework.core.annotation.AnnotationAttributes;
3232

3333
import java.beans.PropertyDescriptor;
34+
import java.util.concurrent.TimeUnit;
3435

3536
import static io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanContext.getInstance;
37+
import static io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanContext.isCandidateClass;
3638
import static io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanContext.isCandidateProperty;
3739
import static io.microsphere.spring.core.annotation.AnnotationUtils.getAnnotationAttributes;
3840
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -151,6 +153,21 @@ void testIsCandidateProperty() {
151153
assertFalse(isCandidateProperty(descriptor));
152154
}
153155

156+
@Test
157+
void testIsCandidateClass() {
158+
// null
159+
assertFalse(isCandidateClass(null));
160+
// primitive type and wrapper type are not candidate class
161+
assertFalse(isCandidateClass(int.class));
162+
assertFalse(isCandidateClass(Integer.class));
163+
// enumeration type
164+
assertFalse(isCandidateClass(TimeUnit.class));
165+
// String is candidate class,but is under java.lang package.
166+
assertFalse(isCandidateClass(String.class));
167+
// current class
168+
assertTrue(isCandidateClass(getClass()));
169+
}
170+
154171
public void setName(String name) {
155172
// Just for testing purpose
156173
}

0 commit comments

Comments
 (0)