diff --git a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java index d47724994c7..8021208bafb 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -17,6 +17,7 @@ package org.springframework.core.io.support; import java.io.IOException; +import java.lang.reflect.Constructor; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -31,6 +32,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.io.UrlResource; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; /** @@ -132,7 +134,9 @@ public abstract class SpringFactoriesLoader { throw new IllegalArgumentException( "Class [" + instanceClassName + "] is not assignable to [" + factoryClass.getName() + "]"); } - return (T) instanceClass.newInstance(); + Constructor constructor = instanceClass.getDeclaredConstructor(); + ReflectionUtils.makeAccessible(constructor); + return (T) constructor.newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot instantiate factory class: " + factoryClass.getName(), ex); diff --git a/spring-core/src/test/java/org/springframework/core/io/support/DummyPackagePrivateFactory.java b/spring-core/src/test/java/org/springframework/core/io/support/DummyPackagePrivateFactory.java new file mode 100644 index 00000000000..4c83bfabe7b --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/io/support/DummyPackagePrivateFactory.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.io.support; + +/** + * Used by {@link SpringFactoriesLoaderTests} + + * @author Phillip Webb + */ +class DummyPackagePrivateFactory { + +} diff --git a/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java new file mode 100644 index 00000000000..4de87d2c727 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2002-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.io.support; + +import java.lang.reflect.Modifier; +import java.util.List; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for {@link SpringFactoriesLoader}. + * + * @author Arjen Poutsma + * @author Phillip Webb + */ +public class SpringFactoriesLoaderTests { + + @Test + public void loadFactoriesInCorrectOrder() { + List factories = SpringFactoriesLoader + .loadFactories(DummyFactory.class, null); + assertEquals(2, factories.size()); + assertTrue(factories.get(0) instanceof MyDummyFactory1); + assertTrue(factories.get(1) instanceof MyDummyFactory2); + } + + @Test(expected = IllegalArgumentException.class) + public void loadInvalid() { + SpringFactoriesLoader.loadFactories(String.class, null); + } + + @Test + public void loadPackagePrivateFactory() throws Exception { + List factories = SpringFactoriesLoader + .loadFactories(DummyPackagePrivateFactory.class, null); + assertEquals(1, factories.size()); + assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0); + } + +} diff --git a/spring-core/src/test/resources/META-INF/spring.factories b/spring-core/src/test/resources/META-INF/spring.factories new file mode 100644 index 00000000000..ab64ffe1b67 --- /dev/null +++ b/spring-core/src/test/resources/META-INF/spring.factories @@ -0,0 +1,9 @@ +org.springframework.core.io.support.DummyFactory=\ +org.springframework.core.io.support.MyDummyFactory2,\ +org.springframework.core.io.support.MyDummyFactory1 + +java.lang.String=\ +org.springframework.core.io.support.MyDummyFactory1 + +org.springframework.core.io.support.DummyPackagePrivateFactory=\ +org.springframework.core.io.support.DummyPackagePrivateFactory diff --git a/spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties b/spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties deleted file mode 100644 index d3e700fe87e..00000000000 --- a/spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.core.io.support.DummyFactory=org.springframework.core.io.support.MyDummyFactory2,org.springframework.core.io.support.MyDummyFactory1 -java.lang.String=org.springframework.core.io.support.MyDummyFactory1 \ No newline at end of file