Skip to content

Commit

Permalink
Merge branch '2.4.x' into 2.5.x
Browse files Browse the repository at this point in the history
Fixes gh-27072
  • Loading branch information
philwebb committed Jun 24, 2021
2 parents d079db1 + 5088927 commit 641dfbd
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -48,7 +48,7 @@ List<EnvironmentPostProcessor> getEnvironmentPostProcessors(DeferredLogFactory l
* @return an {@link EnvironmentPostProcessorsFactory} instance
*/
static EnvironmentPostProcessorsFactory fromSpringFactories(ClassLoader classLoader) {
return new ReflectionEnvironmentPostProcessorsFactory(
return new ReflectionEnvironmentPostProcessorsFactory(classLoader,
SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, classLoader));
}

Expand All @@ -69,7 +69,19 @@ static EnvironmentPostProcessorsFactory of(Class<?>... classes) {
* @return an {@link EnvironmentPostProcessorsFactory} instance
*/
static EnvironmentPostProcessorsFactory of(String... classNames) {
return new ReflectionEnvironmentPostProcessorsFactory(classNames);
return of(null, classNames);
}

/**
* Return a {@link EnvironmentPostProcessorsFactory} that reflectively creates post
* processors from the given class names.
* @param classLoader the source class loader
* @param classNames the post processor class names
* @return an {@link EnvironmentPostProcessorsFactory} instance
* @since 2.4.8
*/
static EnvironmentPostProcessorsFactory of(ClassLoader classLoader, String... classNames) {
return new ReflectionEnvironmentPostProcessorsFactory(classLoader, classNames);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.env;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -35,17 +36,24 @@
*/
class ReflectionEnvironmentPostProcessorsFactory implements EnvironmentPostProcessorsFactory {

private final List<Class<?>> classes;

private ClassLoader classLoader;

private final List<String> classNames;

ReflectionEnvironmentPostProcessorsFactory(Class<?>... classes) {
this(Arrays.stream(classes).map(Class::getName).toArray(String[]::new));
this.classes = new ArrayList<>(Arrays.asList(classes));
this.classNames = null;
}

ReflectionEnvironmentPostProcessorsFactory(String... classNames) {
this(Arrays.asList(classNames));
ReflectionEnvironmentPostProcessorsFactory(ClassLoader classLoader, String... classNames) {
this(classLoader, Arrays.asList(classNames));
}

ReflectionEnvironmentPostProcessorsFactory(List<String> classNames) {
ReflectionEnvironmentPostProcessorsFactory(ClassLoader classLoader, List<String> classNames) {
this.classes = null;
this.classLoader = classLoader;
this.classNames = classNames;
}

Expand All @@ -60,7 +68,8 @@ public List<EnvironmentPostProcessor> getEnvironmentPostProcessors(DeferredLogFa
parameters.add(BootstrapContext.class, bootstrapContext);
parameters.add(BootstrapRegistry.class, bootstrapContext);
});
return instantiator.instantiate(this.classNames);
return (this.classes != null) ? instantiator.instantiateTypes(this.classes)
: instantiator.instantiate(this.classLoader, this.classNames);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -17,7 +17,6 @@
package org.springframework.boot.util;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -27,6 +26,9 @@
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -85,22 +87,48 @@ public void add(Class<?> type, Function<Class<?>, Object> factory) {
* @return a list of instantiated instances
*/
public List<T> instantiate(Collection<String> names) {
List<T> instances = new ArrayList<>(names.size());
for (String name : names) {
instances.add(instantiate(name));
}
return instantiate((ClassLoader) null, names);
}

/**
* Instantiate the given set of class name, injecting constructor arguments as
* necessary.
* @param classLoader the source classloader
* @param names the class names to instantiate
* @return a list of instantiated instances
* @since 2.4.8
*/
public List<T> instantiate(ClassLoader classLoader, Collection<String> names) {
Assert.notNull(names, "Names must not be null");
return instantiate(names.stream().map((name) -> TypeSupplier.forName(classLoader, name)));
}

/**
* Instantiate the given set of classes, injecting constructor arguments as necessary.
* @param types the types to instantiate
* @return a list of instantiated instances
* @since 2.4.8
*/
public List<T> instantiateTypes(Collection<Class<?>> types) {
Assert.notNull(types, "Types must not be null");
return instantiate(types.stream().map((type) -> TypeSupplier.forType(type)));
}

public List<T> instantiate(Stream<TypeSupplier> typeSuppliers) {
List<T> instances = typeSuppliers.map(this::instantiate).collect(Collectors.toList());
AnnotationAwareOrderComparator.sort(instances);
return Collections.unmodifiableList(instances);
}

private T instantiate(String name) {
private T instantiate(TypeSupplier typeSupplier) {
try {
Class<?> type = ClassUtils.forName(name, null);
Class<?> type = typeSupplier.get();
Assert.isAssignable(this.type, type);
return instantiate(type);
}
catch (Throwable ex) {
throw new IllegalArgumentException("Unable to instantiate " + this.type.getName() + " [" + name + "]", ex);
throw new IllegalArgumentException(
"Unable to instantiate " + this.type.getName() + " [" + typeSupplier.getName() + "]", ex);
}
}

Expand Down Expand Up @@ -160,4 +188,47 @@ public interface AvailableParameters {

}

/**
* {@link Supplier} that provides a class type.
*/
private interface TypeSupplier {

String getName();

Class<?> get() throws ClassNotFoundException;

static TypeSupplier forName(ClassLoader classLoader, String name) {
return new TypeSupplier() {

@Override
public String getName() {
return name;
}

@Override
public Class<?> get() throws ClassNotFoundException {
return ClassUtils.forName(name, classLoader);
}

};
}

static TypeSupplier forType(Class<?> type) {
return new TypeSupplier() {

@Override
public String getName() {
return type.getName();
}

@Override
public Class<?> get() throws ClassNotFoundException {
return type;
}

};
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -24,6 +24,7 @@
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.OverridingClassLoader;
import org.springframework.core.env.ConfigurableEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -67,6 +68,25 @@ void ofClassNamesReturnsFactory() {
assertThat(processors.get(0)).isInstanceOf(TestEnvironmentPostProcessor.class);
}

@Test
void ofClassNamesWithClassLoaderReturnsFactory() {
OverridingClassLoader classLoader = new OverridingClassLoader(getClass().getClassLoader()) {

@Override
protected boolean isEligibleForOverriding(String className) {
return super.isEligibleForOverriding(className)
&& className.equals(TestEnvironmentPostProcessor.class.getName());
}

};
EnvironmentPostProcessorsFactory factory = EnvironmentPostProcessorsFactory.of(classLoader,
TestEnvironmentPostProcessor.class.getName());
List<EnvironmentPostProcessor> processors = factory.getEnvironmentPostProcessors(this.logFactory,
this.bootstrapContext);
assertThat(processors).hasSize(1);
assertThat(processors.get(0).getClass().getClassLoader()).isSameAs(classLoader);
}

static class TestEnvironmentPostProcessor implements EnvironmentPostProcessor {

TestEnvironmentPostProcessor(DeferredLogFactory logFactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.OverridingClassLoader;
import org.springframework.core.env.ConfigurableEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -53,49 +54,65 @@ void createWithClassesCreatesFactory() {

@Test
void createWithClassNamesArrayCreatesFactory() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
TestEnvironmentPostProcessor.class.getName());
assertThatFactory(factory).createsSinglePostProcessor(TestEnvironmentPostProcessor.class);
}

@Test
void createWithClassNamesListCreatesFactory() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
Arrays.asList(TestEnvironmentPostProcessor.class.getName()));
assertThatFactory(factory).createsSinglePostProcessor(TestEnvironmentPostProcessor.class);
}

@Test
void createWithClassNamesAndClassLoaderListCreatesFactory() {
OverridingClassLoader classLoader = new OverridingClassLoader(getClass().getClassLoader()) {

@Override
protected boolean isEligibleForOverriding(String className) {
return super.isEligibleForOverriding(className)
&& className.equals(TestEnvironmentPostProcessor.class.getName());
}

};
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(classLoader,
Arrays.asList(TestEnvironmentPostProcessor.class.getName()));
assertThatFactory(factory).createsSinglePostProcessorWithClassLoader(classLoader);
}

@Test
void getEnvironmentPostProcessorsWhenHasDefaultConstructorCreatesPostProcessors() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
TestEnvironmentPostProcessor.class.getName());
assertThatFactory(factory).createsSinglePostProcessor(TestEnvironmentPostProcessor.class);
}

@Test
void getEnvironmentPostProcessorsWhenHasLogFactoryConstructorCreatesPostProcessors() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
TestLogFactoryEnvironmentPostProcessor.class.getName());
assertThatFactory(factory).createsSinglePostProcessor(TestLogFactoryEnvironmentPostProcessor.class);
}

@Test
void getEnvironmentPostProcessorsWhenHasLogConstructorCreatesPostProcessors() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
TestLogEnvironmentPostProcessor.class.getName());
assertThatFactory(factory).createsSinglePostProcessor(TestLogEnvironmentPostProcessor.class);
}

@Test
void getEnvironmentPostProcessorsWhenHasBootstrapRegistryConstructorCreatesPostProcessors() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
TestBootstrapRegistryEnvironmentPostProcessor.class.getName());
assertThatFactory(factory).createsSinglePostProcessor(TestBootstrapRegistryEnvironmentPostProcessor.class);
}

@Test
void getEnvironmentPostProcessorsWhenHasNoSuitableConstructorThrowsException() {
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(
ReflectionEnvironmentPostProcessorsFactory factory = new ReflectionEnvironmentPostProcessorsFactory(null,
BadEnvironmentPostProcessor.class.getName());
assertThatIllegalArgumentException()
.isThrownBy(() -> factory.getEnvironmentPostProcessors(this.logFactory, this.bootstrapContext))
Expand All @@ -115,11 +132,21 @@ class EnvironmentPostProcessorsFactoryAssert {
}

void createsSinglePostProcessor(Class<?> expectedType) {
EnvironmentPostProcessor processor = getSingleProcessor();
assertThat(processor).isInstanceOf(expectedType);
}

void createsSinglePostProcessorWithClassLoader(OverridingClassLoader classLoader) {
EnvironmentPostProcessor processor = getSingleProcessor();
assertThat(processor.getClass().getClassLoader()).isSameAs(classLoader);
}

private EnvironmentPostProcessor getSingleProcessor() {
List<EnvironmentPostProcessor> processors = this.factory.getEnvironmentPostProcessors(
ReflectionEnvironmentPostProcessorsFactoryTests.this.logFactory,
ReflectionEnvironmentPostProcessorsFactoryTests.this.bootstrapContext);
assertThat(processors).hasSize(1);
assertThat(processors.get(0)).isInstanceOf(expectedType);
return processors.get(0);
}

}
Expand Down

0 comments on commit 641dfbd

Please sign in to comment.