Java Code Examples for org.springframework.core.type.classreading.MetadataReader#getClassMetadata()
The following examples show how to use
org.springframework.core.type.classreading.MetadataReader#getClassMetadata() .
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: AutoJsonRpcClientProxyCreator.java From jsonrpc4j with MIT License | 6 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext); DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory; String resolvedPath = resolvePackageToScan(); logger.debug("Scanning '{}' for JSON-RPC service interfaces.", resolvedPath); try { for (Resource resource : applicationContext.getResources(resolvedPath)) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); ClassMetadata classMetadata = metadataReader.getClassMetadata(); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); String jsonRpcPathAnnotation = JsonRpcService.class.getName(); if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) { String className = classMetadata.getClassName(); String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation).get("value"); logger.debug("Found JSON-RPC service to proxy [{}] on path '{}'.", className, path); registerJsonProxyBean(defaultListableBeanFactory, className, path); } } } } catch (IOException e) { throw new IllegalStateException(format("Cannot scan package '%s' for classes.", resolvedPath), e); } }
Example 2
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 3
Source File: MyBatisXmlParser.java From jstarcraft-core with Apache License 2.0 | 6 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; LOGGER.error(message, exception); throw new StorageConfigurationException(message, exception); } }
Example 4
Source File: CubaClientTestCase.java From cuba with Apache License 2.0 | 6 votes |
protected List<String> getClasses(Resource[] resources) { List<String> classNames = new ArrayList<>(); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader metadataReader; try { metadataReader = metadataReaderFactory.getMetadataReader(resource); } catch (IOException e) { throw new RuntimeException("Unable to read metadata resource", e); } AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (annotationMetadata.isAnnotated(com.haulmont.chile.core.annotations.MetaClass.class.getName()) || annotationMetadata.isAnnotated(MappedSuperclass.class.getName()) || annotationMetadata.isAnnotated(Entity.class.getName())) { ClassMetadata classMetadata = metadataReader.getClassMetadata(); classNames.add(classMetadata.getClassName()); } } } return classNames; }
Example 5
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 6
Source File: MyTypeFilter.java From code with Apache License 2.0 | 6 votes |
/** * 自定义包扫描过滤规则 * * @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 7
Source File: CommunicationXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String name = CommunicationModule.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (!annotationMetadata.hasAnnotation(name)) { continue; } ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; LOGGER.error(message, exception); throw new CommunicationConfigurationException(message, exception); } }
Example 8
Source File: ComponentScanCustomFilter.java From tutorials with MIT License | 5 votes |
@Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { ClassMetadata classMetadata = metadataReader.getClassMetadata(); String fullyQualifiedName = classMetadata.getClassName(); String className = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(".") + 1); return className.length() > 5 ? true : false; }
Example 9
Source File: ActionsImport.java From data-prep with Apache License 2.0 | 5 votes |
@Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { final ClassMetadata classMetadata = metadataReader.getClassMetadata(); try { final Class<?> clazz = Class.forName(classMetadata.getClassName()); return AnnotationUtils.findAnnotation(clazz, Action.class) != null; } catch (Throwable e) { // NOSONAR LOGGER.debug("Unable to filter class {}.", classMetadata.getClassName(), e); } return false; }
Example 10
Source File: AppPropertiesLocator.java From cuba with Apache License 2.0 | 5 votes |
protected Set<Class> findConfigInterfaces() { if (interfacesCache == null) { synchronized (this) { if (interfacesCache == null) { log.trace("Locating config interfaces"); Set<String> cache = new HashSet<>(); for (String rootPackage : metadata.getRootPackages()) { String packagePrefix = rootPackage.replace(".", "/") + "/**/*.class"; String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + packagePrefix; Resource[] resources; try { resources = resourcePatternResolver.getResources(packageSearchPath); for (Resource resource : resources) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); ClassMetadata classMetadata = metadataReader.getClassMetadata(); if (classMetadata.isInterface()) { for (String intf : classMetadata.getInterfaceNames()) { if (Config.class.getName().equals(intf)) { cache.add(classMetadata.getClassName()); break; } } } } } } catch (IOException e) { throw new RuntimeException("Error searching for Config interfaces", e); } } log.trace("Found config interfaces: {}", cache); interfacesCache = cache; } } } return interfacesCache.stream() .map(ReflectionHelper::getClass) .collect(Collectors.toSet()); }
Example 11
Source File: MyTypeFilter.java From SpringAll with MIT License | 5 votes |
@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 12
Source File: CodecXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
/** * 获取指定包下的静态资源对象 * * @param packageName 包名 * @return * @throws IOException */ private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String name = ProtocolConfiguration.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource); ClassMetadata classMetadata = metadataReader.getClassMetadata(); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (annotationMetadata.hasAnnotation(name)) { names.add(classMetadata.getClassName()); } else { String[] interfaceNames = classMetadata.getInterfaceNames(); for (String interfaceName : interfaceNames) { metadataReader = this.metadataReaderFactory.getMetadataReader(interfaceName); annotationMetadata = metadataReader.getAnnotationMetadata(); if (annotationMetadata.hasAnnotation(name)) { names.add(classMetadata.getClassName()); break; } } } } return names.toArray(new String[0]); } catch (IOException exception) { throw new RuntimeException(exception); } }
Example 13
Source File: CacheXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String name = CacheConfiguration.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (!annotationMetadata.hasAnnotation(name)) { continue; } ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; LOGGER.error(message, exception); throw new CacheConfigurationException(message, exception); } }
Example 14
Source File: LuceneXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String document = LuceneConfiguration.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (!annotationMetadata.hasAnnotation(document)) { continue; } ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; LOGGER.error(message, exception); throw new StorageConfigurationException(message, exception); } }
Example 15
Source File: BerkeleyXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String entity = Entity.class.getName(); String persistent = Persistent.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (!annotationMetadata.hasAnnotation(entity) && !annotationMetadata.hasAnnotation(persistent)) { continue; } ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; LOGGER.error(message, exception); throw new StorageConfigurationException(message, exception); } }
Example 16
Source File: MongoXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String document = Document.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (!annotationMetadata.hasAnnotation(document)) { continue; } ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; LOGGER.error(message, exception); throw new StorageConfigurationException(message, exception); } }
Example 17
Source File: ResourceXmlParser.java From jstarcraft-core with Apache License 2.0 | 5 votes |
private String[] getResources(String packageName) { try { // 搜索资源 String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN; Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); // 提取资源 Set<String> names = new HashSet<String>(); String name = ResourceConfiguration.class.getName(); for (Resource resource : resources) { if (!resource.isReadable()) { continue; } // 判断是否静态资源 MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); if (!annotationMetadata.hasAnnotation(name)) { continue; } ClassMetadata classMetadata = metadataReader.getClassMetadata(); names.add(classMetadata.getClassName()); } return names.toArray(new String[0]); } catch (IOException exception) { String message = "无法获取资源"; logger.error(message, exception); throw new StorageException(message, exception); } }
Example 18
Source File: RpcDefinitionPostProcessor.java From joyrpc with Apache License 2.0 | 5 votes |
@Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) { ClassMetadata classMetadata = metadataReader.getClassMetadata(); if (classMetadata.isConcrete() && !classMetadata.isAnnotation()) { //找到类 Class<?> clazz = resolveClassName(classMetadata.getClassName(), classLoader); //判断是否Public if (Modifier.isPublic(clazz.getModifiers())) { Class<?> targetClass = clazz; while (targetClass != null && targetClass != Object.class) { //处理类上的服务提供者注解 if (getProviderAnnotation(targetClass) != null) { return true; } //处理字段的消费者注解 for (Field field : targetClass.getDeclaredFields()) { if (!Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers()) && getConsumerAnnotation(field) != null) { return true; } } //处理方法上的消费者注解 for (Method method : clazz.getDeclaredMethods()) { if (!Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers()) && method.getParameterCount() == 1 && method.getName().startsWith("set") && getConsumerAnnotation(method) != null) { return true; } } targetClass = targetClass.getSuperclass(); } } } return false; }