public static Set<Class<?>> getClasses( String[] packageNames, boolean recursive, Class<? extends Annotation>[] annotations, final boolean skiperrors) { final AnnotationClassReader reader = new AnnotationClassReader(); for (Class<? extends Annotation> annotation : annotations) { reader.addAnnotation(annotation); } final Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); final ClassLoader loader = ClassLoaderUtils.getContextClassLoader(); FileFinder finder = new FileFinder() { @Override public void visitFileEntry(FileEntry file) { try { if (file.isJavaClass()) { if (reader.isAnnotationed(file.getInputStream())) { addClass(file.getQualifiedJavaName()); } } } catch (IOException e) { throw new RuntimeException(e); } } private void addClass(String qualifiedClassName) { try { Class<?> klass = loader.loadClass(qualifiedClassName); classes.add(klass); } catch (ClassNotFoundException e) { } catch (Throwable e) { if (skiperrors) { log.warn("Class load error.", e); } else { if (e instanceof RuntimeException) { throw (RuntimeException) e; } else if (e instanceof Error) { throw (Error) e; } else { throw new RuntimeException(e); } } } } }; finder.lookupClasspath(packageNames, recursive); return classes; }
private AntiSamy initHtmlScannerByPolicyFileName(String policyFileName) { AntiSamy scanner = null; String policyFilePath = FileFinder.findPath(policyFileName); if (StringUtils.isBlank(policyFilePath)) { throw new HtmlScannerException( "Policy file not found by file name [" + policyFileName + "]."); } else { Policy policy = null; try { policy = Policy.getInstance(policyFilePath); } catch (PolicyException ex) { throw new HtmlScannerException(ex); } if (policy != null) { scanner = new AntiSamy(policy); AntiSamy actualScanner = policyFileNamesAndSacnners.putIfAbsent(policyFileName, scanner); boolean wasScannerInitedByAnotherThread = actualScanner != null; if (wasScannerInitedByAnotherThread) { scanner = actualScanner; } } } return scanner; }
public static void main(String[] args) { File startingDirectory = new File("/home/myname"); FileFinder finder = new FileFinder(startingDirectory); finder.find(".java"); }