org.springframework.core.type.classreading.MetadataReaderFactory Java Examples

The following examples show how to use org.springframework.core.type.classreading.MetadataReaderFactory. 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 Project: super-cloudops   Author: wl4g   File: DataSourceAutoConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Let typeAliasesPackage alias bean support wildcards.
 * 
 * @return
 */
private Class<?>[] getTypeAliases(PathMatchingResourcePatternResolver resolver) throws Exception {
	List<Class<?>> typeAliases = new ArrayList<>();

	// Define metadataReader
	MetadataReaderFactory metadataReaderFty = new CachingMetadataReaderFactory(resolver);

	for (String pkg : typeAliasesPackage.split(",")) {
		// Get location
		String location = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg)
				+ "**/*.class";
		// Get resources.
		Resource[] resources = resolver.getResources(location);
		if (resources != null) {
			for (Resource resource : resources) {
				if (resource.isReadable()) {
					MetadataReader metadataReader = metadataReaderFty.getMetadataReader(resource);
					typeAliases.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
				}
			}
		}
	}

	return typeAliases.toArray(new Class<?>[] {});
}
 
Example #2
Source Project: code   Author: lastwhispers   File: MyTypeFilter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 自定义包扫描过滤规则
 *
 * @param metadataReader 读取到当前正在扫描的类的信息
 * @param metadataReaderFactory 获取其他任何类型
 * @return boolean
 */
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
    // 获取当前正在扫描的类注解的信息
    AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
    // 获取当前正在扫描的类的信息
    ClassMetadata classMetadata = metadataReader.getClassMetadata();
    // 获取类名
    String className = classMetadata.getClassName();
    System.out.println("扫描的类——》" + className);
    // 根据类名进行过滤
    if (className.contains("Book")) {
        return true;
    }
    // 证明filter在Conditional之前拦截
    //Annotation[] annotations = classMetadata.getClass().getAnnotations();
    //for (Annotation annotation : annotations) {
    //    if (annotation.annotationType() == Conditional.class) {
    //        return false;
    //    }
    //}
    //return true;
    return false;
}
 
Example #3
public static void main(String[] args) throws IOException {
    // @TransactionalService 标注在当前类 TransactionalServiceAnnotationMetadataBootstrap
    String className = TransactionalServiceAnnotationMetadataBootstrap.class.getName();
    // 构建 MetadataReaderFactory 实例
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory();
    // 读取 @TransactionService MetadataReader 信息
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className);
    // 读取 @TransactionService AnnotationMetadata 信息
    AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();

    annotationMetadata.getAnnotationTypes().forEach(annotationType -> {

        Set<String> metaAnnotationTypes = annotationMetadata.getMetaAnnotationTypes(annotationType);

        metaAnnotationTypes.forEach(metaAnnotationType -> {
            System.out.printf("注解 @%s 元标注 @%s\n", annotationType, metaAnnotationType);
        });

    });
}
 
Example #4
Source Project: spring-analysis-note   Author: Vip-Augus   File: ConfigurationClassParser.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@link ConfigurationClassParser} instance that will be used
 * to populate the set of configuration classes.
 */
public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory,
		ProblemReporter problemReporter, Environment environment, ResourceLoader resourceLoader,
		BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry) {

	this.metadataReaderFactory = metadataReaderFactory;
	this.problemReporter = problemReporter;
	this.environment = environment;
	this.resourceLoader = resourceLoader;
	this.registry = registry;
	this.componentScanParser = new ComponentScanAnnotationParser(
			environment, resourceLoader, componentScanBeanNameGenerator, registry);
	this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader);
}
 
Example #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: ClassPathScanningCandidateComponentProvider.java    License: MIT License 5 votes vote down vote up
/**
 * Return the MetadataReaderFactory used by this component provider.
 */
public final MetadataReaderFactory getMetadataReaderFactory() {
	if (this.metadataReaderFactory == null) {
		this.metadataReaderFactory = new CachingMetadataReaderFactory();
	}
	return this.metadataReaderFactory;
}
 
Example #6
Source Project: spring-analysis-note   Author: Vip-Augus   File: ComponentScanAnnotationIntegrationTests.java    License: MIT License 5 votes vote down vote up
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
	((ConfigurableEnvironment) this.environment).addActiveProfile("the-filter-ran");
	assertNotNull(this.beanFactory);
	assertNotNull(this.classLoader);
	assertNotNull(this.resourceLoader);
	assertNotNull(this.environment);
	return false;
}
 
Example #7
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationScopeMetadataResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void customRequestScopeViaAsm() throws IOException {
	MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
	MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScope.class.getName());
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata());
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(NO, scopeMetadata.getScopedProxyMode());
}
 
Example #8
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationScopeMetadataResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void customRequestScopeWithAttributeViaAsm() throws IOException {
	MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
	MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScopeWithAttributeOverride.class.getName());
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata());
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode());
}
 
Example #9
Source Project: spring-analysis-note   Author: Vip-Augus   File: AspectJTypeFilter.java    License: MIT License 5 votes vote down vote up
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
		throws IOException {

	String className = metadataReader.getClassMetadata().getClassName();
	ResolvedType resolvedType = this.world.resolve(className);
	return this.typePattern.matchesStatically(resolvedType);
}
 
Example #10
Source Project: spring-init   Author: spring-projects-experimental   File: FunctionalInstallerListener.java    License: Apache License 2.0 5 votes vote down vote up
private ConditionService initialize(GenericApplicationContext context) {
	if (!context.getBeanFactory().containsSingleton(ConditionService.class.getName())) {
		if (!context.getBeanFactory().containsSingleton(MetadataReaderFactory.class.getName())) {
			context.getBeanFactory().registerSingleton(MetadataReaderFactory.class.getName(),
					new CachingMetadataReaderFactory(context.getClassLoader()));
		}
		context.getBeanFactory().registerSingleton(ConditionService.class.getName(),
				new SimpleConditionService(context, context.getBeanFactory(), context.getEnvironment(), context));
		context.registerBean(ImportRegistrars.class, () -> new FunctionalInstallerImportRegistrars(context));
	}
	return (ConditionService) context.getBeanFactory().getSingleton(ConditionService.class.getName());
}
 
Example #11
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testDirectAnnotationMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponent";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #12
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
	// Must fail as annotation on interfaces should not be considered a match
	assertFalse(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #13
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubclassOfSomeComponent";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #14
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testNonInheritedAnnotationDoesNotMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubclassOfSomeClassMarkedWithNonInheritedAnnotation";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(NonInheritedAnnotation.class);
	// Must fail as annotation isn't inherited
	assertFalse(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #15
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testNonAnnotatedClassDoesntMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeNonCandidateClass";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(Component.class);
	assertFalse(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #16
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testMatchesInterfacesIfConfigured() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class, false, true);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #17
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test
public void asmAnnotationMetadata() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponent.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	doTestAnnotationInfo(metadata);
	doTestMethodAnnotationInfo(metadata);
}
 
Example #18
Source Project: SpringAll   Author: wuyouzhuguli   File: MyTypeFilter.java    License: MIT License 5 votes vote down vote up
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
    // 获取当前正在扫描的类的注解信息
    AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
    // 获取当前正在扫描的类的类信息
    ClassMetadata classMetadata = metadataReader.getClassMetadata();
    // 获取当前正在扫描的类的路径等信息
    Resource resource = metadataReader.getResource();

    String className = classMetadata.getClassName();
    return StringUtils.hasText("er");
}
 
Example #19
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test
public void asmAnnotationMetadataForInterface() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotationMetadata.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	doTestMetadataForInterfaceClass(metadata);
}
 
Example #20
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test
public void asmAnnotationMetadataForAnnotation() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(Component.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	doTestMetadataForAnnotationClass(metadata);
}
 
Example #21
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test
public void metaAnnotationOverridesUsingAnnotationMetadataReadingVisitor() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(ComposedConfigurationWithAttributeOverridesClass.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	assertMetaAnnotationOverrides(metadata);
}
 
Example #22
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test  // SPR-11649
public void multipleAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedAnnotationsClass.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	assertMultipleAnnotationsWithIdenticalAttributeNames(metadata);
}
 
Example #23
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test  // SPR-11649
public void composedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedComposedAnnotationClass.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	assertMultipleAnnotationsWithIdenticalAttributeNames(metadata);
}
 
Example #24
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationMetadataTests.java    License: MIT License 5 votes vote down vote up
@Test
public void inheritedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedComposedAnnotationExtended.class.getName());
	AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
	assertFalse(metadata.hasAnnotation(NamedComposedAnnotation.class.getName()));
}
 
Example #25
Source Project: spring-analysis-note   Author: Vip-Augus   File: AssignableTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void directMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$TestNonInheritingClass";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AssignableTypeFilter matchingFilter = new AssignableTypeFilter(TestNonInheritingClass.class);
	AssignableTypeFilter notMatchingFilter = new AssignableTypeFilter(TestInterface.class);
	assertFalse(notMatchingFilter.match(metadataReader, metadataReaderFactory));
	assertTrue(matchingFilter.match(metadataReader, metadataReaderFactory));
}
 
Example #26
Source Project: spring-analysis-note   Author: Vip-Augus   File: AssignableTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void interfaceMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$TestInterfaceImpl";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AssignableTypeFilter filter = new AssignableTypeFilter(TestInterface.class);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #27
Source Project: spring-analysis-note   Author: Vip-Augus   File: AssignableTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void superClassMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AssignableTypeFilter filter = new AssignableTypeFilter(SimpleJdbcDaoSupport.class);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #28
Source Project: spring-analysis-note   Author: Vip-Augus   File: AssignableTypeFilterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void interfaceThroughSuperClassMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AssignableTypeFilter filter = new AssignableTypeFilter(JdbcDaoSupport.class);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #29
Source Project: spring-analysis-note   Author: Vip-Augus   File: AspectJTypeFilterTests.java    License: MIT License 5 votes vote down vote up
private void assertMatch(String type, String typePattern) throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type);

	AspectJTypeFilter filter = new AspectJTypeFilter(typePattern, getClass().getClassLoader());
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(type);
}
 
Example #30
Source Project: spring-init   Author: spring-projects-experimental   File: SimpleConditionService.java    License: Apache License 2.0 5 votes vote down vote up
public SimpleConditionService(BeanDefinitionRegistry registry, ConfigurableListableBeanFactory beanFactory,
		Environment environment, ResourceLoader resourceLoader) {
	this.beanFactory = beanFactory;
	this.evaluator = new ConditionEvaluator(registry, environment, resourceLoader);
	this.classLoader = resourceLoader.getClassLoader();
	String metadataFactory = MetadataReaderFactory.class.getName();
	this.metadataReaderFactory = beanFactory.containsSingleton(metadataFactory)
			? (MetadataReaderFactory) beanFactory.getSingleton(metadataFactory)
			: new CachingMetadataReaderFactory(this.classLoader);
}