private Set<Class<?>> getSubtypes(final Class<?> type) throws ComboBoxIsTooFullException { final ClassLoader[] classloaders = new ClassLoader[] { currentModelHandler.getCustomClassLoader(), currentModelHandler.getCustomClassLoader().getParent() }; final ConfigurationBuilder builder = new ConfigurationBuilder(). addUrls(ClasspathHelper.forClassLoader(currentModelHandler.getCustomClassLoader())). addClassLoaders(classloaders). setScanners(new SubTypesScanner(false)); final Reflections ref = new Reflections(builder); final Set<Class<?>> result = new HashSet<Class<?>>(); final Set<String> subTypesStr = ref.getStore().getSubTypesOf(type.getName()); if (subTypesStr.size() > MAX_SIZE_OF_SUBPARAMETER_COMBOBOX) throw new ComboBoxIsTooFullException(); for (final String clsName : subTypesStr) { try { result.add(ReflectionUtils.forName(clsName,classloaders)); } catch (final ReflectionsException e) { // intentionally blank } } return result; }
/** tries to create a Dir from the given url, using the given urlTypes*/ public static Dir fromURL(final URL url, final List<UrlType> urlTypes) { for (UrlType type : urlTypes) { try { if (type.matches(url)) { Dir dir = type.createDir(url); if (dir != null) return dir; } } catch (Throwable e) { /*if (Reflections.log != null) { Reflections.log.warn("could not create Dir using " + type + " from url " + url.toExternalForm() + ". skipping.", e); }*/ } } throw new ReflectionsException("could not create Vfs.Dir from url, no matching UrlType was found [" + url.toExternalForm() + "]\n" + "either use fromURL(final URL url, final List<UrlType> urlTypes) or " + "use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) " + "with your specialized UrlType."); }
/** tries to create a Dir from the given url, using the given urlTypes*/ public static Dir fromURL(final URL url, final List<UrlType> urlTypes) { for (UrlType type : urlTypes) { try { if (type.matches(url)) { Dir dir = type.createDir(url); if (dir != null) return dir; } } catch (Throwable e) { if (Reflections.log != null) { Reflections.log.warn("could not create Dir using " + type + " from url " + url.toExternalForm() + ". skipping.", e); } } } throw new ReflectionsException("could not create Vfs.Dir from url, no matching UrlType was found [" + url.toExternalForm() + "]\n" + "either use fromURL(final URL url, final List<UrlType> urlTypes) or " + "use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) " + "with your specialized UrlType."); }
public static Method resolveMethod(final Class aMethod) { String methodOgnl = aMethod.getSimpleName(); try { String methodName; Class<?>[] paramTypes; if (methodOgnl.contains(tokenSeparator)) { methodName = methodOgnl.substring(0, methodOgnl.indexOf(tokenSeparator)); String[] params = methodOgnl.substring(methodOgnl.indexOf(tokenSeparator) + 1).split(doubleSeparator); paramTypes = new Class<?>[params.length]; for (int i = 0; i < params.length; i++) { String typeName = params[i].replace(arrayDescriptor, "[]").replace(pathSeparator, dotSeparator); paramTypes[i] = ReflectionUtils.forName(typeName); } } else { methodName = methodOgnl; paramTypes = null; } Class<?> declaringClass = aMethod.getDeclaringClass().getDeclaringClass(); return resolveClassOf(declaringClass).getDeclaredMethod(methodName, paramTypes); } catch (Exception e) { throw new ReflectionsException("could not resolve to method " + aMethod.getName(), e); } }
@Override public Object scan(Vfs.File file, Object classObject) { if (classObject == null) { try { classObject = configuration.getMetadataAdapter().getOfCreateClassObject(file); } catch (Exception e) { throw new ReflectionsException("could not create class object from file " + file.getRelativePath()); } } scan(classObject); return classObject; }
PackageScope(JPackage pack, GlobalScope parent) { super(pack.name(), parent); this.pack = pack; Reflections reflections = new Reflections(name(), new SubTypesScanner(false)); try { for (String s : reflections.getAllTypes()) { Optional<JClass> ojc = Optional.absent(); externalTypes.put(s, ojc); } } catch (ReflectionsException ex) { // it's ok for this to happen...a new package will indeed be empty Logger.warn("No types in package " + name()); } }
URL replaceZipSeparators(String path, Predicate<File> acceptFile) throws MalformedURLException { int pos = 0; while (pos != -1) { pos = findFirstMatchOfDeployableExtention(path, pos); if (pos > 0) { File file = new File(path.substring(0, pos - 1)); if (acceptFile.apply(file)) { return replaceZipSeparatorStartingFrom(path, pos); } } } throw new ReflectionsException("Unable to identify the real zip file in path '" + path + "'."); }
public Object scan(Vfs.File file, Object classObject) { if (classObject == null) { try { classObject = configuration.getMetadataAdapter().getOrCreateClassObject(file); } catch (Exception e) { throw new ReflectionsException("could not create class object from file " + file.getRelativePath(), e); } } scan(classObject); return classObject; }
public ClassFile getOrCreateClassObject(final Vfs.File file) { InputStream inputStream = null; try { inputStream = file.openInputStream(); DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream)); return new ClassFile(dis); } catch (IOException e) { throw new ReflectionsException("could not create class file from " + file.getName(), e); } finally { Utils.close(inputStream); } }
/** * Parses a string representation of an include/exclude filter. * <p> * The given includeExcludeString is a comma separated list of regexes, * each starting with either + or - to indicate include/exclude. * <p> * For example parsePackages("-java\\..*, -javax\\..*, -sun\\..*, -com\\.sun\\..*") * or parse("+com\\.myn\\..*,-com\\.myn\\.excluded\\..*"). * Note that "-java\\..*" will block "java.foo" but not "javax.foo". * <p> * See also the more useful {@link FilterBuilder#parsePackages(String)} method. */ public static FilterBuilder parse(String includeExcludeString) { List<Predicate<String>> filters = new ArrayList<Predicate<String>>(); if (!Utils.isEmpty(includeExcludeString)) { for (String string : includeExcludeString.split(",")) { String trimmed = string.trim(); char prefix = trimmed.charAt(0); String pattern = trimmed.substring(1); Predicate<String> filter; switch (prefix) { case '+': filter = new Include(pattern); break; case '-': filter = new Exclude(pattern); break; default: throw new ReflectionsException("includeExclude should start with either + or -"); } filters.add(filter); } return new FilterBuilder(filters); } else { return new FilterBuilder(); } }
/** * Parses a string representation of an include/exclude filter. * <p> * The given includeExcludeString is a comma separated list of package name segments, * each starting with either + or - to indicate include/exclude. * <p> * For example parsePackages("-java, -javax, -sun, -com.sun") or parse("+com.myn,-com.myn.excluded"). * Note that "-java" will block "java.foo" but not "javax.foo". * <p> * The input strings "-java" and "-java." are equivalent. */ public static FilterBuilder parsePackages(String includeExcludeString) { List<Predicate<String>> filters = new ArrayList<Predicate<String>>(); if (!Utils.isEmpty(includeExcludeString)) { for (String string : includeExcludeString.split(",")) { String trimmed = string.trim(); char prefix = trimmed.charAt(0); String pattern = trimmed.substring(1); if (pattern.endsWith(".") == false) { pattern += "."; } pattern = prefix(pattern); Predicate<String> filter; switch (prefix) { case '+': filter = new Include(pattern); break; case '-': filter = new Exclude(pattern); break; default: throw new ReflectionsException("includeExclude should start with either + or -"); } filters.add(filter); } return new FilterBuilder(filters); } else { return new FilterBuilder(); } }
public static Member getMemberFromDescriptor(String descriptor, ClassLoader... classLoaders) throws ReflectionsException { int p0 = descriptor.lastIndexOf('('); String memberKey = p0 != -1 ? descriptor.substring(0, p0) : descriptor; String methodParameters = p0 != -1 ? descriptor.substring(p0 + 1, descriptor.lastIndexOf(')')) : ""; int p1 = Math.max(memberKey.lastIndexOf('.'), memberKey.lastIndexOf("$")); String className = memberKey.substring(memberKey.lastIndexOf(' ') + 1, p1); String memberName = memberKey.substring(p1 + 1); Class<?>[] parameterTypes = null; if (!isEmpty(methodParameters)) { String[] parameterNames = methodParameters.split(","); List<Class<?>> result = new ArrayList<Class<?>>(parameterNames.length); for (String name : parameterNames) { result.add(forName(name.trim(), classLoaders)); } parameterTypes = result.toArray(new Class<?>[result.size()]); } Class<?> aClass = forName(className, classLoaders); while (aClass != null) { try { if (!descriptor.contains("(")) { return aClass.isInterface() ? aClass.getField(memberName) : aClass.getDeclaredField(memberName); } else if (isConstructor(descriptor)) { return aClass.isInterface() ? aClass.getConstructor(parameterTypes) : aClass.getDeclaredConstructor(parameterTypes); } else { return aClass.isInterface() ? aClass.getMethod(memberName, parameterTypes) : aClass.getDeclaredMethod(memberName, parameterTypes); } } catch (Exception e) { aClass = aClass.getSuperclass(); } } throw new ReflectionsException("Can't resolve member named " + memberName + " for class " + className); }
public static Set<Member> getMembersFromDescriptors(Iterable<String> values, ClassLoader... classLoaders) { Set<Member> result = Sets.newHashSet(); for (String value : values) { try { result.add(Utils.getMemberFromDescriptor(value, classLoaders)); } catch (ReflectionsException e) { throw new ReflectionsException("Can't resolve member named " + value, e); } } return result; }
public static Field getFieldFromString(String field, ClassLoader... classLoaders) { String className = field.substring(0, field.lastIndexOf('.')); String fieldName = field.substring(field.lastIndexOf('.') + 1); try { return forName(className, classLoaders).getDeclaredField(fieldName); } catch (NoSuchFieldException e) { throw new ReflectionsException("Can't resolve field named " + fieldName, e); } }
public static Class<?> resolveClass(final Class aClass) { try { return resolveClassOf(aClass); } catch (Exception e) { throw new ReflectionsException("could not resolve to class " + aClass.getName(), e); } }
public static Field resolveField(final Class aField) { try { String name = aField.getSimpleName(); Class<?> declaringClass = aField.getDeclaringClass().getDeclaringClass(); return resolveClassOf(declaringClass).getDeclaredField(name); } catch (Exception e) { throw new ReflectionsException("could not resolve to field " + aField.getName(), e); } }
public static Annotation resolveAnnotation(Class annotation) { try { String name = annotation.getSimpleName().replace(pathSeparator, dotSeparator); Class<?> declaringClass = annotation.getDeclaringClass().getDeclaringClass(); Class<?> aClass = resolveClassOf(declaringClass); Class<? extends Annotation> aClass1 = (Class<? extends Annotation>) ReflectionUtils.forName(name); Annotation annotation1 = aClass.getAnnotation(aClass1); return annotation1; } catch (Exception e) { throw new ReflectionsException("could not resolve to annotation " + annotation.getName(), e); } }
public void execute() throws MojoExecutionException, MojoFailureException { // if (StringUtils.isEmpty(destinations)) { destinations = resolveOutputDirectory() + "/META-INF/reflections/" + getProject().getArtifactId() + "-reflections.xml"; } String outputDirectory = resolveOutputDirectory(); if (!new File(outputDirectory).exists()) { getLog().warn(String.format("Reflections plugin is skipping because %s was not found", outputDirectory)); return; } // ConfigurationBuilder config = new ConfigurationBuilder(); config.setUrls(parseUrls()); if (!isEmpty(includeExclude)) { config.filterInputsBy(FilterBuilder.parse(includeExclude)); } config.setScanners(!isEmpty(scanners) ? parseScanners() : new Scanner[]{new SubTypesScanner(), new TypeAnnotationsScanner()}); if (!isEmpty(serializer)) { try { Serializer serializerInstance = (Serializer) forName(serializer, "org.reflections.serializers").newInstance(); config.setSerializer(serializerInstance); if (serializerInstance instanceof JavaCodeSerializer) { int size = config.getScanners().size(); config.addScanners(new TypeElementsScanner()); if (size != config.getScanners().size()) { getLog().info("added type scanners for JavaCodeSerializer"); } } } catch (Exception ex) { throw new ReflectionsException("could not create serializer instance", ex); } } if (parallel != null && parallel.equals(Boolean.TRUE)) { config.useParallelExecutor(); } // if (Reflections.log == null) { try { Reflections.log = new MavenLogAdapter(getLog()); } catch (Error e) { //ignore } } Reflections reflections = new Reflections(config); reflections.save(destinations.trim()); }