📄 classfinder.java
字号:
}
private static String[] fixDotDirs(String[] paths) {
for (int i = 0; i < paths.length; i++) {
paths[i] = fixDotDir(paths[i]);
}
return paths;
}
private static String fixDotDir(String path) {
if (path == null)
return null;
if (path.equals(".")) {
return System.getProperty("user.dir");
} else {
return path.trim();
}
}
private static String[] fixEndingSlashes(String[] strings) {
String[] strNew = new String[strings.length];
for (int i = 0; i < strings.length; i++) {
strNew[i] = fixEndingSlashes(strings[i]);
}
return strNew;
}
private static String fixEndingSlashes(String string) {
if (string.endsWith("/") || string.endsWith("\\")) {
string = string.substring(0, string.length() - 1);
string = fixEndingSlashes(string);
}
return string;
}
private static String[] fixSlashes(String[] strings) {
String[] strNew = new String[strings.length];
for (int i = 0; i < strings.length; i++) {
strNew[i] = fixSlashes(strings[i]) /* .toLowerCase() */;
}
return strNew;
}
private static String fixSlashes(final String str) {
// replace \ with /
String newStr = str.replace('\\', '/');
// compress multiples into singles;
// do in 2 steps with dummy string
// to avoid infinte loop
newStr = replaceString(newStr, "//", "_____");
newStr = replaceString(newStr, "_____", "/");
return newStr;
}
private static String replaceString(String s, String strToFind, String strToReplace) {
int index;
int currentPos;
StringBuffer buffer = null;
if (s.indexOf(strToFind) == -1) {
return s;
}
currentPos = 0;
buffer = new StringBuffer();
while (true) {
index = s.indexOf(strToFind, currentPos);
if (index == -1) {
break;
}
buffer.append(s.substring(currentPos, index));
buffer.append(strToReplace);
currentPos = index + strToFind.length();
}
buffer.append(s.substring(currentPos));
return buffer.toString();
}
/**
* Finds all classes that extend the classes in the listSuperClasses ArrayList, searching in the listAllClasses ArrayList.
*
* @param listSuperClasses the base classes to find subclasses for
* @param listAllClasses the collection of classes to search in
* @param innerClasses indicate whether to include inner classes in the search
* @return ArrayList of the subclasses
*/
private static ArrayList findAllSubclasses(List listSuperClasses, List listAllClasses, boolean innerClasses) {
Iterator iterClasses = null;
ArrayList listSubClasses = null;
String strClassName = null;
Class tempClass = null;
listSubClasses = new ArrayList();
iterClasses = listSuperClasses.iterator();
while (iterClasses.hasNext()) {
strClassName = (String) iterClasses.next();
// only check classes if they are not inner classes
// or we intend to check for inner classes
if ((strClassName.indexOf("$") == -1) || innerClasses) {
// might throw an exception, assume this is ignorable
try {
tempClass = Class.forName(strClassName, false, Thread.currentThread().getContextClassLoader());
findAllSubclassesOneClass(tempClass, listAllClasses, listSubClasses, innerClasses);
// call by reference - recursive
}
catch (Throwable ignored) {
}
}
}
return listSubClasses;
}
/**
* Finds all classes that extend the class, searching in the listAllClasses ArrayList.
*
* @param theClass the parent class
* @param listAllClasses the collection of classes to search in
* @param listSubClasses the collection of discovered subclasses
* @param innerClasses indicates whether inners classes should be included in the search
*/
private static void findAllSubclassesOneClass(Class theClass, List listAllClasses, List listSubClasses, boolean innerClasses) {
Iterator iterClasses = null;
String strClassName = null;
Class c = null;
boolean bIsSubclass = false;
iterClasses = listAllClasses.iterator();
while (iterClasses.hasNext()) {
strClassName = (String) iterClasses.next();
// only check classes if they are not inner classes
// or we intend to check for inner classes
if ((strClassName.indexOf("$") == -1) || innerClasses) {
// might throw an exception, assume this is ignorable
try {
c = Class.forName(strClassName, false, Thread.currentThread().getContextClassLoader());
if (!c.isInterface() && !Modifier.isAbstract(c.getModifiers())) {
bIsSubclass = theClass.isAssignableFrom(c);
} else {
bIsSubclass = false;
}
if (bIsSubclass) {
listSubClasses.add(strClassName);
}
}
catch (Throwable ignored) {
}
}
}
}
/**
* Converts a class file from the text stored in a Jar file to a version that can be used in Class.forName().
*
* @param strClassName the class name from a Jar file
* @return String the Java-style dotted version of the name
*/
private static String fixClassName(String strClassName) {
strClassName = strClassName.replace('\\', '.');
strClassName = strClassName.replace('/', '.');
strClassName = strClassName.substring(0, strClassName.length() - 6);
// remove ".class"
return strClassName;
}
private static void findClassesInOnePath(String strPath, List listClasses) throws IOException {
File file = null;
ZipFile zipFile = null;
Enumeration entries = null;
String strEntry = null;
file = new File(strPath);
if (file.isDirectory()) {
findClassesInPathsDir(strPath, file, listClasses);
} else if (file.exists()) {
try {
zipFile = new ZipFile(file);
entries = zipFile.entries();
while (entries.hasMoreElements()) {
strEntry = entries.nextElement().toString();
if (strEntry.endsWith(".class")) {
listClasses.add(fixClassName(strEntry));
}
}
}
catch (ZipException e) {
log.debug("Error opening zip " + file + ". Skipping this one", e);
}
}
}
private static void findClassesInPaths(List listPaths, List listClasses) throws IOException {
Iterator iterPaths = listPaths.iterator();
while (iterPaths.hasNext()) {
findClassesInOnePath((String) iterPaths.next(), listClasses);
}
}
private static void findClassesInPathsDir(String strPathElement, File dir, List listClasses) throws IOException {
File file = null;
String[] list = dir.list();
for (int i = 0; i < list.length; i++) {
file = new File(dir, list[i]);
if (file.isDirectory()) {
findClassesInPathsDir(strPathElement, file, listClasses);
} else if (file.exists() && (file.length() != 0) && list[i].endsWith(".class")) {
listClasses.add(file.getPath().substring(strPathElement.length() + 1, file.getPath().lastIndexOf(".")).replace(
File.separator.charAt(0), '.'));
}
}
}
/**
* @return Returns the javaClassPath.
*/
public static String getJavaClassPath() {
return javaClassPath;
}
/**
* @param thisJavaClassPath The javaClassPath to set.
*/
public static void setJavaClassPath(String thisJavaClassPath) {
ClassFinder.javaClassPath = thisJavaClassPath;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -