public static String fullClassNameFromJarEntry(JarEntry entry) { if (entry == null || isBlank(entry.getName())) { return null; } String s = entry.getName(); return getClassNameFromPath(s); }
public int complete(String buffer, int cursor, List<CharSequence> candidates) { candidates.addAll(classFinder.findFQClassesStartingWith(buffer)); return 0; } }
public Set<String> findClassesInPackageByName(String packageName, String className) { Set<String> matches = new TreeSet<String>(); if (!isBlank(packageName)) { for (String match : findClassesByName(className)) { if (match.startsWith(packageName)) { matches.add(match); } } } else { matches.addAll(findClassesByName(className)); } return matches; }
protected List<String> findAllClassesInClasspath() { ArrayList<String> classes = new ArrayList<String>(); for (File file : getClasspathLocationsThatExist()) { if (file.isDirectory()) { classes.addAll(getClassesInDirectory(file)); } else if (file.getName().endsWith(".jar")) { classes.addAll(getClassesInJar(file)); } } return classes; }
public final List<File> getClasspathLocationsThatExist() { List<File> locations = new ArrayList<File>(); ClassLoader currentClassLoader = this.getClass().getClassLoader(); while (currentClassLoader.getParent() != null && currentClassLoader.getParent() != currentClassLoader) { logger.debug("Looking at class loader of type: " + currentClassLoader.getClass()); if (currentClassLoader instanceof URLClassLoader) { locations.addAll(getLocationsThatExistFromURLClassLoader((URLClassLoader) currentClassLoader)); } else if (currentClassLoader == ClassLoader.getSystemClassLoader()) { locations.addAll(getFilesFromClassPath(System.getProperty("java.class.path"))); } else { throw new RuntimeException(currentClassLoader.getClass().getName()); } currentClassLoader = currentClassLoader.getParent(); } locations.addAll(getFilesFromClassPath(getBootClassPath())); if (logger.isInfoEnabled()) { for (File file : locations) { logger.info(String.format("Adding %s to class finder.", file)); } } return locations; }
public Set<String> findClassesInPackageByGlob(String packageName, String glob) { Set<String> matches = new TreeSet<String>(); if (!isBlank(packageName)) { for (String match : findClassesByGlob(glob)) { if (match.startsWith(packageName)) { matches.add(match); } } } else { matches.addAll(findClassesByGlob(glob)); } return matches; }
public int complete(String buffer, int cursor, List<CharSequence> candidates) { if (buffer.contains(" ")) { int matchStart = buffer.lastIndexOf(" ") + 1; String classNamePrefix = buffer.substring(matchStart); String packageName = buffer.substring(0, buffer.indexOf(" ")); NavigableMap<String, NavigableSet<String>> classesByPackageName = classFinder.getClassesByPackageName(); if (classesByPackageName.containsKey(packageName)) { candidates.addAll(classesByPackageName.get(packageName).subSet(classNamePrefix, classNamePrefix + "\uffff")); } return matchStart; } else { candidates.addAll(classFinder.findPackagesStartingWith(buffer)); return 0; } } }
matches = classFinder.findClassesInPackageByGlob(packageName, className); } else { matches = classFinder.findClassesInPackageByName(packageName, className);
public Set<String> findClassesByGlob(String glob) { Set<String> matches = new TreeSet<String>(); String regex = convertGlobToRegEx(glob); for (String className : packagesByClassName.keySet()) { if (className.matches(regex)) { for (String packageName : packagesByClassName.get(className)) { matches.add(packageName + "." + className); } } } return matches; }
private List<File> getFilesFromClassPath(String classpath) { List<File> locations = new ArrayList<File>(); if (isBlank(classpath)) { return locations; } for (String path : classpath.split(System.getProperty("path.separator"))) { File file = new File(path); if (!file.exists()) { continue; } locations.add(file); } return locations; }
private List<String> getClassesInJar(File file) { logger.debug("Getting classes in jar file " + file); List<String> classes = new ArrayList<String>(); try { JarFile jar = new JarFile(file); // TODO: handle items added to classpath by the jar // String jarClassPath = jar.getManifest().getMainAttributes().getValue("Class-Path"); Enumeration<JarEntry> e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); if (entry.isDirectory()) { if (entry.getName().toUpperCase().equals("META-INF/")) continue; } else if (entry.getName().endsWith(".class")) { String className = fullClassNameFromJarEntry(entry); classes.add(className); } } } catch (IOException e) { e.printStackTrace(); return classes; } return classes; }
public List<String> getClassesInDirectory(File startingWithDirectory) { logger.debug("Getting classes in directory " + startingWithDirectory.getAbsolutePath()); List<String> classes = new ArrayList<String>(); Set<String> seen = new HashSet<String>(); seen.add(startingWithDirectory.getAbsolutePath()); Queue<File> directoriesToScan = new ArrayDeque<File>(); directoriesToScan.add(startingWithDirectory); File directory; while ((directory = directoriesToScan.poll()) != null) { File[] files = directory.listFiles(); for (File file : files) { if (file.isFile() && file.getName().endsWith(".class")) { String pathToClass = file.getAbsolutePath().replace(startingWithDirectory.getAbsolutePath(), ""); String className = getClassNameFromPath(pathToClass); logger.debug("Found " + className); classes.add(className); } else if (file.isDirectory() && !seen.contains(file.getAbsolutePath())) { seen.add(file.getAbsolutePath()); directoriesToScan.add(file); } } } return classes; }
for (String fullClassName : findAllClassesInClasspath()) { int lastIndex = fullClassName.lastIndexOf(".");