比如1)
有个类叫
a.b.c.MyClass
输入1个字符串"MyClass" 返回字符串"a.b.c.MyClass"
比如2)
有重名的情况,全部返回
有两个类
a.b.c.MyClass
x.y.z.MyClass
输入1个字符串"MyClass"
返回字符串
"a.b.c.MyClass"
和
"x.y.z.MyClass"
调用方法
完整代码
有个类叫
a.b.c.MyClass
输入1个字符串"MyClass" 返回字符串"a.b.c.MyClass"
比如2)
有重名的情况,全部返回
有两个类
a.b.c.MyClass
x.y.z.MyClass
输入1个字符串"MyClass"
返回字符串
"a.b.c.MyClass"
和
"x.y.z.MyClass"
调用方法
ArrayList<String> fullNames = FullClassNameGetter.getInstance().getFullClassName(不含包的类名);
完整代码
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class FullClassNameGetter {
private static final String JAVA_CLASS_PATH = "java.class.path";
private static final String CLASSPATH_SEPARATOR = System.getProperty("path.separator");
private static final String PACKAGE_SEPARATOR = ".";
private static final String JAR_DIR_SEPARATOR = "/";
private static final String CLASS_SUFFIX = ".class";
private HashMap<String, ArrayList<String>> nameingMap;
private String[] jdkJars = {"lib/rt.jar",
"lib/jsse.jar",
"lib/jce.jar",
"lib/charsets.jar",
"lib/ext/dnsns.jar",
"lib/ext/localedata.jar",
"lib/ext/sunjce_provider.jar",
"lib/ext/sunmscapi.jar",
"lib/ext/sunpkcs11.jar"};
private static class FullClassNameGetterSingletonHolder {
private static FullClassNameGetter instance = new FullClassNameGetter();
}
private FullClassNameGetter() {
final String initialClasspath = System.getProperty(JAVA_CLASS_PATH);
//Date now = new Date();
setClassPath(initialClasspath);
//System.out.println("initialize FullClassNameGetter done. " + (new Date().getTime() - now.getTime()));
}
public static FullClassNameGetter getInstance() {
return FullClassNameGetterSingletonHolder.instance;
}
public ArrayList<String> getFullClassName(String className) {
ArrayList<String> ret = null;
if (className.indexOf(PACKAGE_SEPARATOR) != -1) {
ret = new ArrayList<String>();
ret.add(className);
return ret;
}
ret = nameingMap.get(className);
return ret;
}
public void setClassPath(String classPath) {
nameingMap = new HashMap<String, ArrayList<String>>();
String[] classPathArray = classPath.split(CLASSPATH_SEPARATOR);
File file;
for (String onePath : classPathArray) {
file = new File(onePath);
if (file.exists()) {
if (file.isDirectory()) {
// dir
setDirClasses(file, "");
} else {
// jar file
setJarClasses(file);
}
}
}
String jreDir = System.getProperty("java.home");
for (String jreJar : jdkJars) {
file = new File(jreDir + JAR_DIR_SEPARATOR + jreJar);
if (file.exists()) {
// jar file
setJarClasses(file);
}
}
}
private void setDirClasses(File classDir, String path) {
File[] files = classDir.listFiles();
String className;
for (File file : files) {
if (file.isDirectory()) {
setDirClasses(file, path + file.getName() + PACKAGE_SEPARATOR);
} else {
if (file.getName().endsWith(CLASS_SUFFIX)) {
className = file.getName().substring(0, file.getName().length() - CLASS_SUFFIX.length());
if (!"".equals(path)) {
setNamingMap(className, path + className);
} else {
//no package
setNamingMap(className, className);
}
}
}
}
}
private void setJarClasses(File jarFile) {
JarFile jf = null;
try {
jf = new JarFile(jarFile);
Enumeration<JarEntry> es = jf.entries();
JarEntry entry;
String simpleName;
while (es.hasMoreElements()) {
entry = es.nextElement();
if (!entry.isDirectory()) {
String fullName = entry.getName();
if (fullName.endsWith(CLASS_SUFFIX)) {
fullName = fullName.substring(0, fullName.length() - CLASS_SUFFIX.length());
int index = fullName.lastIndexOf(JAR_DIR_SEPARATOR);
if (index != -1) {
simpleName = fullName.substring(index + 1, fullName.length());
fullName = fullName.replace(JAR_DIR_SEPARATOR, PACKAGE_SEPARATOR);
setNamingMap(simpleName, fullName);
} else {
//no package
simpleName = fullName;
setNamingMap(simpleName, fullName);
}
}
}
}
} catch (IOException e) {
//do nothing
} finally {
if (jf != null) {
try {
jf.close();
} catch (IOException e) {
//do nothing
}
}
}
}
private void setNamingMap(String simpleName, String fullName) {
ArrayList<String> value = nameingMap.get(simpleName);
if (value == null) {
value = new ArrayList<String>();
nameingMap.put(simpleName, value);
}
if (!value.contains(fullName)) {
value.add(fullName);
}
}
}