Java Code Examples for org.springframework.util.ClassUtils#toClassArray()

The following examples show how to use org.springframework.util.ClassUtils#toClassArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringValidatorAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Turn the specified validation hints into JSR-303 validation groups.
 * @since 5.1
 */
private Class<?>[] asValidationGroups(Object... validationHints) {
	Set<Class<?>> groups = new LinkedHashSet<>(4);
	for (Object hint : validationHints) {
		if (hint instanceof Class) {
			groups.add((Class<?>) hint);
		}
	}
	return ClassUtils.toClassArray(groups);
}
 
Example 2
Source File: AnnotationConfigContextLoaderUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Detect the default configuration classes for the supplied test class.
 * <p>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}.
 * <p>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<Class<?>> configClasses = new ArrayList<>();

	for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
		if (isDefaultConfigurationClassCandidate(candidate)) {
			configClasses.add(candidate);
		}
		else {
			if (logger.isDebugEnabled()) {
				logger.debug(String.format(
					"Ignoring class [%s]; it must be static, non-private, non-final, and annotated " +
							"with @Configuration to be considered a default configuration class.",
					candidate.getName()));
			}
		}
	}

	if (configClasses.isEmpty()) {
		if (logger.isInfoEnabled()) {
			logger.info(String.format("Could not detect default configuration classes for test class [%s]: " +
					"%s does not declare any static, non-private, non-final, nested classes " +
					"annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
		}
	}

	return ClassUtils.toClassArray(configClasses);
}
 
Example 3
Source File: SpringValidatorAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Turn the specified validation hints into JSR-303 validation groups.
 * @since 5.1
 */
private Class<?>[] asValidationGroups(Object... validationHints) {
	Set<Class<?>> groups = new LinkedHashSet<>(4);
	for (Object hint : validationHints) {
		if (hint instanceof Class) {
			groups.add((Class<?>) hint);
		}
	}
	return ClassUtils.toClassArray(groups);
}
 
Example 4
Source File: AnnotationConfigContextLoaderUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Detect the default configuration classes for the supplied test class.
 * <p>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}.
 * <p>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<Class<?>> configClasses = new ArrayList<>();

	for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
		if (isDefaultConfigurationClassCandidate(candidate)) {
			configClasses.add(candidate);
		}
		else {
			if (logger.isDebugEnabled()) {
				logger.debug(String.format(
					"Ignoring class [%s]; it must be static, non-private, non-final, and annotated " +
							"with @Configuration to be considered a default configuration class.",
					candidate.getName()));
			}
		}
	}

	if (configClasses.isEmpty()) {
		if (logger.isInfoEnabled()) {
			logger.info(String.format("Could not detect default configuration classes for test class [%s]: " +
					"%s does not declare any static, non-private, non-final, nested classes " +
					"annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
		}
	}

	return ClassUtils.toClassArray(configClasses);
}
 
Example 5
Source File: IntroductionInfoSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getInterfaces() {
	return ClassUtils.toClassArray(this.publishedInterfaces);
}
 
Example 6
Source File: DefaultIntroductionAdvisor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getInterfaces() {
	return ClassUtils.toClassArray(this.interfaces);
}
 
Example 7
Source File: AdvisedSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getProxiedInterfaces() {
	return ClassUtils.toClassArray(this.interfaces);
}
 
Example 8
Source File: AbstractTestContextBootstrapper.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Build the {@link MergedContextConfiguration merged context configuration}
 * for the supplied {@link Class testClass}, context configuration attributes,
 * and parent context configuration.
 * @param testClass the test class for which the {@code MergedContextConfiguration}
 * should be built (must not be {@code null})
 * @param configAttributesList the list of context configuration attributes for the
 * specified test class, ordered <em>bottom-up</em> (i.e., as if we were
 * traversing up the class hierarchy); never {@code null} or empty
 * @param parentConfig the merged context configuration for the parent application
 * context in a context hierarchy, or {@code null} if there is no parent
 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
 * be passed to the {@code MergedContextConfiguration} constructor
 * @param requireLocationsClassesOrInitializers whether locations, classes, or
 * initializers are required; typically {@code true} but may be set to {@code false}
 * if the configured loader supports empty configuration
 * @return the merged context configuration
 * @see #resolveContextLoader
 * @see ContextLoaderUtils#resolveContextConfigurationAttributes
 * @see SmartContextLoader#processContextConfiguration
 * @see ContextLoader#processLocations
 * @see ActiveProfilesUtils#resolveActiveProfiles
 * @see ApplicationContextInitializerUtils#resolveInitializerClasses
 * @see MergedContextConfiguration
 */
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributesList, @Nullable MergedContextConfiguration parentConfig,
		CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate,
		boolean requireLocationsClassesOrInitializers) {

	Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");

	ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
	List<String> locations = new ArrayList<>();
	List<Class<?>> classes = new ArrayList<>();
	List<Class<?>> initializers = new ArrayList<>();

	for (ContextConfigurationAttributes configAttributes : configAttributesList) {
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Processing locations and classes for context configuration attributes %s",
					configAttributes));
		}
		if (contextLoader instanceof SmartContextLoader) {
			SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
			smartContextLoader.processContextConfiguration(configAttributes);
			locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
			classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
		}
		else {
			String[] processedLocations = contextLoader.processLocations(
					configAttributes.getDeclaringClass(), configAttributes.getLocations());
			locations.addAll(0, Arrays.asList(processedLocations));
			// Legacy ContextLoaders don't know how to process classes
		}
		initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
		if (!configAttributes.isInheritLocations()) {
			break;
		}
	}

	Set<ContextCustomizer> contextCustomizers = getContextCustomizers(testClass,
			Collections.unmodifiableList(configAttributesList));

	Assert.state(!(requireLocationsClassesOrInitializers &&
			areAllEmpty(locations, classes, initializers, contextCustomizers)), () -> String.format(
			"%s was unable to detect defaults, and no ApplicationContextInitializers " +
			"or ContextCustomizers were declared for context configuration attributes %s",
			contextLoader.getClass().getSimpleName(), configAttributesList));

	MergedTestPropertySources mergedTestPropertySources =
			TestPropertySourceUtils.buildMergedTestPropertySources(testClass);
	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass,
			StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes),
			ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList),
			ActiveProfilesUtils.resolveActiveProfiles(testClass),
			mergedTestPropertySources.getLocations(),
			mergedTestPropertySources.getProperties(),
			contextCustomizers, contextLoader, cacheAwareContextLoaderDelegate, parentConfig);

	return processMergedContextConfiguration(mergedConfig);
}
 
Example 9
Source File: IntroductionInfoSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getInterfaces() {
	return ClassUtils.toClassArray(this.publishedInterfaces);
}
 
Example 10
Source File: DefaultIntroductionAdvisor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getInterfaces() {
	return ClassUtils.toClassArray(this.interfaces);
}
 
Example 11
Source File: AdvisedSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Class<?>[] getProxiedInterfaces() {
	return ClassUtils.toClassArray(this.interfaces);
}
 
Example 12
Source File: AbstractTestContextBootstrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Build the {@link MergedContextConfiguration merged context configuration}
 * for the supplied {@link Class testClass}, context configuration attributes,
 * and parent context configuration.
 * @param testClass the test class for which the {@code MergedContextConfiguration}
 * should be built (must not be {@code null})
 * @param configAttributesList the list of context configuration attributes for the
 * specified test class, ordered <em>bottom-up</em> (i.e., as if we were
 * traversing up the class hierarchy); never {@code null} or empty
 * @param parentConfig the merged context configuration for the parent application
 * context in a context hierarchy, or {@code null} if there is no parent
 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
 * be passed to the {@code MergedContextConfiguration} constructor
 * @param requireLocationsClassesOrInitializers whether locations, classes, or
 * initializers are required; typically {@code true} but may be set to {@code false}
 * if the configured loader supports empty configuration
 * @return the merged context configuration
 * @see #resolveContextLoader
 * @see ContextLoaderUtils#resolveContextConfigurationAttributes
 * @see SmartContextLoader#processContextConfiguration
 * @see ContextLoader#processLocations
 * @see ActiveProfilesUtils#resolveActiveProfiles
 * @see ApplicationContextInitializerUtils#resolveInitializerClasses
 * @see MergedContextConfiguration
 */
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributesList, @Nullable MergedContextConfiguration parentConfig,
		CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate,
		boolean requireLocationsClassesOrInitializers) {

	Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");

	ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
	List<String> locations = new ArrayList<>();
	List<Class<?>> classes = new ArrayList<>();
	List<Class<?>> initializers = new ArrayList<>();

	for (ContextConfigurationAttributes configAttributes : configAttributesList) {
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Processing locations and classes for context configuration attributes %s",
					configAttributes));
		}
		if (contextLoader instanceof SmartContextLoader) {
			SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
			smartContextLoader.processContextConfiguration(configAttributes);
			locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
			classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
		}
		else {
			String[] processedLocations = contextLoader.processLocations(
					configAttributes.getDeclaringClass(), configAttributes.getLocations());
			locations.addAll(0, Arrays.asList(processedLocations));
			// Legacy ContextLoaders don't know how to process classes
		}
		initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
		if (!configAttributes.isInheritLocations()) {
			break;
		}
	}

	Set<ContextCustomizer> contextCustomizers = getContextCustomizers(testClass,
			Collections.unmodifiableList(configAttributesList));

	Assert.state(!(requireLocationsClassesOrInitializers &&
			areAllEmpty(locations, classes, initializers, contextCustomizers)), () -> String.format(
			"%s was unable to detect defaults, and no ApplicationContextInitializers " +
			"or ContextCustomizers were declared for context configuration attributes %s",
			contextLoader.getClass().getSimpleName(), configAttributesList));

	MergedTestPropertySources mergedTestPropertySources =
			TestPropertySourceUtils.buildMergedTestPropertySources(testClass);
	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass,
			StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes),
			ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList),
			ActiveProfilesUtils.resolveActiveProfiles(testClass),
			mergedTestPropertySources.getLocations(),
			mergedTestPropertySources.getProperties(),
			contextCustomizers, contextLoader, cacheAwareContextLoaderDelegate, parentConfig);

	return processMergedContextConfiguration(mergedConfig);
}
 
Example 13
Source File: AbstractTestContextBootstrapper.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Build the {@link MergedContextConfiguration merged context configuration}
 * for the supplied {@link Class testClass}, context configuration attributes,
 * and parent context configuration.
 * @param testClass the test class for which the {@code MergedContextConfiguration}
 * should be built (must not be {@code null})
 * @param configAttributesList the list of context configuration attributes for the
 * specified test class, ordered <em>bottom-up</em> (i.e., as if we were
 * traversing up the class hierarchy); never {@code null} or empty
 * @param parentConfig the merged context configuration for the parent application
 * context in a context hierarchy, or {@code null} if there is no parent
 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
 * be passed to the {@code MergedContextConfiguration} constructor
 * @return the merged context configuration
 * @see #resolveContextLoader
 * @see ContextLoaderUtils#resolveContextConfigurationAttributes
 * @see SmartContextLoader#processContextConfiguration
 * @see ContextLoader#processLocations
 * @see ActiveProfilesUtils#resolveActiveProfiles
 * @see ApplicationContextInitializerUtils#resolveInitializerClasses
 * @see MergedContextConfiguration
 */
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributesList, MergedContextConfiguration parentConfig,
		CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {

	ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
	List<String> locationsList = new ArrayList<String>();
	List<Class<?>> classesList = new ArrayList<Class<?>>();

	for (ContextConfigurationAttributes configAttributes : configAttributesList) {
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Processing locations and classes for context configuration attributes %s",
				configAttributes));
		}
		if (contextLoader instanceof SmartContextLoader) {
			SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
			smartContextLoader.processContextConfiguration(configAttributes);
			locationsList.addAll(0, Arrays.asList(configAttributes.getLocations()));
			classesList.addAll(0, Arrays.asList(configAttributes.getClasses()));
		}
		else {
			String[] processedLocations = contextLoader.processLocations(configAttributes.getDeclaringClass(),
				configAttributes.getLocations());
			locationsList.addAll(0, Arrays.asList(processedLocations));
			// Legacy ContextLoaders don't know how to process classes
		}
		if (!configAttributes.isInheritLocations()) {
			break;
		}
	}

	String[] locations = StringUtils.toStringArray(locationsList);
	Class<?>[] classes = ClassUtils.toClassArray(classesList);
	Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializerClasses = //
	ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList);
	String[] activeProfiles = ActiveProfilesUtils.resolveActiveProfiles(testClass);
	MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils.buildMergedTestPropertySources(testClass);

	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass, locations, classes,
		initializerClasses, activeProfiles, mergedTestPropertySources.getLocations(),
		mergedTestPropertySources.getProperties(), contextLoader, cacheAwareContextLoaderDelegate, parentConfig);

	return processMergedContextConfiguration(mergedConfig);
}