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

发布于 2024-11-07 20:46:49 字数 159 浏览 0 评论 0原文

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

PackageName, ClassName, API Name, ParamName:type;ParameterName:type,ReturnType

I want to list all the APIs of the given jar ( for which I don't have javadoc) in following fashion any help on that?

PackageName, ClassName, API Name, ParamName:type;ParameterName:type,ReturnType

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-11-14 20:46:49

有趣的问题。可能有很多工具可以做类似的事情,尽管不完全是你想要的。当然,使用 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,尽管您可以获得这些。

Interesting problem. Probably a number of tools to do something similar, though not exactly what you want. Certainly there would be an easy implementation with ASM. As it is, I whipped up one using reflection:

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);
            }
        }
    }
}

Include the JAR (and its dependencies) in the classpath. This won't print the names of the parameters, since you can't get those via reflection. It also doesn't show type parameters or var-args, though you could get those.

被翻牌 2024-11-14 20:46:49

您可以尝试 classdoc,它尝试从已编译的文件和 jar 档案生成 javadoc。

http://classdoc.sourceforge.net/classdoc10/classdoc.html

You can try classdoc, which attempts to generate a javadoc from compiled files and jar archives.

http://classdoc.sourceforge.net/classdoc10/classdoc.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文