From 9beb97880fe1668f59fbc9715739357c130b0522 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 7 Jan 2018 23:19:47 +0100 Subject: [PATCH] Support for static field access on non-public enums Issue: SPR-16284 --- .../beans/TypeConverterDelegate.java | 3 +- .../beans/BeanWrapperEnumTests.java | 30 ++++++++++++++++- .../DefaultListableBeanFactoryTests.java | 32 ++++++++++++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java index d334440e44..eeadc330a1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -346,6 +346,7 @@ class TypeConverterDelegate { // to be checked, hence we don't return it right away. try { Field enumField = requiredType.getField(trimmedValue); + ReflectionUtils.makeAccessible(enumField); convertedValue = enumField.get(null); } catch (Throwable ex) { diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java index a06bad4780..f5c87cdf9d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -173,4 +173,32 @@ public class BeanWrapperEnumTests { assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1)); } + @Test + public void testNonPublicEnum() { + NonPublicEnumHolder holder = new NonPublicEnumHolder(); + BeanWrapper bw = new BeanWrapperImpl(holder); + bw.setPropertyValue("nonPublicEnum", "VALUE_1"); + assertEquals(NonPublicEnum.VALUE_1, holder.getNonPublicEnum()); + } + + + enum NonPublicEnum { + + VALUE_1, VALUE_2; + } + + + static class NonPublicEnumHolder { + + private NonPublicEnum nonPublicEnum; + + public NonPublicEnum getNonPublicEnum() { + return nonPublicEnum; + } + + public void setNonPublicEnum(NonPublicEnum nonPublicEnum) { + this.nonPublicEnum = nonPublicEnum; + } + } + } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index a69ee9606d..7c5390ee00 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -2736,6 +2736,16 @@ public class DefaultListableBeanFactoryTests { assertSame(Optional.empty(), bf.getBean(Optional.class)); } + @Test + public void testNonPublicEnum() { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + RootBeanDefinition bd = new RootBeanDefinition(NonPublicEnumHolder.class); + bd.getConstructorArgumentValues().addGenericArgumentValue("VALUE_1"); + bf.registerBeanDefinition("holderBean", bd); + NonPublicEnumHolder holder = (NonPublicEnumHolder) bf.getBean("holderBean"); + assertEquals(NonPublicEnum.VALUE_1, holder.getNonPublicEnum()); + } + /** * Test that by-type bean lookup caching is working effectively by searching for a * bean of type B 10K times within a container having 1K additional beans of type A. @@ -3263,4 +3273,24 @@ public class DefaultListableBeanFactoryTests { } } + + enum NonPublicEnum { + + VALUE_1, VALUE_2; + } + + + static class NonPublicEnumHolder { + + final NonPublicEnum nonPublicEnum; + + public NonPublicEnumHolder(NonPublicEnum nonPublicEnum) { + this.nonPublicEnum = nonPublicEnum; + } + + public NonPublicEnum getNonPublicEnum() { + return nonPublicEnum; + } + } + }