Java 类org.springframework.test.context.ActiveProfiles 实例源码

项目:spring4-understanding    文件:DefaultActiveProfilesResolver.java   
/**
 * Resolve the <em>bean definition profiles</em> for the given {@linkplain
 * Class test class} based on profiles configured declaratively via
 * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
 * @param testClass the test class for which the profiles should be resolved;
 * never {@code null}
 * @return the list of bean definition profiles to use when loading the
 * {@code ApplicationContext}; never {@code null}
 */
@Override
public String[] resolve(Class<?> testClass) {
    Assert.notNull(testClass, "Class must not be null");

    final Set<String> activeProfiles = new LinkedHashSet<String>();

    Class<ActiveProfiles> annotationType = ActiveProfiles.class;
    AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);

    if (descriptor == null) {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format(
                "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
                annotationType.getName(), testClass.getName()));
        }
    }
    else {
        Class<?> declaringClass = descriptor.getDeclaringClass();
        ActiveProfiles annotation = descriptor.synthesizeAnnotation();

        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
                declaringClass.getName()));
        }

        for (String profile : annotation.profiles()) {
            if (StringUtils.hasText(profile)) {
                activeProfiles.add(profile.trim());
            }
        }
    }

    return StringUtils.toStringArray(activeProfiles);
}