📄 classfinder.java
字号:
//$Header: /java/zilverline/src/org/zilverline/util/ClassFinder.java,v 1.10 2006/04/19 08:06:23 mfranken Exp $
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.zilverline.util;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class finds classes that implement one or more specified interfaces.
*
* @author Burt Beckwith
* @author Michael Stover (mstover1 at apache.org)
* @author Michael Franken
* @version $Revision: 1.10 $
*/
public final class ClassFinder {
private static Log log = LogFactory.getLog(ClassFinder.class);
private static String javaClassPath = System.getProperty("java.class.path");
private ClassFinder() {
}
// static only
/**
* Convenience method for <code>findClassesThatExtend(Class[],
* boolean)</code> with the option to include inner classes in the
* search set to false.
*
* @return ArrayList containing discovered classes.
*/
public static List findClassesThatExtend(String[] paths, Class[] superClasses) throws IOException, ClassNotFoundException {
return findClassesThatExtend(paths, superClasses, false);
}
/**
* Convenience method that finds classes on the standard java classpath.
*
* @param superClasses
* @return list of discovered classes
* @throws IOException
* @throws ClassNotFoundException
*/
public static List findClassesThatExtend(Class[] superClasses) throws IOException, ClassNotFoundException {
log.debug("CLASSPATH: " + javaClassPath);
String[] paths = javaClassPath.split(File.pathSeparator);
return ClassFinder.findClassesThatExtend(paths, superClasses);
}
/**
* Convenience method that finds classes on the standard java classpath.
*
* @param superClass
* @return list of discovered classes
* @throws IOException
* @throws ClassNotFoundException
*/
public static List findClassesThatExtend(Class superClass) throws IOException, ClassNotFoundException {
log.debug("CLASSPATH: " + javaClassPath);
String paths[] = javaClassPath.split("" + File.pathSeparatorChar);
Class superClasses[] = new Class[1];
superClasses[0] = superClass;
return ClassFinder.findClassesThatExtend(paths, superClasses);
}
/**
* Convenience method to get a list of classes that can be instantiated.
*
* @param superclass an interface or base class
* @return Array of discovered classes
* @throws IOException
* @throws ClassNotFoundException
*/
public static Class[] getInstantiableSubclasses(Class superclass) throws IOException, ClassNotFoundException {
ArrayList instantiableSubclasses = new ArrayList();
List classes = findClassesThatExtend(superclass);
for (Iterator iter = classes.iterator(); iter.hasNext();) {
String className = (String) iter.next();
try {
log.debug("Trying to instantiate: " + className);
Class clazz = Class.forName(className);
int modifiers = clazz.getModifiers();
if (!Modifier.isAbstract(modifiers)) {
log.debug("Can instantiate: " + className);
instantiableSubclasses.add(clazz);
}
}
catch (Throwable t) {
log.warn("Can't instantiate: " + className, t);
}
}
return (Class[]) instantiableSubclasses.toArray(new Class[0]);
}
/**
* Find classes in the provided path(s)/jar(s) that extend the class(es).
*
* @return ArrayList containing discovered classes
*/
private static String[] addJarsInPath(String[] paths) {
Set fullList = new HashSet();
for (int i = 0; i < paths.length; i++) {
fullList.add(paths[i]);
if (!paths[i].endsWith(".jar")) {
File dir = new File(paths[i]);
if (dir.exists() && dir.isDirectory()) {
String[] jars = dir.list(new FilenameFilter() {
public boolean accept(File f, String name) {
if (name.endsWith(".jar")) {
return true;
}
return false;
}
});
for (int x = 0; x < jars.length; x++) {
fullList.add(jars[x]);
}
}
}
}
return (String[]) fullList.toArray(new String[0]);
}
public static List findClassesThatExtend(String[] strPathsOrJars, Class[] superClasses, boolean innerClasses)
throws IOException, ClassNotFoundException {
List listPaths = null;
ArrayList listClasses = null;
List listSuperClasses = null;
strPathsOrJars = addJarsInPath(strPathsOrJars);
/*
* if (log.isDebugEnabled()) { for (int k = 0; k < strPathsOrJars.length; k++) { log.debug("strPathsOrJars : " +
* strPathsOrJars[k]); } }
*/
listPaths = getClasspathMatches(strPathsOrJars);
/*
* if (log.isDebugEnabled()) { Iterator tIter = listPaths.iterator(); for (; tIter.hasNext();) { log.debug("listPaths : " +
* tIter.next()); } }
*/listClasses = new ArrayList();
listSuperClasses = new ArrayList();
for (int i = 0; i < superClasses.length; i++) {
listSuperClasses.add(superClasses[i].getName());
}
// first get all the classes
findClassesInPaths(listPaths, listClasses);
/*
* if (log.isDebugEnabled()) { Iterator tIter = listClasses.iterator(); for (; tIter.hasNext();) { log.debug("listClasses : " +
* tIter.next()); } }
*/List subClassList = findAllSubclasses(listSuperClasses, listClasses, innerClasses);
return subClassList;
}
private static List getClasspathMatches(String[] strPathsOrJars) {
ArrayList listPaths = null;
StringTokenizer stPaths = null;
String strPath = null;
int i;
listPaths = new ArrayList();
// log.debug("Classpath = " + System.getProperty("java.class.path"));
// stPaths = new StringTokenizer(System.getProperty("java.class.path"), System.getProperty("path.separator"));
stPaths = new StringTokenizer(javaClassPath, System.getProperty("path.separator"));
if (strPathsOrJars != null) {
strPathsOrJars = fixDotDirs(strPathsOrJars);
strPathsOrJars = fixSlashes(strPathsOrJars);
strPathsOrJars = fixEndingSlashes(strPathsOrJars);
}
/*
* if (log.isDebugEnabled()) { for (i = 0; i < strPathsOrJars.length; i++) { log.debug("strPathsOrJars[" + i + "] : " +
* strPathsOrJars[i]); } }
*/
// find all jar files or paths that end with strPathOrJar
while (stPaths.hasMoreTokens()) {
strPath = fixDotDir((String) stPaths.nextToken());
strPath = fixSlashes(strPath);
strPath = fixEndingSlashes(strPath);
if (strPathsOrJars == null) {
log.debug("Adding: " + strPath);
listPaths.add(strPath);
} else {
boolean found = false;
for (i = 0; i < strPathsOrJars.length; i++) {
if (strPath.endsWith(strPathsOrJars[i])) {
found = true;
listPaths.add(strPath);
break;// no need to look further
}
}
if (!found) {
log.debug("Did not find: " + strPath);
}
}
}
return listPaths;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -