@ -18,6 +18,7 @@ package org.springframework.boot.test.autoconfigure.filter;
import java.io.IOException ;
import java.lang.reflect.Constructor ;
import java.util.Collections ;
import java.util.LinkedHashSet ;
import java.util.Set ;
@ -40,48 +41,65 @@ class TypeExcludeFiltersContextCustomizer implements ContextCustomizer {
private static final String EXCLUDE_FILTER_BEAN_NAME = TypeExcludeFilters . class
. getName ( ) ;
private final Class < ? > testClass ;
private final Set < Class < ? extends TypeExcludeFilter > > filterClasses ;
private final Set < TypeExcludeFilter > filters ;
TypeExcludeFiltersContextCustomizer ( Class < ? > testClass ,
Set < Class < ? extends TypeExcludeFilter > > filterClasses ) {
this . testClass = testClass ;
this . filterClasses = filterClasses ;
this . filters = instantiateTypeExcludeFilters ( testClass , filterClasses ) ;
}
private Set < TypeExcludeFilter > instantiateTypeExcludeFilters ( Class < ? > testClass ,
Set < Class < ? extends TypeExcludeFilter > > filterClasses ) {
Set < TypeExcludeFilter > filters = new LinkedHashSet < TypeExcludeFilter > ( ) ;
for ( Class < ? extends TypeExcludeFilter > filterClass : filterClasses ) {
filters . add ( instantiateTypeExcludeFilter ( testClass , filterClass ) ) ;
}
return Collections . unmodifiableSet ( filters ) ;
}
private TypeExcludeFilter instantiateTypeExcludeFilter ( Class < ? > testClass ,
Class < ? > filterClass ) {
try {
Constructor < ? > constructor = getTypeExcludeFilterConstructor ( filterClass ) ;
ReflectionUtils . makeAccessible ( constructor ) ;
if ( constructor . getParameterTypes ( ) . length = = 1 ) {
return ( TypeExcludeFilter ) constructor . newInstance ( testClass ) ;
}
return ( TypeExcludeFilter ) constructor . newInstance ( ) ;
}
catch ( Exception ex ) {
throw new IllegalStateException ( "Unable to create filter for " + filterClass ,
ex ) ;
}
}
@Override
public int hashCode ( ) {
return this . filterClasses . hashCode ( ) ;
return this . filter s. hashCode ( ) ;
}
@Override
public boolean equals ( Object obj ) {
return ( obj ! = null & & getClass ( ) . equals ( obj . getClass ( ) ) & & this . filterClasses
. equals ( ( ( TypeExcludeFiltersContextCustomizer ) obj ) . filterClasses ) ) ;
return ( obj ! = null & & getClass ( ) . equals ( obj . getClass ( ) ) & & this . filter s
. equals ( ( ( TypeExcludeFiltersContextCustomizer ) obj ) . filter s) ) ;
}
@Override
public void customizeContext ( ConfigurableApplicationContext context ,
MergedContextConfiguration mergedContextConfiguration ) {
if ( ! this . filterClasses . isEmpty ( ) ) {
if ( ! this . filter s. isEmpty ( ) ) {
context . getBeanFactory ( ) . registerSingleton ( EXCLUDE_FILTER_BEAN_NAME ,
createDelegatingTypeExcludeFilter ( ) ) ;
}
}
private TypeExcludeFilter createDelegatingTypeExcludeFilter ( ) {
final Set < TypeExcludeFilter > filters = new LinkedHashSet < TypeExcludeFilter > (
this . filterClasses . size ( ) ) ;
for ( Class < ? extends TypeExcludeFilter > filterClass : this . filterClasses ) {
filters . add ( createTypeExcludeFilter ( filterClass ) ) ;
}
return new TypeExcludeFilter ( ) {
@Override
public boolean match ( MetadataReader metadataReader ,
MetadataReaderFactory metadataReaderFactory ) throws IOException {
for ( TypeExcludeFilter filter : filters) {
for ( TypeExcludeFilter filter : TypeExcludeFiltersContextCustomizer. this . filters) {
if ( filter . match ( metadataReader , metadataReaderFactory ) ) {
return true ;
}
@ -92,20 +110,6 @@ class TypeExcludeFiltersContextCustomizer implements ContextCustomizer {
} ;
}
private TypeExcludeFilter createTypeExcludeFilter ( Class < ? > type ) {
try {
Constructor < ? > constructor = getTypeExcludeFilterConstructor ( type ) ;
ReflectionUtils . makeAccessible ( constructor ) ;
if ( constructor . getParameterTypes ( ) . length = = 1 ) {
return ( TypeExcludeFilter ) constructor . newInstance ( this . testClass ) ;
}
return ( TypeExcludeFilter ) constructor . newInstance ( ) ;
}
catch ( Exception ex ) {
throw new IllegalStateException ( "Unable to create filter for " + type , ex ) ;
}
}
private Constructor < ? > getTypeExcludeFilterConstructor ( Class < ? > type )
throws NoSuchMethodException {
try {