/* * Copyright 2002-2015 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.test.context.support; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.SmartContextLoader; import org.springframework.util.Assert; /** * Utility methods for {@link SmartContextLoader SmartContextLoaders} that deal * with annotated classes (e.g., {@link Configuration @Configuration} classes). * * @author Sam Brannen * @since 3.2 */ public abstract class AnnotationConfigContextLoaderUtils { private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoaderUtils.class); /** * Detect the default configuration classes for the supplied test class. *
The returned class array will contain all static nested classes of * the supplied class that meet the requirements for {@code @Configuration} * class implementations as specified in the documentation for * {@link Configuration @Configuration}. *
The implementation of this method adheres to the contract defined in the
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
* SPI. Specifically, this method uses introspection to detect default
* configuration classes that comply with the constraints required of
* {@code @Configuration} class implementations. If a potential candidate
* configuration class does not meet these requirements, this method will log a
* debug message, and the potential candidate class will be ignored.
* @param declaringClass the test class that declared {@code @ContextConfiguration}
* @return an array of default configuration classes, potentially empty but
* never {@code null}
*/
public static Class>[] detectDefaultConfigurationClasses(Class> declaringClass) {
Assert.notNull(declaringClass, "Declaring class must not be null");
List Specifically, such candidates:
*
*
* @param clazz the class to check
* @return {@code true} if the supplied class meets the candidate criteria
*/
private static boolean isDefaultConfigurationClassCandidate(Class> clazz) {
return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class));
}
private static boolean isStaticNonPrivateAndNonFinal(Class> clazz) {
Assert.notNull(clazz, "Class must not be null");
int modifiers = clazz.getModifiers();
return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
}
}