java - 列出带有参数和返回类型的类 API

标签 java api reflection

我想按照以下方式列出给定 jar 的所有 API(我没有 javadoc),对此有什么帮助吗?

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,尽管您可以获得这些。

关于java - 列出带有参数和返回类型的类 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6027819/

相关文章:

java - 如何在FileInputStream中加载外部图像

Java尝试进行反射(类实例并在运行时调用方法)

c# - 如何根据名称获取属性值

java - 解析 TypeVariable

java - JAXB 使用 ID 引用而不是包含来序列化 XML

java - 从 Spring 服务器请求获取字符串主体

javascript - 将 Nashorn 迁移到 GraalVM

javascript - 变量更改后调用事件

php - zabbix一段时间内动态创建主机组

api - 通过 Gmail SMTP API 发送预定的电子邮件