org.springframework.core.type.classreading.CachingMetadataReaderFactory Java Examples
The following examples show how to use
org.springframework.core.type.classreading.CachingMetadataReaderFactory.
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: CsvReportEntitiesMapping.java From aw-reporting with Apache License 2.0 | 6 votes |
/** * Finds the beans classes that are annotated with {@code CsvReport} and extends the * {@code Report} base class. * * @param basePackage the package to be scanned. * @return the list of classes that match the requirements to be a report bean. */ private List<Class<? extends Report>> findReportBeans(String basePackage) throws IOException, ClassNotFoundException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + "/" + "**/*.class"; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); List<Class<? extends Report>> candidates = new ArrayList<Class<? extends Report>>(); for (Resource resource : resources) { addCandidateIfApplicable(resource, metadataReaderFactory, candidates); } return candidates; }
Example #2
Source File: DataSourceAutoConfiguration.java From super-cloudops with Apache License 2.0 | 6 votes |
/** * 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 #3
Source File: TransactionalServiceAnnotationMetadataBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 6 votes |
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 File: MangoDaoAutoCreator.java From mango-spring-boot-starter with Apache License 2.0 | 6 votes |
private List<Class<?>> findMangoDaoClasses(String packages) { try { List<Class<?>> daos = new ArrayList<Class<?>>(); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); for (String locationPattern : getLocationPattern(packages)) { Resource[] rs = resourcePatternResolver.getResources(locationPattern); for (Resource r : rs) { MetadataReader reader = metadataReaderFactory.getMetadataReader(r); AnnotationMetadata annotationMD = reader.getAnnotationMetadata(); if (annotationMD.hasAnnotation(DB.class.getName())) { ClassMetadata clazzMD = reader.getClassMetadata(); daos.add(Class.forName(clazzMD.getClassName())); } } } return daos; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
Example #5
Source File: HandlerAnnotationFactoryBean.java From MultimediaDesktop with Apache License 2.0 | 6 votes |
private void beforeHandlerResource( ConcurrentHashMap<String, ConcurrentHashMap<HandlerScope, LinkedList<String>>> handlersMap) { try { if (!this.packagesList.isEmpty()) { for (String pkg : this.packagesList) { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN; Resource[] resources = this.resourcePatternResolver .getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory( this.resourcePatternResolver); onHandlerResource(resources, readerFactory, handlersMap); } } } catch (IOException e) { log.fatal("扫描业务处理异常", e); } }
Example #6
Source File: ClassScanner.java From kaif with Apache License 2.0 | 6 votes |
public static List<Class> searchAnnotatedClasses(String basePackage, Class<?> annotation) throws IOException, ClassNotFoundException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory( resourcePatternResolver); List<Class> candidates = new ArrayList<>(); String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + "/" + "**/*.class"; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); if (isCandidate(metadataReader, annotation)) { candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName())); } } } return candidates; }
Example #7
Source File: CustomTagAnnotations.java From rice with Educational Community License v2.0 | 6 votes |
/** * Finds all the classes which have a BeanTag or BeanTags annotation * * @param basePackage the package to start in * @return classes which have BeanTag or BeanTags annotation * @throws IOException * @throws ClassNotFoundException */ protected static List<Class<?>> findTagClasses(String basePackage) throws IOException, ClassNotFoundException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); List<Class<?>> classes = new ArrayList<Class<?>>(); String resolvedBasePackage = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders( basePackage)); String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolvedBasePackage + "/" + "**/*.class"; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); if (metadataReader != null && isBeanTag(metadataReader)) { classes.add(Class.forName(metadataReader.getClassMetadata().getClassName())); } } } return classes; }
Example #8
Source File: AutomaticJobRegistrarConfiguration.java From spring-boot-starter-batch-web with Apache License 2.0 | 6 votes |
private List<Class<?>> findMyTypes(String basePackage) throws IOException, ClassNotFoundException { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); List<Class<?>> candidates = new ArrayList<>(); String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + "/**/*.class"; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); if (isCandidate(metadataReader)) { candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName())); } } } return candidates; }
Example #9
Source File: ClassPathBeanDefinitionScannerAgent.java From HotswapAgent with GNU General Public License v2.0 | 6 votes |
private void resetCachingMetadataReaderFactoryCache() { if (getMetadataReaderFactory() instanceof CachingMetadataReaderFactory) { Map metadataReaderCache = (Map) ReflectionHelper.getNoException(getMetadataReaderFactory(), CachingMetadataReaderFactory.class, "metadataReaderCache"); if (metadataReaderCache == null) metadataReaderCache = (Map) ReflectionHelper.getNoException(getMetadataReaderFactory(), CachingMetadataReaderFactory.class, "classReaderCache"); if (metadataReaderCache != null) { metadataReaderCache.clear(); LOGGER.debug("Cache cleared: CachingMetadataReaderFactory.clearCache()"); } else { LOGGER.warning("Cache NOT cleared: neither CachingMetadataReaderFactory.metadataReaderCache nor clearCache does not exist."); } } }
Example #10
Source File: MangoDaoScanner.java From mango with Apache License 2.0 | 6 votes |
private List<Class<?>> findMangoDaoClasses() { try { List<Class<?>> daos = new ArrayList<Class<?>>(); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); for (String locationPattern : locationPatterns) { Resource[] rs = resourcePatternResolver.getResources(locationPattern); for (Resource r : rs) { MetadataReader reader = metadataReaderFactory.getMetadataReader(r); AnnotationMetadata annotationMD = reader.getAnnotationMetadata(); if (annotationMD.hasAnnotation(DB.class.getName())) { ClassMetadata clazzMD = reader.getClassMetadata(); daos.add(Class.forName(clazzMD.getClassName())); } } } return daos; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
Example #11
Source File: ModelClassFinder.java From herd with Apache License 2.0 | 5 votes |
/** * Finds all the model classes and the model error class. * * @throws MojoExecutionException if a class couldn't be instantiated or an I/O error occurred. */ private void findModelClasses() throws MojoExecutionException { try { log.debug("Finding model classes."); // Get the model classes as resources. modelClasses = new HashSet<>(); // Loop through each model resource and add each one to the set of model classes. for (Resource resource : ResourceUtils.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(modelJavaPackage)) + "/**/*.class")) { if (resource.isReadable()) { MetadataReader metadataReader = new CachingMetadataReaderFactory(new PathMatchingResourcePatternResolver()).getMetadataReader(resource); Class<?> clazz = Class.forName(metadataReader.getClassMetadata().getClassName()); modelClasses.add(clazz); log.debug("Found model class \"" + clazz.getName() + "\"."); // If the model error class name is configured and matches this class, then hold onto it. if (clazz.getSimpleName().equals(modelErrorClassName)) { log.debug("Found model error class \"" + clazz.getName() + "\"."); modelErrorClass = clazz; } } } } catch (IOException | ClassNotFoundException e) { throw new MojoExecutionException("Error finding model classes. Reason: " + e.getMessage(), e); } }
Example #12
Source File: TopicConsumerSpringProvider.java From jeesuite-libs with Apache License 2.0 | 5 votes |
private void scanAndRegisterAnnotationTopics(String[] scanBasePackages){ String RESOURCE_PATTERN = "/**/*.class"; ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); for (String scanBasePackage : scanBasePackages) { logger.info(">>begin scan package [{}] with Annotation[ConsumerHandler] MessageHanlder ",scanBasePackage); try { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(scanBasePackage) + RESOURCE_PATTERN; org.springframework.core.io.Resource[] resources = resourcePatternResolver.getResources(pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver); for (org.springframework.core.io.Resource resource : resources) { if (resource.isReadable()) { MetadataReader reader = readerFactory.getMetadataReader(resource); String className = reader.getClassMetadata().getClassName(); Class<?> clazz = Class.forName(className); if(clazz.isAnnotationPresent(ConsumerHandler.class)){ ConsumerHandler annotation = clazz.getAnnotation(ConsumerHandler.class); MessageHandler hander = (MessageHandler) context.getBean(clazz); if(!topicHandlers.containsKey(annotation.topic())){ topicHandlers.put(annotation.topic(), hander); logger.info("register new MessageHandler:{}-{}",annotation.topic(),clazz.getName()); } } } } logger.info("<<scan package["+scanBasePackage+"] finished!"); } catch (Exception e) { if(e instanceof org.springframework.beans.factory.NoSuchBeanDefinitionException){ throw (org.springframework.beans.factory.NoSuchBeanDefinitionException)e; } logger.error("<<scan package["+scanBasePackage+"] error", e); } } }
Example #13
Source File: AsmCircularImportDetectionTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override protected ConfigurationClassParser newParser() { return new ConfigurationClassParser( new CachingMetadataReaderFactory(), new FailFastProblemReporter(), new StandardEnvironment(), new DefaultResourceLoader(), new AnnotationBeanNameGenerator(), new DefaultListableBeanFactory()); }
Example #14
Source File: ConfigurationClassPostProcessor.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(beanClassLoader); } }
Example #15
Source File: ConfigurationClassPostProcessor.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(beanClassLoader); } }
Example #16
Source File: ConfigurationClassPostProcessor.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void setResourceLoader(ResourceLoader resourceLoader) { Assert.notNull(resourceLoader, "ResourceLoader must not be null"); this.resourceLoader = resourceLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader); } }
Example #17
Source File: TestModuleInitializer.java From spring-init with Apache License 2.0 | 5 votes |
@Override public void initialize(GenericApplicationContext context) { if (!ClassUtils.isPresent("org.springframework.boot.test.context.ImportsContextCustomizer", context.getClassLoader()) || !context.getEnvironment().getProperty("spring.functional.enabled", Boolean.class, true)) { // Only used in tests - could move to separate jar return; } ImportRegistrars registrars; // TODO: extract this logic and share with FunctionalInstallerListener? 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)); registrars = new FunctionalInstallerImportRegistrars(context); context.registerBean(ImportRegistrars.class, () -> registrars); } else { registrars = context.getBean(ImportRegistrars.class.getName(), ImportRegistrars.class); } for (String name : context.getBeanFactory().getBeanDefinitionNames()) { BeanDefinition definition = context.getBeanFactory().getBeanDefinition(name); if (definition.getBeanClassName().contains("ImportsContextCustomizer$ImportsConfiguration")) { SimpleConditionService.EXCLUDES_ENABLED = true; Class<?> testClass = (definition != null) ? (Class<?>) definition.getAttribute("testClass") : null; if (testClass != null) { Set<Import> merged = AnnotatedElementUtils.findAllMergedAnnotations(testClass, Import.class); for (Import ann : merged) { for (Class<?> imported : ann.value()) { registrars.add(testClass, imported); } } } } } }
Example #18
Source File: ConfigurationClassPostProcessor.java From spring-analysis-note with MIT License | 5 votes |
@Override public void setResourceLoader(ResourceLoader resourceLoader) { Assert.notNull(resourceLoader, "ResourceLoader must not be null"); this.resourceLoader = resourceLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader); } }
Example #19
Source File: FunctionalInstallerListener.java From spring-init with Apache License 2.0 | 5 votes |
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 #20
Source File: FunctionalInstallerImportRegistrars.java From spring-init with Apache License 2.0 | 5 votes |
public FunctionalInstallerImportRegistrars(GenericApplicationContext context) { this.context = context; String metadataFactory = MetadataReaderFactory.class.getName(); this.metadataReaderFactory = context.getBeanFactory().containsSingleton(metadataFactory) ? (MetadataReaderFactory) context.getBeanFactory().getSingleton(metadataFactory) : new CachingMetadataReaderFactory(context.getClassLoader()); }
Example #21
Source File: ClassPathScanningCandidateComponentProvider.java From java-technology-stack with MIT License | 5 votes |
/** * Return the MetadataReaderFactory used by this component provider. */ public final MetadataReaderFactory getMetadataReaderFactory() { if (this.metadataReaderFactory == null) { this.metadataReaderFactory = new CachingMetadataReaderFactory(); } return this.metadataReaderFactory; }
Example #22
Source File: ConfigurationClassPostProcessor.java From spring-analysis-note with MIT License | 5 votes |
@Override public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(beanClassLoader); } }
Example #23
Source File: ClassPathScanningCandidateComponentProvider.java From spring-analysis-note with MIT License | 5 votes |
/** * Return the MetadataReaderFactory used by this component provider. */ public final MetadataReaderFactory getMetadataReaderFactory() { if (this.metadataReaderFactory == null) { this.metadataReaderFactory = new CachingMetadataReaderFactory(); } return this.metadataReaderFactory; }
Example #24
Source File: ClassPathScanningCandidateComponentProvider.java From spring-analysis-note with MIT License | 5 votes |
/** * Clear the local metadata cache, if any, removing all cached class metadata. */ public void clearCache() { if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it'll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } }
Example #25
Source File: AsmCircularImportDetectionTests.java From spring-analysis-note with MIT License | 5 votes |
@Override protected ConfigurationClassParser newParser() { return new ConfigurationClassParser( new CachingMetadataReaderFactory(), new FailFastProblemReporter(), new StandardEnvironment(), new DefaultResourceLoader(), new AnnotationBeanNameGenerator(), new DefaultListableBeanFactory()); }
Example #26
Source File: ConfigurationClassPostProcessor.java From java-technology-stack with MIT License | 5 votes |
@Override public void setResourceLoader(ResourceLoader resourceLoader) { Assert.notNull(resourceLoader, "ResourceLoader must not be null"); this.resourceLoader = resourceLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader); } }
Example #27
Source File: ConfigurationClassPostProcessor.java From java-technology-stack with MIT License | 5 votes |
@Override public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(beanClassLoader); } }
Example #28
Source File: SimpleConditionService.java From spring-init with Apache License 2.0 | 5 votes |
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); }
Example #29
Source File: ClassPathScanningCandidateComponentProvider.java From java-technology-stack with MIT License | 5 votes |
/** * Clear the local metadata cache, if any, removing all cached class metadata. */ public void clearCache() { if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it'll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } }
Example #30
Source File: AsmCircularImportDetectionTests.java From java-technology-stack with MIT License | 5 votes |
@Override protected ConfigurationClassParser newParser() { return new ConfigurationClassParser( new CachingMetadataReaderFactory(), new FailFastProblemReporter(), new StandardEnvironment(), new DefaultResourceLoader(), new AnnotationBeanNameGenerator(), new DefaultListableBeanFactory()); }