-
Notifications
You must be signed in to change notification settings - Fork 40.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AutoConfigurationImportFilter base classes
Add `FilteringSpringBootCondition` base class and refactor the existing `OnClassCondition` to use it. Also update the `match` method so that the `autoConfigurationClasses` array may include `null` elements. See gh-13328
- Loading branch information
Showing
5 changed files
with
184 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...n/java/org/springframework/boot/autoconfigure/condition/FilteringSpringBootCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright 2012-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. | ||
* 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.boot.autoconfigure.condition; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanClassLoaderAware; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
/** | ||
* Abstract base class for a {@link SpringBootCondition} that also implements | ||
* {@link AutoConfigurationImportFilter}. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
abstract class FilteringSpringBootCondition extends SpringBootCondition | ||
implements AutoConfigurationImportFilter, BeanFactoryAware, BeanClassLoaderAware { | ||
|
||
private BeanFactory beanFactory; | ||
|
||
private ClassLoader beanClassLoader; | ||
|
||
@Override | ||
public boolean[] match(String[] autoConfigurationClasses, | ||
AutoConfigurationMetadata autoConfigurationMetadata) { | ||
ConditionEvaluationReport report = ConditionEvaluationReport | ||
.find(this.beanFactory); | ||
ConditionOutcome[] outcomes = getOutcomes(autoConfigurationClasses, | ||
autoConfigurationMetadata); | ||
boolean[] match = new boolean[outcomes.length]; | ||
for (int i = 0; i < outcomes.length; i++) { | ||
match[i] = (outcomes[i] == null || outcomes[i].isMatch()); | ||
if (!match[i] && outcomes[i] != null) { | ||
logOutcome(autoConfigurationClasses[i], outcomes[i]); | ||
if (report != null) { | ||
report.recordConditionEvaluation(autoConfigurationClasses[i], this, | ||
outcomes[i]); | ||
} | ||
} | ||
} | ||
return match; | ||
} | ||
|
||
protected abstract ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses, | ||
AutoConfigurationMetadata autoConfigurationMetadata); | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = beanFactory; | ||
} | ||
|
||
protected final BeanFactory getBeanFactory() { | ||
return this.beanFactory; | ||
} | ||
|
||
protected final ClassLoader getBeanClassLoader() { | ||
return this.beanClassLoader; | ||
} | ||
|
||
@Override | ||
public void setBeanClassLoader(ClassLoader classLoader) { | ||
this.beanClassLoader = classLoader; | ||
} | ||
|
||
protected List<String> filter(Collection<String> classNames, | ||
ClassNameFilter classNameFilter, ClassLoader classLoader) { | ||
if (CollectionUtils.isEmpty(classNames)) { | ||
return Collections.emptyList(); | ||
} | ||
List<String> matches = new ArrayList<>(classNames.size()); | ||
for (String candidate : classNames) { | ||
if (classNameFilter.matches(candidate, classLoader)) { | ||
matches.add(candidate); | ||
} | ||
} | ||
return matches; | ||
} | ||
|
||
protected enum ClassNameFilter { | ||
|
||
PRESENT { | ||
|
||
@Override | ||
public boolean matches(String className, ClassLoader classLoader) { | ||
return isPresent(className, classLoader); | ||
} | ||
|
||
}, | ||
|
||
MISSING { | ||
|
||
@Override | ||
public boolean matches(String className, ClassLoader classLoader) { | ||
return !isPresent(className, classLoader); | ||
} | ||
|
||
}; | ||
|
||
public abstract boolean matches(String className, ClassLoader classLoader); | ||
|
||
public static boolean isPresent(String className, ClassLoader classLoader) { | ||
if (classLoader == null) { | ||
classLoader = ClassUtils.getDefaultClassLoader(); | ||
} | ||
try { | ||
forName(className, classLoader); | ||
return true; | ||
} | ||
catch (Throwable ex) { | ||
return false; | ||
} | ||
} | ||
|
||
private static Class<?> forName(String className, ClassLoader classLoader) | ||
throws ClassNotFoundException { | ||
if (classLoader != null) { | ||
return classLoader.loadClass(className); | ||
} | ||
return Class.forName(className); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.