Skip to content

Commit 9beb978

Browse files
committed
Support for static field access on non-public enums
Issue: SPR-16284
1 parent 7ad69bf commit 9beb978

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -346,6 +346,7 @@ private Object attemptToConvertStringToEnum(Class<?> requiredType, String trimme
346346
// to be checked, hence we don't return it right away.
347347
try {
348348
Field enumField = requiredType.getField(trimmedValue);
349+
ReflectionUtils.makeAccessible(enumField);
349350
convertedValue = enumField.get(null);
350351
}
351352
catch (Throwable ex) {

spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -173,4 +173,32 @@ public void testStandardEnumMapWithAutoGrowing() {
173173
assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1));
174174
}
175175

176+
@Test
177+
public void testNonPublicEnum() {
178+
NonPublicEnumHolder holder = new NonPublicEnumHolder();
179+
BeanWrapper bw = new BeanWrapperImpl(holder);
180+
bw.setPropertyValue("nonPublicEnum", "VALUE_1");
181+
assertEquals(NonPublicEnum.VALUE_1, holder.getNonPublicEnum());
182+
}
183+
184+
185+
enum NonPublicEnum {
186+
187+
VALUE_1, VALUE_2;
188+
}
189+
190+
191+
static class NonPublicEnumHolder {
192+
193+
private NonPublicEnum nonPublicEnum;
194+
195+
public NonPublicEnum getNonPublicEnum() {
196+
return nonPublicEnum;
197+
}
198+
199+
public void setNonPublicEnum(NonPublicEnum nonPublicEnum) {
200+
this.nonPublicEnum = nonPublicEnum;
201+
}
202+
}
203+
176204
}

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -2736,6 +2736,16 @@ public void emptyJavaUtilOptionalBean() {
27362736
assertSame(Optional.empty(), bf.getBean(Optional.class));
27372737
}
27382738

2739+
@Test
2740+
public void testNonPublicEnum() {
2741+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
2742+
RootBeanDefinition bd = new RootBeanDefinition(NonPublicEnumHolder.class);
2743+
bd.getConstructorArgumentValues().addGenericArgumentValue("VALUE_1");
2744+
bf.registerBeanDefinition("holderBean", bd);
2745+
NonPublicEnumHolder holder = (NonPublicEnumHolder) bf.getBean("holderBean");
2746+
assertEquals(NonPublicEnum.VALUE_1, holder.getNonPublicEnum());
2747+
}
2748+
27392749
/**
27402750
* Test that by-type bean lookup caching is working effectively by searching for a
27412751
* bean of type B 10K times within a container having 1K additional beans of type A.
@@ -3263,4 +3273,24 @@ public boolean isSingleton() {
32633273
}
32643274
}
32653275

3276+
3277+
enum NonPublicEnum {
3278+
3279+
VALUE_1, VALUE_2;
3280+
}
3281+
3282+
3283+
static class NonPublicEnumHolder {
3284+
3285+
final NonPublicEnum nonPublicEnum;
3286+
3287+
public NonPublicEnumHolder(NonPublicEnum nonPublicEnum) {
3288+
this.nonPublicEnum = nonPublicEnum;
3289+
}
3290+
3291+
public NonPublicEnum getNonPublicEnum() {
3292+
return nonPublicEnum;
3293+
}
3294+
}
3295+
32663296
}

0 commit comments

Comments
 (0)