Commit 167e31d5 authored by Phillip Webb's avatar Phillip Webb

Provide a simple Instantiator utility class

Make `Instantiator` a public utility class so that it can be used
by other parts of the codebase.

Closes gh-23029
parent f7ef3445
...@@ -24,6 +24,7 @@ import java.util.List; ...@@ -24,6 +24,7 @@ import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.boot.util.Instantiator;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
......
...@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log; ...@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log;
import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.boot.util.Instantiator;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.context.config; package org.springframework.boot.util;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -38,8 +38,9 @@ import org.springframework.util.ReflectionUtils; ...@@ -38,8 +38,9 @@ import org.springframework.util.ReflectionUtils;
* *
* @param <T> the type to instantiate * @param <T> the type to instantiate
* @author Phillip Webb * @author Phillip Webb
* @since 2.4.0
*/ */
class Instantiator<T> { public class Instantiator<T> {
private static final Comparator<Constructor<?>> CONSTRUCTOR_COMPARATOR = Comparator private static final Comparator<Constructor<?>> CONSTRUCTOR_COMPARATOR = Comparator
.<Constructor<?>>comparingInt(Constructor::getParameterCount).reversed(); .<Constructor<?>>comparingInt(Constructor::getParameterCount).reversed();
...@@ -53,7 +54,7 @@ class Instantiator<T> { ...@@ -53,7 +54,7 @@ class Instantiator<T> {
* @param type the type to instantiate * @param type the type to instantiate
* @param availableParameters consumer used to register available parameters * @param availableParameters consumer used to register available parameters
*/ */
Instantiator(Class<?> type, Consumer<AvailableParameters> availableParameters) { public Instantiator(Class<?> type, Consumer<AvailableParameters> availableParameters) {
this.type = type; this.type = type;
this.availableParameters = getAvailableParameters(availableParameters); this.availableParameters = getAvailableParameters(availableParameters);
} }
...@@ -83,7 +84,7 @@ class Instantiator<T> { ...@@ -83,7 +84,7 @@ class Instantiator<T> {
* @param names the class names to instantiate * @param names the class names to instantiate
* @return a list of instantiated instances * @return a list of instantiated instances
*/ */
List<T> instantiate(Collection<String> names) { public List<T> instantiate(Collection<String> names) {
List<T> instances = new ArrayList<>(names.size()); List<T> instances = new ArrayList<>(names.size());
for (String name : names) { for (String name : names) {
instances.add(instantiate(name)); instances.add(instantiate(name));
...@@ -141,7 +142,7 @@ class Instantiator<T> { ...@@ -141,7 +142,7 @@ class Instantiator<T> {
/** /**
* Callback used to register available parameters. * Callback used to register available parameters.
*/ */
interface AvailableParameters { public interface AvailableParameters {
/** /**
* Add a parameter with an instance value. * Add a parameter with an instance value.
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.context.config; package org.springframework.boot.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment