我想按照以下方式列出给定jar(为此我没有javadoc)的所有API,对此有任何帮助?列出具有参数和返回类型的类API
PackageName, ClassName, API Name, ParamName:type;ParameterName:type,ReturnType
我想按照以下方式列出给定jar(为此我没有javadoc)的所有API,对此有任何帮助?列出具有参数和返回类型的类API
PackageName, ClassName, API Name, ParamName:type;ParameterName:type,ReturnType
有趣的问题。可能有许多工具可以做类似的事情,但不完全是你想要的。当然,ASM会很容易实现。实际上,我使用反射掀起了一个:
public class C {
public static void main(String[] args) throws java.io.IOException {
java.util.jar.JarFile jar = new java.util.jar.JarFile(args[0]);
java.util.Enumeration<java.util.jar.JarEntry> jarEntries = jar.entries();
while (jarEntries.hasMoreElements()) {
java.util.jar.JarEntry jarEntry = jarEntries.nextElement();
String jarEntryName = jarEntry.getName();
if (!jarEntryName.endsWith(".class")) {
continue;
}
int jarEntryNameSuffixIndex = jarEntryName.length() - 6;
String binaryName = jarEntryName.substring(0, jarEntryNameSuffixIndex).replaceAll("/", "\\.");
Class<?> type = null;
while (type == null) {
try {
type = ClassLoader.getSystemClassLoader().loadClass(binaryName);
} catch (ClassNotFoundException e) {
int binaryNameQualifiedIndex = binaryName.indexOf(".");
if (binaryNameQualifiedIndex == -1) {
throw new RuntimeException("couldn't load class for " + jarEntryName);
} else {
binaryName = binaryName.substring(binaryNameQualifiedIndex + 1);
}
}
}
int typeModifiers = type.getModifiers();
if (!(java.lang.reflect.Modifier.isPublic(typeModifiers) || java.lang.reflect.Modifier.isProtected(typeModi$
continue;
}
String packageName = (type.getPackage() == null) ? "" : type.getPackage().getName();
String typeName = type.getCanonicalName();
for (java.lang.reflect.Method method : type.getDeclaredMethods()) {
if (method.isSynthetic() || method.isBridge()) {
continue;
}
int methodModifiers = method.getModifiers();
if (!(java.lang.reflect.Modifier.isPublic(methodModifiers) || java.lang.reflect.Modifier.isProtected(me$
continue;
}
String methodName = method.getName();
System.out.print(packageName + ", " + typeName + ", " + methodName + ", ");
for (Class<?> parameterType : method.getParameterTypes()) {
String parameterTypeName = parameterType.getCanonicalName();
System.out.print(":" + parameterTypeName + ", ");
}
Class<?> returnType = method.getReturnType();
String returnTypeName = returnType.getCanonicalName();
System.out.println(returnTypeName);
}
}
}
}
将JAR(及其依赖项)包含在类路径中。这将不会打印参数的名称,因为您无法通过反射来获取这些参数。它也不显示类型参数或var-args,尽管你可以得到它们。
你可以尝试classdoc,它试图从编译的文件和jar文件生成一个javadoc。
很不错的解决方案,虽然我更喜欢它,如果功能被分成几种不同的方法。但是你正在按照我自己的方式做所有事情(+1) – 2011-05-17 10:08:19
@ Sean:是的,只是一个大脑转储。肯定可以使用一些重构。 – 2011-05-17 10:10:11
感谢弥敦道非常有帮助。有没有办法来获取参数名称和类型? – 2011-05-17 11:50:05