@Override public void afterPropertiesSet() throws Exception { // on recherche toutes les classes concrètes du package à la recherche de celles qui sont annotées 'TipiTopProcess' final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) { @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { return beanDefinition.getMetadata().isConcrete(); } }; scanner.addIncludeFilter(new AnnotationTypeFilter(TipiTopProcess.class)); if (excludeFilters != null) { for (TypeFilter filter : excludeFilters) { scanner.addExcludeFilter(filter); } } Set<BeanDefinition> beans = scanner.findCandidateComponents(aPackage); LOGGER.info("Registering " + beans.size() + " Tipi activities"); for (BeanDefinition bean : beans) { Class<?> clazz = Class.forName(bean.getBeanClassName()); registerClass(clazz); } }
/** * Configures parent scanner to search for the right interfaces. It can search * for all interfaces or just for those that extends a markerInterface or/and * those annotated with the annotationClass */ public void registerFilters() { if (Envs.classExists("org.n3r.eql.eqler.annotations.Eqler")) { addExcludeFilter(new AnnotationTypeFilter(Eqler.class)); addExcludeFilter(new AnnotationTypeFilter(EqlerConfig.class)); } addIncludeFilter(new TypeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory ) throws IOException { val metadata = metadataReader.getClassMetadata(); if (!metadata.isInterface()) return false; val className = metadata.getClassName(); val clazz = Envs.forName(className); return Anns.isFastWestCacheAnnotated(clazz); } }); }
@SuppressWarnings("unchecked") private TypeFilter createTypeFilter(FilterType filterType, Class<?> filterClass) { switch (filterType) { case ANNOTATION: Assert.isAssignable(Annotation.class, filterClass, "An error occurred while processing a ANNOTATION type filter: "); return new AnnotationTypeFilter((Class<Annotation>) filterClass); case ASSIGNABLE_TYPE: return new AssignableTypeFilter(filterClass); case CUSTOM: Assert.isAssignable(TypeFilter.class, filterClass, "An error occurred while processing a CUSTOM type filter: "); return BeanUtils.instantiateClass(filterClass, TypeFilter.class); } throw new IllegalArgumentException( "Filter type not supported with Class value: " + filterType); }
/** * Invoke {@link ResourceLoaderAware}, {@link BeanClassLoaderAware} and * {@link BeanFactoryAware} contracts if implemented by the given {@code filter}. */ private void invokeAwareMethods(TypeFilter filter) { if (filter instanceof Aware) { if (filter instanceof EnvironmentAware) { ((EnvironmentAware) filter).setEnvironment(this.environment); } if (filter instanceof ResourceLoaderAware) { ((ResourceLoaderAware) filter).setResourceLoader(this.resourceLoader); } if (filter instanceof BeanClassLoaderAware) { ClassLoader classLoader = (this.registry instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) this.registry).getBeanClassLoader() : this.resourceLoader.getClassLoader()); ((BeanClassLoaderAware) filter).setBeanClassLoader(classLoader); } if (filter instanceof BeanFactoryAware && this.registry instanceof BeanFactory) { ((BeanFactoryAware) filter).setBeanFactory((BeanFactory) this.registry); } } }
private Set<Class<?>> scanForClasses(String packageName, TypeFilter typeFilter) throws IOException { Set<Class<?>> result = new HashSet<Class<?>>(); String classSearchPattern = getClassSearchPattern(packageName); Resource[] resources = this.resourcePatternResolver.getResources(classSearchPattern); for (Resource resource : resources) { if (!this.matches(resource, typeFilter)) { continue; } try { Class<?> foundClass = this.getClass(resource); result.add(foundClass); } catch (ClassNotFoundException e) { throw new IOException("Failed to read class '" + resource.getFilename() + "'.", e); } } return result; }
@SuppressWarnings("unchecked") private static <T> Set<Class<? extends T>> findClasses(String rootPackage, Class<T> clazz, TypeFilter filter) { ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(filter); Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents(rootPackage); Set<Class<? extends T>> classes = new LinkedHashSet<Class<? extends T>>(); for (BeanDefinition beanDefinition : beanDefinitions) { try { classes.add((Class<? extends T>) Class.forName(beanDefinition.getBeanClassName())); } catch (ClassNotFoundException e) { LOGGER.warn("Class not found: " + beanDefinition.getBeanClassName()); } } return classes; }
/** * 检查当前扫描到的Bean含有任何一个指定的注解标记 * * @param reader * @param readerFactory * @return * @throws IOException */ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { if (!this.typeFilters.isEmpty()) { for (TypeFilter filter : this.typeFilters) { if (filter.match(reader, readerFactory)) { return true; } } } return false; }
@SuppressWarnings("unchecked") protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader) { String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); try { if ("annotation".equals(filterType)) { return new AnnotationTypeFilter((Class<Annotation>) classLoader.loadClass(expression)); } else if ("assignable".equals(filterType)) { return new AssignableTypeFilter(classLoader.loadClass(expression)); } else if ("aspectj".equals(filterType)) { return new AspectJTypeFilter(expression, classLoader); } else if ("regex".equals(filterType)) { return new RegexPatternTypeFilter(Pattern.compile(expression)); } else if ("custom".equals(filterType)) { Class<?> filterClass = classLoader.loadClass(expression); if (!TypeFilter.class.isAssignableFrom(filterClass)) { throw new IllegalArgumentException( "Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression); } return (TypeFilter) BeanUtils.instantiateClass(filterClass); } else { throw new IllegalArgumentException("Unsupported filter type: " + filterType); } } catch (ClassNotFoundException ex) { throw new FatalBeanException("Type filter class not found: " + expression, ex); } }
/** * Check whether any of the configured entity type filters matches * the current class descriptor contained in the metadata reader. */ private boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { for (TypeFilter filter : entityTypeFilters) { if (filter.match(reader, readerFactory)) { return true; } } return false; }
/** * Check whether any of the configured entity type filters matches * the current class descriptor contained in the metadata reader. */ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { for (TypeFilter filter : entityTypeFilters) { if (filter.match(reader, readerFactory)) { return true; } } return false; }
/** * Check whether any of the configured entity type filters matches * the current class descriptor contained in the metadata reader. */ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { if (this.entityTypeFilters != null) { for (TypeFilter filter : this.entityTypeFilters) { if (filter.match(reader, readerFactory)) { return true; } } } return false; }
public void registerDefaultFilters() { this.addIncludeFilter(new TypeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return true; } }); }
/** * 检查当前扫描到的Bean含有任何一个指定的注解标记 * @param reader * @param readerFactory * @return * @throws IOException */ private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { if (!this.typeFilters.isEmpty()) { for (TypeFilter filter : this.typeFilters) { if (filter.match(reader, readerFactory)) { return true; } } } return false; }
@Override public boolean isCandidateComponent(MetadataReader metadataReader) throws IOException { for(TypeFilter filter: includeFilters){ if(!filter.match(metadataReader, metadataReaderFactory)){ return false; } } return true; }
private boolean isCodeableClass(MetadataReader metadataReader) throws IOException { for (TypeFilter tf : this.includeFilters) { if (tf.match(metadataReader, this.metadataReaderFactory)) { return true; } } return false; }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException { // TODO Auto-generated method stub TypeFilter filter = new AnnotationTypeFilter(Wmz7year.class); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner((BeanDefinitionRegistry) arg0, false); scanner.addIncludeFilter(filter); int scan = scanner.scan("com.wmz7year.main"); System.out.println("scan:" + scan); }
@SuppressWarnings("unchecked") protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader, ParserContext parserContext) { String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression); try { if ("annotation".equals(filterType)) { return new AnnotationTypeFilter((Class<Annotation>) classLoader.loadClass(expression)); } else if ("assignable".equals(filterType)) { return new AssignableTypeFilter(classLoader.loadClass(expression)); } else if ("aspectj".equals(filterType)) { return new AspectJTypeFilter(expression, classLoader); } else if ("regex".equals(filterType)) { return new RegexPatternTypeFilter(Pattern.compile(expression)); } else if ("custom".equals(filterType)) { Class<?> filterClass = classLoader.loadClass(expression); if (!TypeFilter.class.isAssignableFrom(filterClass)) { throw new IllegalArgumentException( "Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression); } return (TypeFilter) BeanUtils.instantiateClass(filterClass); } else { throw new IllegalArgumentException("Unsupported filter type: " + filterType); } } catch (ClassNotFoundException ex) { throw new FatalBeanException("Type filter class not found: " + expression, ex); } }
protected boolean isJaxb2Class(MetadataReader reader, MetadataReaderFactory factory) throws IOException { for (TypeFilter filter : JAXB2_TYPE_FILTERS) { if (filter.match(reader, factory) && !reader.getClassMetadata().isInterface() ) { return true; } } return false; }
private List<TypeFilter> createTypeFilters(Filter[] filters) { List<TypeFilter> typeFilters = new ArrayList<TypeFilter>(); for (Filter filter : filters) { for (Class<?> filterClass : filter.classes()) { typeFilters.add(createTypeFilter(filter.type(), filterClass)); } for (String pattern : filter.pattern()) { typeFilters.add(createTypeFilter(filter.type(), pattern)); } } return Collections.unmodifiableList(typeFilters); }
private TypeFilter createTypeFilter(FilterType filterType, String pattern) { switch (filterType) { case ASPECTJ: return new AspectJTypeFilter(pattern, this.classLoader); case REGEX: return new RegexPatternTypeFilter(Pattern.compile(pattern)); } throw new IllegalArgumentException( "Filter type not supported with String pattern: " + filterType); }
public boolean anyMatches(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { for (TypeFilter filter : this) { if (filter.match(metadataReader, metadataReaderFactory)) { return true; } } return false; }
DataMappers() { scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(new TypeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { if(metadataReader.getClassMetadata().getClassName().equals(DataMappers.class.getName())) return false; String[] iFaces = metadataReader.getClassMetadata().getInterfaceNames(); Arrays.sort(iFaces); return Arrays.binarySearch(iFaces, DataMapper.class.getName()) >= 0; } }); Set<BeanDefinition> set = scanner.findCandidateComponents(ClassUtils.getPackageName(DataMapper.class)); if(set.isEmpty()) { throw new BeanCreationException("No data mapper implementation classes could be found under package ["+ClassUtils.getPackageName(DataMapper.class)+"]"); } for(BeanDefinition bd : set) { try { DataMapper dm = (DataMapper) ObjenesisHelper.newInstance(Class.forName(bd.getBeanClassName())); cache.put(dm.type(), dm); log.debug("Found data mapper:: Type ["+dm.type()+"] \t"+dm.getClass()); } catch (ClassNotFoundException e) { throw new BeanCreationException("Unable to instantiate data mapper class", e); } } }
/** * * @param basePkg * @return * @throws ClassNotFoundException */ public static Collection<Class<?>> findMapEntityClasses(String basePkg) throws ClassNotFoundException { ClassPathScanningCandidateComponentProvider provider= new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new TypeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { AnnotationMetadata aMeta = metadataReader.getAnnotationMetadata(); return aMeta.hasAnnotation(HzMapConfig.class.getName()); } }); //consider the finder class to be in the root package Set<BeanDefinition> beans = null; try { beans = provider.findCandidateComponents(StringUtils.hasText(basePkg) ? basePkg : EntityFinder.class.getName().substring(0, EntityFinder.class.getName().lastIndexOf("."))); } catch (Exception e) { throw new IllegalArgumentException("Unable to scan for entities under base package", e); } Set<Class<?>> classes = new HashSet<>(); if (beans != null && !beans.isEmpty()) { classes = new HashSet<>(beans.size()); for (BeanDefinition bd : beans) { classes.add(Class.forName(bd.getBeanClassName())); } } else { log.warn(">> Did not find any key value entities under the given base scan package ["+basePkg+"]"); } return classes; }
/** * Check whether any of the configured entity type filters matches * the current class descriptor contained in the metadata reader. */ protected boolean matchesFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException { for (TypeFilter filter : ENTITY_TYPE_FILTERS) { if (filter.match(reader, readerFactory)) { return true; } } return false; }
/** * Configures parent scanner to search for the right interfaces. It can * search for all interfaces or just for those that extends a * markerInterface or/and those annotated with the annotationClass */ public void registerFilters() { addIncludeFilter(new AnnotationTypeFilter(Mapper.class)); // exclude package-info.java addExcludeFilter(new TypeFilter() { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { String className = metadataReader.getClassMetadata() .getClassName(); return className.endsWith("package-info"); } }); }
/** * Find classes in the current class path that match the given <code>typeFilter</code> and reside in the given * <code>packages</code>. * * @param typeFilter The {@link TypeFilter} a class must satisfy to be considered. * @param packages The packages to look for classes in. At least one is required! * * @return The classes matching the given parameters. * * @throws IOException If reading resources or loading classes failed. */ public Set<Class<?>> findClasses(TypeFilter typeFilter, String... packages) throws IOException { Set<Class<?>> result = new HashSet<Class<?>>(); if (packages == null || packages.length == 0) { throw new IllegalArgumentException("At least one package is required"); } for (String eachPackage : packages) { result.addAll(this.scanForClasses(eachPackage, typeFilter)); } return result; }