Java 如何从 jar 文件中获取方法签名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/976388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:47:26  来源:igfitidea点击:

How to get method signatures from a jar file?

javajarreverse-engineeringmethod-signature

提问by MCS

I have a third-party jar file that comes with the javadocs for only part of the API. Is there a way to reverse engineer the jar file to obtain a complete listing of classes and methods?

我有一个第三方 jar 文件,它只包含 API 的一部分的 javadoc。有没有办法对 jar 文件进行逆向工程以获得完整的类和方法列表?

采纳答案by Tom Hawtin - tackline

jar tfwill list the contents for you. javapwill allow you to see more details of the classes (see the tools guide).

jar tf将为您列出内容。javap将允许您查看类的更多详细信息(请参阅工具指南)。

For instance if you have a class named mypkg.HelloWorldin a jar myjar.jarthen run it like

例如,如果你有一个mypkg.HelloWorld在 jar 中命名的类,myjar.jar那么运行它

javap -classpath myjar.jar mypkg.HelloWorld

However, are you sure you want to be using these unpublished APIs? It's usually a really bad idea.

但是,您确定要使用这些未发布的 API 吗?这通常是一个非常糟糕的主意。

回答by Scott Stanchfield

If you're using eclipse, you can just add it to a project's classpath and explore it using the treeview and/or content assist.

如果您使用的是 eclipse,您只需将其添加到项目的类路径中,然后使用树视图和/或内容辅助来探索它。

I'd assume other IDEs can do similar.

我假设其他 IDE 可以做类似的事情。

From a command-line point of view, you can unjar it (jar xf foo.jar) and use javap against all files.

从命令行的角度来看,您可以解压它 (jar xf foo.jar) 并对所有文件使用 javap。

回答by Mike Deck

As Scott said, you can use Eclipse to get a lot of what you're looking for.

正如 Scott 所说,您可以使用 Eclipse 来获得很多您想要的东西。

I would recommend getting the JadClipse pluginwhich will decompile the .class files on the fly and show you actual Java code as you browse the classes in the IDE.

我建议使用JadClipse 插件,它会在运行中反编译 .class 文件,并在您浏览 IDE 中的类时向您显示实际的 Java 代码。

回答by Bert F

  • Eclipse would work great

  • A Java Decompilerwould translate the classes back into some semblence of source code that you could study to learn about the classes, the methods, their signatures, and maybe even some insight into valid values for some arguments (e.g. don't pass a null for this argument or you'll trigger a NullPointerException immediately). But roll up your sleeves to unjar the jar and run the decompiler against all the class files. This is essentially what Eclipse is doing for help text with undocumented classes.

  • Finally, of course, a "real programmer"would read the byte-codedirectly without need for a decompiler.

  • Eclipse 会很好用

  • 一个Java的反编译器将类转换回源代码的一些semblence,你可以学习学习类,方法,他们的签名,并且甚至一些见解有效值一些参数(例如,不要传递一个空的此参数,否则您将立即触发 NullPointerException)。但是请卷起袖子解压 jar 并针对所有类文件运行反编译器。这本质上是 Eclipse 正在为具有未记录类的帮助文本所做的工作。

  • 最后,当然,“真正的程序员”会直接读取字节码而无需反编译器。

回答by Balaswamy Vaddeman

a quick help for knowing methods of a normal class (not abstract class),I do the following .

了解普通类(不是抽象类)的方法的快速帮助,我执行以下操作。

new classname().press ctrl+space for methods listing in eclipse.

new classname().press ctrl+space 用于在 eclipse 中列出的方法。

回答by Akash Yadav

Use Eclipse > Package Explorer to see the classes and thier hierarchy.

使用 Eclipse > Package Explorer 查看类及其层次结构。

Content Assist(autocomplete feature (ctrl + space)) is also a good help , but wouldnt recomend using an unpublished API

内容辅助(自动完成功能(ctrl + 空格))也是一个很好的帮助,但不建议使用未发布的 API

回答by milnet

You can use the library WALAto read out all methods signatures. You'll however need to load Stub-Code for Java first. The following program should read out all the signatures:

您可以使用库WALA来读出所有方法签名。但是,您首先需要为 Java 加载 Stub-Code。以下程序应读出所有签名:

import com.ibm.wala.ipa.cha.ClassHierarchy; 
import com.ibm.wala.ipa.cha.IClassHierarchy; 
import com.ibm.wala.classLoader.IClass; 
import com.ibm.wala.classLoader.IMethod; 
import com.ibm.wala.ipa.callgraph.AnalysisOptions; 
import com.ibm.wala.ipa.callgraph.AnalysisScope; 
import com.ibm.wala.types.ClassLoaderReference; 
import java.util.jar.JarFile; 
import java.io.IOException; 
import com.ibm.wala.ipa.cha.ClassHierarchyException; 

public class methods { 
    public static void main(String[] args) throws IOException, ClassHierarchyException { 
        AnalysisScope scope = AnalysisScope.createJavaAnalysisScope(); 
        scope.addToScope(ClassLoaderReference.Primordial, new JarFile("jSDG-stubs-jre1.5.jar")); 
        scope.addToScope(ClassLoaderReference.Application, new JarFile("myProgram.jar")); 
        IClassHierarchy cha = ClassHierarchy.make(scope); 

        for (IClass cl : cha) { 
            if (cl.getClassLoader().getReference().equals(ClassLoaderReference.Application)) { 
                for (IMethod m : cl.getAllMethods()) { 
                    String ac = ""; 
                    if (m.isAbstract()) ac = ac + "abstract "; 
                    if (m.isClinit()) ac = ac + "clinit "; 
                    if (m.isFinal()) ac = ac + "final ";  
                    if (m.isInit()) ac = ac + "init ";  
                    if (m.isNative()) ac = ac + "native ";  
                    if (m.isPrivate()) ac = ac + "private "; 
                    if (m.isProtected()) ac = ac + "protected ";  
                    if (m.isPublic()) ac = ac + "public ";  
                    if (m.isSynchronized()) ac = ac + "synchronized ";  
                    System.out.println(ac + m.getSignature()); 
                } 
            } 
        } 
    } 
} 

If you use the adapted WALA-version from hereit does Dalvik (e.g. Android Apps) as well.

如果您从这里使用改编的 WALA 版本,它也适用于Dalvik(例如 Android 应用程序)。