我正在做一个个人项目,希望能发展成一个大而可怕的东西……
我想派生一个字符串引用的类,即使它不是完全限定的。我想尽可能地重现 JVM 的行为。例如,在代码中,我可以引用已导入或在同一个包中的不合格事物。我知道这可能并不完全可能,但我想试一试。我当前的代码有以下问题:
- 它很慢。它生成所有类的列表并在其中查找匹配项。
在我的机器上大约需要 6 秒。更新:我更改了反射代码以获取一组类名而不是类。现在快两倍了。 - 显然这也意味着我可以有重复的麻烦。有没有办法像 Java 用来消除歧义一样对列表进行排序? 更新这是剩下的唯一严重问题。如果发现歧义,至少我现在抛出异常。
- 我使用 Google Reflection 库来派生类列表。我使用
ClasspathHelper.forClassLoader()
andClasspathHelper.forJavaClassPath()
,但我仍然可能遗漏一些。 更新我正在联合四个不同的类集,它们似乎提供了很好的覆盖范围。 - 我当然想念 java.*. 这是因为这些类是由原始类加载器加载的,并且可以进行硬编码。因此它们不可用?作为最后的手段,是否有一个 java.* 中的类列表以机器可读的形式,我可以用它来查找它们? UPDATE
http://docs.oracle.com/javase/7/docs/api/overview-summary.html
似乎是迄今为止最好的选择。显然我还可以编写一个 doclet,它会以机器可读的形式输出 javadocs。已经有了吗?
还有什么问题吗?这是当前代码:
/**
* Attempts to return the class specified by a string even if the it is not
* fully qualified. It does this by going through all the classes there are.
* Note: You may specify arrays in normal declaration form, e.g. myArray[][].
*
* @param classString The string rep of the class/type
* @return The class I think it refers to
*/
private static Class<?> getClassOfString(String classString) {
classString = convertTypeToCanonicalForm(classString);
Matcher matcher = Pattern.compile("(\\[+(\\w))?((\\w+(\\.\\w+)*);?)?").matcher(classString);
matcher.find();
String arrayPrefix = matcher.group(2);
String className = matcher.group(4);
try {
if (arrayPrefix == null || arrayPrefix.equals("L")) {
String classFound = null;
for (String clazz : getSetOfAllClasses()) {
if (clazz != null) {
if (clazz.matches("(.*\\.)?"+Pattern.quote(className)))
if (classFound == null)
classFound = clazz;
else
throw new RuntimeException("Class name '" + className +
"' is ambiguous: " + classFound + " vs " + clazz);
}
}
if (classFound != null) {
classString = classString.replaceAll(Pattern.quote(className), classFound);
return Class.forName(classString);
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException();
}
if (arrayPrefix == null)
if (className == "boolean") return(boolean.class); // primitive types
else if (className == "byte") return(byte.class);
else if (className == "char") return(char.class);
else if (className == "double") return(double.class);
else if (className == "float") return(float.class);
else if (className == "int") return(int.class);
else if (className == "long") return(long.class);
else if (className == "short") return(short.class);
else if (className == "void") return(void.class);
// hack for java.* types-- look 'em up
if (className != null) {
String prefixFound = null;
for (String prefix : javaTypes.keySet())
for (String type : javaTypes.get(prefix))
if ((prefix+"."+type).matches(".*" + className))
if (prefixFound == null)
prefixFound = prefix;
else
throw new RuntimeException("Class name '" + className +
"' is ambiguous: java." + prefixFound + "." + className +
" vs java." + prefix + "." + className);
if (prefixFound == null) prefixFound = "util"; // temp hack
classString = classString.replaceAll(Pattern.quote(className),
"java." + prefixFound + "." + className);
}
try {
return Class.forName(classString);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class '" + className +
"' is unknown or somewhere in java.* that I don't know about");
}
}
/**
* Converts the type from standard declaration form to canonical internal form
* if necessary. Practically this just means doing array translation.
*
* @param type
* @return
*/
private static String convertTypeToCanonicalForm(String type) {
Matcher matcher = Pattern.compile("^(\\w+(\\.\\w+)*)((\\[\\])+)$").matcher(type);
if (matcher.find()) {
String typeTemp = matcher.group(1);
if (typeTemp.equals("boolean")) typeTemp = "Z"; // primitive typeTemps
else if (typeTemp.equals("byte")) typeTemp = "B";
else if (typeTemp.equals("char")) typeTemp = "C";
else if (typeTemp.equals("double")) typeTemp = "D";
else if (typeTemp.equals("float")) typeTemp = "F";
else if (typeTemp.equals("int")) typeTemp = "I";
else if (typeTemp.equals("long")) typeTemp = "J";
else if (typeTemp.equals("short")) typeTemp = "S";
else typeTemp = "L" + typeTemp + ";";
matcher = Pattern.compile("\\[\\]").matcher(matcher.group(3));
while (matcher.find())
typeTemp = "[" + typeTemp;
type = typeTemp;
}
return type;
}
/**
* List of package classes for each prefix in the java.* domain
*/
@SuppressWarnings("serial")
static final Map<String, List<String>> javaTypes = new HashMap<String , List<String>>() {{
put("lang", Arrays.asList(new String[]{"Boolean","Byte","Character","Class","Double",
"Float","Integer","Long","Short","String","Void"}));
// rest of java.* goes here
}};
/**
* Gets and stores a set of all the classes we can find. Missing the java.* domain.
* Uses the Google Reflection library.
*
* @return The class set
*/
static Set<String> classStringSet = null;
private static Set<String> getSetOfAllClasses() {
if (classStringSet == null) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
classLoadersList.add(ClassLoader.getSystemClassLoader());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))));
classStringSet = reflections.getStore().getSubTypesOf(Object.class.getName());
reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forJavaClassPath()));
classStringSet.addAll(reflections.getStore().getSubTypesOf(Object.class.getName()));
}
return classStringSet;
}
我为什么要这样做?那一定会很好玩!你会看到的。