/** * This method gets the {@link ReflectionUtil}. * * @return the {@link ReflectionUtil}. */ public ReflectionUtil getReflectionUtil() { if (this.reflectionUtil == null) { this.reflectionUtil = ReflectionUtilImpl.getInstance(); } return this.reflectionUtil; }
@Override public Set<String> findClassNames(String packageName, boolean includeSubPackages, Filter<? super String> filter) { Set<String> result = new HashSet<>(); findClassNames(packageName, includeSubPackages, result, filter, getDefaultClassLoader(filter.getClass())); return result; }
/** * This method gets the singleton instance of this {@link ReflectionUtil}. <br> * <b>ATTENTION:</b><br> * Please prefer dependency-injection instead of using this method. * * @return the singleton instance. */ public static ReflectionUtil getInstance() { if (instance == null) { ReflectionUtilImpl impl = null; synchronized (ReflectionUtilImpl.class) { if (instance == null) { impl = new ReflectionUtilImpl(); instance = impl; } } if (impl != null) { // static access is generally discouraged, cyclic dependency forces this ugly initialization to // prevent deadlock. impl.initialize(); } } return instance; }
@Override public Set<String> findResourceNames(String packageName, boolean includeSubPackages, Filter<? super String> filter) { return findResourceNames(packageName, includeSubPackages, filter, getDefaultClassLoader(filter.getClass())); }
private void visitResourceClassloader(boolean includeSubPackages, ClassLoader classLoader, ResourceVisitor visitor, String pathWithPrefix, StringBuilder qualifiedNameBuilder, int qualifiedNamePrefixLength, Set<String> urlSet) throws IOException { if (classLoader instanceof URLClassLoader) { URLClassLoader urlClassLoader = (URLClassLoader) classLoader; for (URL url : urlClassLoader.getURLs()) { String file = BasicHelper.toLowerCase(url.getFile()); if (file.endsWith(FILE_EXTENSION_JAR)) { URL jarUrl = createJarUrl(url.toString()); visitResourceUrl(includeSubPackages, visitor, pathWithPrefix, qualifiedNameBuilder, qualifiedNamePrefixLength, jarUrl, urlSet); } else { visitResourceUrl(includeSubPackages, visitor, pathWithPrefix, qualifiedNameBuilder, qualifiedNamePrefixLength, url, urlSet); } } } ClassLoader parent = classLoader.getParent(); if (parent != null) { visitResourceClassloader(includeSubPackages, parent, visitor, pathWithPrefix, qualifiedNameBuilder, qualifiedNamePrefixLength, urlSet); } }
/** * This method gets the default {@link ClassLoader} to use. This should be the {@link Thread#getContextClassLoader() * ContextClassLoader} but falls back to alternatives if no such {@link ClassLoader} is available. * * @return the default {@link ClassLoader} to use. */ protected ClassLoader getDefaultClassLoader() { return getDefaultClassLoader(null); }
@Override public Set<String> findResourceNames(String packageName, boolean includeSubPackages, Filter<? super String> filter, ClassLoader classLoader) { Set<String> result = new HashSet<>(); ResourceNameCollector visitor = new ResourceNameCollector(result, filter); visitResourceNames(packageName, includeSubPackages, classLoader, visitor); return result; }
private void visitResourceClasspath(boolean includeSubPackages, ResourceVisitor visitor, StringBuilder qualifiedNameBuilder, String pathWithPrefix, int qualifiedNamePrefixLength, Set<String> urlSet) throws MalformedURLException, UnsupportedEncodingException, IOException { String classpath = System.getProperty(PROPERTY_JAVA_CLASS_PATH); String separator = System.getProperty(PROPERTY_PATH_SEPARATOR); String[] entries = classpath.split(separator); for (String entry : entries) { File file = new File(entry); URL url; if (BasicHelper.toLowerCase(file.getName()).endsWith(FILE_EXTENSION_JAR)) { url = createJarUrl(URL_PREFIX_FILE + file.getAbsolutePath()); } else { url = file.toURI().toURL(); } visitResourceUrl(includeSubPackages, visitor, pathWithPrefix, qualifiedNameBuilder, qualifiedNamePrefixLength, url, urlSet); } }
@Override public GenericType<?> createGenericType(Type type, Class<?> definingType) { return createGenericType(type, createGenericType(definingType)); }
@Bean public ReflectionUtil reflectionUtil() { return new ReflectionUtilImpl(); }
@Override public Set<DataResource> findResources(String packageName, boolean includeSubPackages, Filter<? super String> filter) { return findResources(packageName, includeSubPackages, filter, this.reflectionUtil.getDefaultClassLoader(filter.getClass())); }
@Override public Set<DataResource> findResources(String packageName, boolean includeSubPackages, Filter<? super String> filter, ClassLoader classLoader) { Set<DataResource> result = new HashSet<>(); ResourceVisitor visitor = new ResourceCollector(result, filter); this.reflectionUtil.visitResourceNames(packageName, includeSubPackages, classLoader, visitor); return result; }
@Override @SuppressWarnings("rawtypes") public GenericType<?> createGenericType(Type type) { if (type instanceof GenericType) { return (GenericType<?>) type; } else if (type instanceof Class) { return createGenericType((Class<?>) type); } else { return new GenericTypeImpl(type); } }
@Override protected void doInitialize() { super.doInitialize(); if (this.reflectionUtil == null) { this.reflectionUtil = ReflectionUtilImpl.getInstance(); } }
@Override public void findClassNames(String packageName, boolean includeSubPackages, Set<String> classSet) { Filter<String> filter = ConstantFilter.getInstance(true); findClassNames(packageName, includeSubPackages, classSet, filter, getDefaultClassLoader()); }
@Override public Set<DataResource> findResources(String absoluteClasspath) { try { Set<DataResource> result = new HashSet<>(); Enumeration<URL> resourceUrlEnumeration = this.reflectionUtil.getDefaultClassLoader(getClass()).getResources(absoluteClasspath); while (resourceUrlEnumeration.hasMoreElements()) { URL url = resourceUrlEnumeration.nextElement(); result.add(new UrlResource(url)); } return result; } catch (IOException e) { throw new IllegalStateException("Error reading resources.", e); } }
/** * @see #findClassNames(String, boolean, Filter, ClassLoader) * * @param packageName is the name of the {@link Package} to scan. * @param includeSubPackages - if {@code true} all sub-packages of the specified {@link Package} will be included in * the search. * @param classSet is where to add the classes. * @param filter is used to {@link Filter#accept(Object) filter} the {@link Class}-names to be added to the resulting * {@link Set}. The {@link Filter} will receive {@link Class#getName() fully qualified class-names} as argument * (e.g. "net.sf.mmm.reflect.api.ReflectionUtil"). * @param classLoader is the explicit {@link ClassLoader} to use. * @if the operation failed with an I/O error. */ protected void findClassNames(String packageName, boolean includeSubPackages, Set<String> classSet, Filter<? super String> filter, ClassLoader classLoader) { ResourceVisitor visitor = new ClassNameCollector(classSet, filter); visitResourceNames(packageName, includeSubPackages, classLoader, visitor); }
@Override protected void doInitialize() { super.doInitialize(); if (this.reflectionUtil == null) { this.reflectionUtil = ReflectionUtilImpl.getInstance(); } }
@Override protected void doInitialize() { super.doInitialize(); if (this.reflectionUtil == null) { this.reflectionUtil = (ReflectionUtilImpl) ReflectionUtilImpl.getInstance(); } }
@Override public String toString() { String upperBoundString = ReflectionUtilImpl.getInstance().toString(this.lowerBound); // "? super ".length == 8 StringBuilder result = new StringBuilder(upperBoundString.length() + 8); result.append(PREFIX); result.append(upperBoundString); return result.toString(); }