Java Code Examples for org.springframework.core.annotation.AnnotationAttributes#getClassArray()

The following examples show how to use org.springframework.core.annotation.AnnotationAttributes#getClassArray() . 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: WebAdminComponentScanRegistrar.java    From wallride with Apache License 2.0 6 votes vote down vote up
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
	AnnotationAttributes attributes = AnnotationAttributes
			.fromMap(metadata.getAnnotationAttributes(WebAdminComponentScan.class.getName()));
	String[] value = attributes.getStringArray("value");
	String[] basePackages = attributes.getStringArray("basePackages");
	Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
	if (!ObjectUtils.isEmpty(value)) {
		Assert.state(ObjectUtils.isEmpty(basePackages),
				"@WebAdminComponentScan basePackages and value attributes are mutually exclusive");
	}
	Set<String> packagesToScan = new LinkedHashSet<String>();
	packagesToScan.addAll(Arrays.asList(value));
	packagesToScan.addAll(Arrays.asList(basePackages));
	for (Class<?> basePackageClass : basePackageClasses) {
		packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
	}
	if (packagesToScan.isEmpty()) {
		return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
	}
	return packagesToScan;
}
 
Example 2
Source File: StoreUtils.java    From spring-content with Apache License 2.0 6 votes vote down vote up
public static String[] getBasePackages(AnnotationAttributes attributes, String[] defaultPackages) {

		String[] value = attributes.getStringArray("value");
		String[] basePackages = attributes.getStringArray(BASE_PACKAGES);
		Class<?>[] basePackageClasses = attributes.getClassArray(BASE_PACKAGE_CLASSES);

		// Default configuration - return package of annotated class
		if (value.length == 0 && basePackages.length == 0
				&& basePackageClasses.length == 0) {
			return defaultPackages;
		}

		Set<String> packages = new HashSet<String>();
		packages.addAll(Arrays.asList(value));
		packages.addAll(Arrays.asList(basePackages));

		for (Class<?> typeName : basePackageClasses) {
			packages.add(ClassUtils.getPackageName(typeName));
		}

		return packages.toArray(new String[] {});
	}
 
Example 3
Source File: WebGuestComponentScanRegistrar.java    From wallride with Apache License 2.0 6 votes vote down vote up
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
	AnnotationAttributes attributes = AnnotationAttributes
			.fromMap(metadata.getAnnotationAttributes(WebGuestComponentScan.class.getName()));
	String[] value = attributes.getStringArray("value");
	String[] basePackages = attributes.getStringArray("basePackages");
	Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
	if (!ObjectUtils.isEmpty(value)) {
		Assert.state(ObjectUtils.isEmpty(basePackages),
				"@WebGuestComponentScan basePackages and value attributes are mutually exclusive");
	}
	Set<String> packagesToScan = new LinkedHashSet<String>();
	packagesToScan.addAll(Arrays.asList(value));
	packagesToScan.addAll(Arrays.asList(basePackages));
	for (Class<?> basePackageClass : basePackageClasses) {
		packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
	}
	if (packagesToScan.isEmpty()) {
		return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
	}
	return packagesToScan;
}
 
Example 4
Source File: ConfigurationScanRegistrar.java    From conf4j with MIT License 6 votes vote down vote up
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
    AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(ConfigurationScan.class.getName()));
    String[] value = attributes.getStringArray("value");
    String[] basePackages = attributes.getStringArray("basePackages");
    Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");

    if (!ObjectUtils.isEmpty(value)) {
        Assert.state(ObjectUtils.isEmpty(basePackages),
                "@ConfigurationScan basePackages and value attributes are mutually exclusive");
    }

    Set<String> packagesToScan = new LinkedHashSet<>();
    packagesToScan.addAll(Arrays.asList(value));
    packagesToScan.addAll(Arrays.asList(basePackages));
    for (Class<?> basePackageClass : basePackageClasses) {
        packagesToScan.add(getPackageName(basePackageClass));
    }

    return packagesToScan.isEmpty() ? singleton(getPackageName(metadata.getClassName())) : packagesToScan;
}
 
Example 5
Source File: FlowerComponentScanRegistrar.java    From flower with Apache License 2.0 6 votes vote down vote up
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
  AnnotationAttributes attributes =
      AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(FlowerComponentScan.class.getName()));
  String[] basePackages = attributes.getStringArray("basePackages");
  Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
  String[] value = attributes.getStringArray("value");
  // Appends value array attributes
  Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value));
  packagesToScan.addAll(Arrays.asList(basePackages));
  for (Class<?> basePackageClass : basePackageClasses) {
    packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
  }
  if (packagesToScan.isEmpty()) {
    return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
  }
  return packagesToScan;
}
 
Example 6
Source File: DubboComponentScanRegistrar.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
//        获取@DubboComponentScan属性值
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(
                metadata.getAnnotationAttributes(DubboComponentScan.class.getName()));
//        查询basePackages属性值,包扫描路径
        String[] basePackages = attributes.getStringArray("basePackages");
//        扫描basePackageClasses这个属性值指定的类所在的包
        Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
        String[] value = attributes.getStringArray("value");
        // Appends value array attributes 对要扫描的包去重
        Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value));
        packagesToScan.addAll(Arrays.asList(basePackages));
        for (Class<?> basePackageClass : basePackageClasses) {
            packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
        }
//        如果上面的三个属性都没指定,就扫描这个注解所在的包
        if (packagesToScan.isEmpty()) {
            return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName()));
        }
        return packagesToScan;
    }
 
Example 7
Source File: EnodeBootstrapRegistrar.java    From enode with MIT License 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
    String declaringClass = metadata.getClass().getName();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false, this.environment, this.resourceLoader);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Command.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(Event.class));
    AnnotationAttributes enodeScan = attributesFor(metadata, EnableEnode.class.getName());
    Set<String> basePackages = new LinkedHashSet<>();
    String[] basePackagesArray = enodeScan.getStringArray("basePackages");
    for (String pkg : basePackagesArray) {
        String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        Collections.addAll(basePackages, tokenized);
    }
    for (Class<?> clazz : enodeScan.getClassArray("basePackageClasses")) {
        basePackages.add(ClassUtils.getPackageName(clazz));
    }
    if (basePackages.isEmpty()) {
        basePackages.add(ClassUtils.getPackageName(metadata.getClassName()));
    }
    scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
        @Override
        protected boolean matchClassName(String className) {
            return declaringClass.equals(className);
        }
    });
    String[] scanPackages = StringUtils.toStringArray(basePackages);
    ObjectContainer.BASE_PACKAGES = scanPackages;
    scanner.scan(scanPackages);
}
 
Example 8
Source File: ImportAwareTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void directlyAnnotatedWithImport() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ImportingConfig.class);
	ctx.refresh();
	assertNotNull(ctx.getBean("importedConfigBean"));

	ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
	AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
	assertThat("import metadata was not injected", importMetadata, notNullValue());
	assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName()));
	AnnotationAttributes importAttribs = AnnotationConfigUtils.attributesFor(importMetadata, Import.class);
	Class<?>[] importedClasses = importAttribs.getClassArray("value");
	assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName()));
}
 
Example 9
Source File: AnnotationMetadataTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) {
	AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes(
			TestComponentScan.class.getName(), false);
	String[] basePackages = attributes.getStringArray("basePackages");
	assertThat("length of basePackages[]", basePackages.length, is(1));
	assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan"));
	String[] value = attributes.getStringArray("value");
	assertThat("length of value[]", value.length, is(0));
	Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
	assertThat("length of basePackageClasses[]", basePackageClasses.length, is(0));
}
 
Example 10
Source File: AnnotationMetadataTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) {
	AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes(
			TestComponentScan.class.getName(), false);
	String[] basePackages = attributes.getStringArray("basePackages");
	assertThat("length of basePackages[]", basePackages.length, is(1));
	assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan"));
	String[] value = attributes.getStringArray("value");
	assertThat("length of value[]", value.length, is(0));
	Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
	assertThat("length of basePackageClasses[]", basePackageClasses.length, is(0));
}
 
Example 11
Source File: ImportAwareTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void directlyAnnotatedWithImportLite() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ImportingConfigLite.class);
	ctx.refresh();
	assertNotNull(ctx.getBean("importedConfigBean"));

	ImportedConfigLite importAwareConfig = ctx.getBean(ImportedConfigLite.class);
	AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
	assertThat("import metadata was not injected", importMetadata, notNullValue());
	assertThat(importMetadata.getClassName(), is(ImportingConfigLite.class.getName()));
	AnnotationAttributes importAttribs = AnnotationConfigUtils.attributesFor(importMetadata, Import.class);
	Class<?>[] importedClasses = importAttribs.getClassArray("value");
	assertThat(importedClasses[0].getName(), is(ImportedConfigLite.class.getName()));
}
 
Example 12
Source File: ImportAwareTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void directlyAnnotatedWithImport() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ImportingConfig.class);
	ctx.refresh();
	assertNotNull(ctx.getBean("importedConfigBean"));

	ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
	AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
	assertThat("import metadata was not injected", importMetadata, notNullValue());
	assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName()));
	AnnotationAttributes importAttribs = AnnotationConfigUtils.attributesFor(importMetadata, Import.class);
	Class<?>[] importedClasses = importAttribs.getClassArray("value");
	assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName()));
}
 
Example 13
Source File: AbstractTemplateProvider.java    From spring-data-generator with MIT License 5 votes vote down vote up
public AbstractTemplateProvider(AnnotationAttributes attributes) {
    Assert.notNull(attributes, "AnnotationAttributes must not be null!");
    this.excludeClasses = attributes.getClassArray(getExcludeClasses());
    this.postfix = attributes.getString(getPostfix());
    this.debug = attributes.getBoolean("debug");
    this.overwrite = attributes.getBoolean("overwrite");
    if (excludeClasses.length > 0 && debug) {
        SDLogger.debug(String.format("Exclude %s %s in the %s generator", excludeClasses.length, excludeClasses.length == 1 ? "entity":"entities", postfix));
    }
}
 
Example 14
Source File: ImportAwareTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void directlyAnnotatedWithImport() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ImportingConfig.class);
	ctx.refresh();
	assertNotNull(ctx.getBean("importedConfigBean"));

	ImportedConfig importAwareConfig = ctx.getBean(ImportedConfig.class);
	AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
	assertThat("import metadata was not injected", importMetadata, notNullValue());
	assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName()));
	AnnotationAttributes importAttribs = AnnotationConfigUtils.attributesFor(importMetadata, Import.class);
	Class<?>[] importedClasses = importAttribs.getClassArray("value");
	assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName()));
}
 
Example 15
Source File: AnnotationMetadataTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) {
	AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes(
			TestComponentScan.class.getName(), false);
	String[] basePackages = attributes.getStringArray("basePackages");
	assertThat("length of basePackages[]", basePackages.length, is(1));
	assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan"));
	String[] value = attributes.getStringArray("value");
	assertThat("length of value[]", value.length, is(0));
	Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
	assertThat("length of basePackageClasses[]", basePackageClasses.length, is(0));
}
 
Example 16
Source File: ComponentScanAnnotationParser.java    From java-technology-stack with MIT License 4 votes vote down vote up
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
	List<TypeFilter> typeFilters = new ArrayList<>();
	FilterType filterType = filterAttributes.getEnum("type");

	for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
		switch (filterType) {
			case ANNOTATION:
				Assert.isAssignable(Annotation.class, filterClass,
						"@ComponentScan ANNOTATION type filter requires an annotation type");
				@SuppressWarnings("unchecked")
				Class<Annotation> annotationType = (Class<Annotation>) filterClass;
				typeFilters.add(new AnnotationTypeFilter(annotationType));
				break;
			case ASSIGNABLE_TYPE:
				typeFilters.add(new AssignableTypeFilter(filterClass));
				break;
			case CUSTOM:
				Assert.isAssignable(TypeFilter.class, filterClass,
						"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
				TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
				ParserStrategyUtils.invokeAwareMethods(
						filter, this.environment, this.resourceLoader, this.registry);
				typeFilters.add(filter);
				break;
			default:
				throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType);
		}
	}

	for (String expression : filterAttributes.getStringArray("pattern")) {
		switch (filterType) {
			case ASPECTJ:
				typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
				break;
			case REGEX:
				typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
				break;
			default:
				throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
		}
	}

	return typeFilters;
}
 
Example 17
Source File: ComponentScanAnnotationParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
	List<TypeFilter> typeFilters = new ArrayList<TypeFilter>();
	FilterType filterType = filterAttributes.getEnum("type");

	for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
		switch (filterType) {
			case ANNOTATION:
				Assert.isAssignable(Annotation.class, filterClass,
						"@ComponentScan ANNOTATION type filter requires an annotation type");
				@SuppressWarnings("unchecked")
				Class<Annotation> annotationType = (Class<Annotation>) filterClass;
				typeFilters.add(new AnnotationTypeFilter(annotationType));
				break;
			case ASSIGNABLE_TYPE:
				typeFilters.add(new AssignableTypeFilter(filterClass));
				break;
			case CUSTOM:
				Assert.isAssignable(TypeFilter.class, filterClass,
						"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
				TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
				ParserStrategyUtils.invokeAwareMethods(
						filter, this.environment, this.resourceLoader, this.registry);
				typeFilters.add(filter);
				break;
			default:
				throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType);
		}
	}

	for (String expression : filterAttributes.getStringArray("pattern")) {
		switch (filterType) {
			case ASPECTJ:
				typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
				break;
			case REGEX:
				typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
				break;
			default:
				throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
		}
	}

	return typeFilters;
}
 
Example 18
Source File: ContextConfigurationAttributes.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Construct a new {@link ContextConfigurationAttributes} instance for the
 * supplied {@link AnnotationAttributes} (parsed from a
 * {@link ContextConfiguration @ContextConfiguration} annotation) and
 * the {@linkplain Class test class} that declared them.
 * @param declaringClass the test class that declared {@code @ContextConfiguration}
 * @param annAttrs the annotation attributes from which to retrieve the attributes
 */
@SuppressWarnings("unchecked")
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
	this(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getClassArray("classes"),
			annAttrs.getBoolean("inheritLocations"),
			(Class<? extends ApplicationContextInitializer<?>>[]) annAttrs.getClassArray("initializers"),
			annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"), annAttrs.getClass("loader"));
}
 
Example 19
Source File: ContextConfigurationAttributes.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Construct a new {@link ContextConfigurationAttributes} instance for the
 * supplied {@link AnnotationAttributes} (parsed from a
 * {@link ContextConfiguration @ContextConfiguration} annotation) and
 * the {@linkplain Class test class} that declared them.
 * @param declaringClass the test class that declared {@code @ContextConfiguration}
 * @param annAttrs the annotation attributes from which to retrieve the attributes
 */
@SuppressWarnings("unchecked")
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
	this(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getClassArray("classes"),
			annAttrs.getBoolean("inheritLocations"),
			(Class<? extends ApplicationContextInitializer<?>>[]) annAttrs.getClassArray("initializers"),
			annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"), annAttrs.getClass("loader"));
}
 
Example 20
Source File: ContextConfigurationAttributes.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Construct a new {@link ContextConfigurationAttributes} instance for the
 * supplied {@link AnnotationAttributes} (parsed from a
 * {@link ContextConfiguration @ContextConfiguration} annotation) and
 * the {@linkplain Class test class} that declared them.
 * @param declaringClass the test class that declared {@code @ContextConfiguration}
 * @param annAttrs the annotation attributes from which to retrieve the attributes
 */
@SuppressWarnings("unchecked")
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
	this(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getClassArray("classes"), annAttrs.getBoolean("inheritLocations"),
		(Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[]) annAttrs.getClassArray("initializers"),
		annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"), (Class<? extends ContextLoader>) annAttrs.getClass("loader"));
}