public TypeElementsScanner includeAnnotations() { return includeAnnotations(true); } public TypeElementsScanner includeAnnotations(boolean include) { includeAnnotations = include; return this; }
public TypeElementsScanner includeFields() { return includeFields(true); } public TypeElementsScanner includeFields(boolean include) { includeFields = include; return this; }
public TypeElementsScanner includeMethods() { return includeMethods(true); } public TypeElementsScanner includeMethods(boolean include) { includeMethods = include; return this; }
public void scan(Object cls) { String className = getMetadataAdapter().getClassName(cls); if (!acceptResult(className)) return; getStore().put(className, ""); if (includeFields) { for (Object field : getMetadataAdapter().getFields(cls)) { String fieldName = getMetadataAdapter().getFieldName(field); getStore().put(className, fieldName); } } if (includeMethods) { for (Object method : getMetadataAdapter().getMethods(cls)) { if (!publicOnly || getMetadataAdapter().isPublic(method)) { String methodKey = getMetadataAdapter().getMethodName(method) + "(" + Joiner.on(", ").join(getMetadataAdapter().getParameterNames(method)) + ")"; getStore().put(className, methodKey); } } } if (includeAnnotations) { for (Object annotation : getMetadataAdapter().getClassAnnotationNames(cls)) { getStore().put(className, "@" + annotation); } } }
/** * Add all the classes with a package prefix in the scope. * @param packageName the root package * @return the selector */ public Selector from(final String packageName) { Assertion.checkArgNotEmpty(packageName); checkScope(); // --- new Reflections(packageName, new TypeElementsScanner().includeAnnotations(false).includeFields(false).includeMethods(false)) .getStore() .get(TypeElementsScanner.class.getSimpleName()) .keys() .forEach(className -> from(ClassUtil.classForName(className))); return this; }
conf.setScanners(new TypeElementsScanner(), new TypeAnnotationsScanner());
public TypeElementsScanner publicOnly() { return publicOnly(true); } }
/** * Locate properties class for type class. * * @param type the type * @return the class */ public Class locatePropertiesClassForType(final ClassOrInterfaceType type) { if (cachedPropertiesClasses.containsKey(type.getNameAsString())) { return cachedPropertiesClasses.get(type.getNameAsString()); } val packageName = ConfigurationMetadataGenerator.class.getPackage().getName(); val reflections = new Reflections(new ConfigurationBuilder() .filterInputsBy(s -> s.contains(type.getNameAsString())) .setUrls(ClasspathHelper.forPackage(packageName)) .setScanners(new TypeElementsScanner() .includeFields(false) .includeMethods(false) .includeAnnotations(false) .filterResultsBy(s -> s.endsWith(type.getNameAsString())), new SubTypesScanner(false))); val clz = reflections.getSubTypesOf(Serializable.class).stream() .filter(c -> c.getSimpleName().equalsIgnoreCase(type.getNameAsString())) .findFirst() .orElseThrow(() -> new IllegalArgumentException("Cant locate class for " + type.getNameAsString())); cachedPropertiesClasses.put(type.getNameAsString(), clz); return clz; } }
public void scan(Object cls) { String className = getMetadataAdapter().getClassName(cls); if (!acceptResult(className)) return; getStore().put(className, ""); if (includeFields) { for (Object field : getMetadataAdapter().getFields(cls)) { String fieldName = getMetadataAdapter().getFieldName(field); getStore().put(className, fieldName); } } if (includeMethods) { for (Object method : getMetadataAdapter().getMethods(cls)) { if (!publicOnly || getMetadataAdapter().isPublic(method)) { String methodKey = getMetadataAdapter().getMethodName(method) + "(" + Joiner.on(", ").join(getMetadataAdapter().getParameterNames(method)) + ")"; getStore().put(className, methodKey); } } } if (includeAnnotations) { for (Object annotation : getMetadataAdapter().getClassAnnotationNames(cls)) { getStore().put(className, "@" + annotation); } } }
Reflections ref = new Reflections(new ConfigurationBuilder().setScanners(new SubTypesScanner(false), new ResourcesScanner(), new TypeElementsScanner()) ... Set<String> typeSet = reflections.getStore().getStoreMap().get("TypeElementsScanner").keySet(); HashSet<Class<? extends Object>> classes = Sets.newHashSet(ReflectionUtils.forNames(typeSet, reflections .getConfiguration().getClassLoaders()));
public TypeElementsScanner publicOnly() { return publicOnly(true); } }
public void scan(Object cls) { String className = getMetadataAdapter().getClassName(cls); if (!acceptResult(className)) return; getStore().put(className, ""); if (includeFields) { for (Object field : getMetadataAdapter().getFields(cls)) { String fieldName = getMetadataAdapter().getFieldName(field); getStore().put(className, fieldName); } } if (includeMethods) { for (Object method : getMetadataAdapter().getMethods(cls)) { if (!publicOnly || getMetadataAdapter().isPublic(method)) { String methodKey = getMetadataAdapter().getMethodName(method) + "(" + Joiner.on(", ").join(getMetadataAdapter().getParameterNames(method)) + ")"; getStore().put(className, methodKey); } } } if (includeAnnotations) { for (Object annotation : getMetadataAdapter().getClassAnnotationNames(cls)) { getStore().put(className, "@" + annotation); } } }
private static Collection<? extends Class<?>> getPackageTypes(String packageToPase, Collection<URL> urls) { Set<Class<?>> classes = new HashSet<>(); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* exclude Object.class */), new ResourcesScanner(), new TypeElementsScanner()) .setUrls(urls) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageToPase)).exclude("java.*"))); Set<String> types; types = reflections.getStore().get("TypeElementsScanner").keySet(); for (String type: types) { Class<?> aClass = TypesHelper.loadClass(type, CLASS_LOADER); boolean wantedElement = StringUtils.startsWith(type, packageToPase); if (null != aClass && wantedElement) { logger.log(Level.INFO, "looking up for type: " + type); classes.add(aClass); } } return classes; }
public TypeElementsScanner includeAnnotations() { return includeAnnotations(true); } public TypeElementsScanner includeAnnotations(boolean include) { includeAnnotations = include; return this; }
public TypeElementsScanner includeMethods() { return includeMethods(true); } public TypeElementsScanner includeMethods(boolean include) { includeMethods = include; return this; }
public TypeElementsScanner includeFields() { return includeFields(true); } public TypeElementsScanner includeFields(boolean include) { includeFields = include; return this; }
public TypeElementsScanner publicOnly() { return publicOnly(true); }
public void scan(Object cls) { String className = getMetadataAdapter().getClassName(cls); if (!acceptResult(className)) return; getStore().put(className, ""); if (includeFields) { for (Object field : getMetadataAdapter().getFields(cls)) { String fieldName = getMetadataAdapter().getFieldName(field); getStore().put(className, fieldName); } } if (includeMethods) { for (Object method : getMetadataAdapter().getMethods(cls)) { if (!publicOnly || getMetadataAdapter().isPublic(method)) { String methodKey = getMetadataAdapter().getMethodName(method) + "(" + Joiner.on(", ").join(getMetadataAdapter().getParameterNames(method)) + ")"; getStore().put(className, methodKey); } } } if (includeAnnotations) { for (Object annotation : getMetadataAdapter().getClassAnnotationNames(cls)) { getStore().put(className, "@" + annotation); } } }
/** * Create {@link org.reflections.Configuration} object for given package name and class loader. * * @param packageName the root package to scan * @param classLoader class loader to be used in reflections * @return */ public static ConfigurationBuilder getReflectionsConfiguration(String packageName, ClassLoader classLoader) { return new ConfigurationBuilder() .addUrls(ClasspathHelper.forPackage(packageName, classLoader)) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName + "."))) .setScanners(new TypeElementsScanner(), new TypeAnnotationsScanner(), new SubTypesScanner()); }
public TypeElementsScanner includeAnnotations() { return includeAnnotations(true); }