Skip to content

Commit d8a0559

Browse files
committed
Allow package private classes in spring.factories
Update SpringFactoriesLoader so that package private classes can be used. Issue: SPR-13969
1 parent 2ca1102 commit d8a0559

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.io.support;
1818

1919
import java.io.IOException;
20+
import java.lang.reflect.Constructor;
2021
import java.net.URL;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
@@ -31,6 +32,7 @@
3132
import org.springframework.core.io.UrlResource;
3233
import org.springframework.util.Assert;
3334
import org.springframework.util.ClassUtils;
35+
import org.springframework.util.ReflectionUtils;
3436
import org.springframework.util.StringUtils;
3537

3638
/**
@@ -132,7 +134,9 @@ private static <T> T instantiateFactory(String instanceClassName, Class<T> facto
132134
throw new IllegalArgumentException(
133135
"Class [" + instanceClassName + "] is not assignable to [" + factoryClass.getName() + "]");
134136
}
135-
return (T) instanceClass.newInstance();
137+
Constructor<?> constructor = instanceClass.getDeclaredConstructor();
138+
ReflectionUtils.makeAccessible(constructor);
139+
return (T) constructor.newInstance();
136140
}
137141
catch (Throwable ex) {
138142
throw new IllegalArgumentException("Cannot instantiate factory class: " + factoryClass.getName(), ex);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io.support;
18+
19+
/**
20+
* Used by {@link SpringFactoriesLoaderTests}
21+
22+
* @author Phillip Webb
23+
*/
24+
class DummyPackagePrivateFactory {
25+
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io.support;
18+
19+
import java.lang.reflect.Modifier;
20+
import java.util.List;
21+
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
* Tests for {@link SpringFactoriesLoader}.
28+
*
29+
* @author Arjen Poutsma
30+
* @author Phillip Webb
31+
*/
32+
public class SpringFactoriesLoaderTests {
33+
34+
@Test
35+
public void loadFactoriesInCorrectOrder() {
36+
List<DummyFactory> factories = SpringFactoriesLoader
37+
.loadFactories(DummyFactory.class, null);
38+
assertEquals(2, factories.size());
39+
assertTrue(factories.get(0) instanceof MyDummyFactory1);
40+
assertTrue(factories.get(1) instanceof MyDummyFactory2);
41+
}
42+
43+
@Test(expected = IllegalArgumentException.class)
44+
public void loadInvalid() {
45+
SpringFactoriesLoader.loadFactories(String.class, null);
46+
}
47+
48+
@Test
49+
public void loadPackagePrivateFactory() throws Exception {
50+
List<DummyPackagePrivateFactory> factories = SpringFactoriesLoader
51+
.loadFactories(DummyPackagePrivateFactory.class, null);
52+
assertEquals(1, factories.size());
53+
assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0);
54+
}
55+
56+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
org.springframework.core.io.support.DummyFactory=\
2+
org.springframework.core.io.support.MyDummyFactory2,\
3+
org.springframework.core.io.support.MyDummyFactory1
4+
5+
java.lang.String=\
6+
org.springframework.core.io.support.MyDummyFactory1
7+
8+
org.springframework.core.io.support.DummyPackagePrivateFactory=\
9+
org.springframework.core.io.support.DummyPackagePrivateFactory

spring-core/src/test/resources/org/springframework/core/io/support/springFactoriesLoaderTests.properties

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)