How to get method signatures from a jar file?
Asked Answered
M

7

49

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?

Mariejeanne answered 10/6, 2009 at 15:33 Comment(0)
C
64

jar tf will list the contents for you. javap will allow you to see more details of the classes (see the tools guide).

For instance if you have a class named mypkg.HelloWorld in a jar myjar.jar then run it like

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.

Cholecystotomy answered 10/6, 2009 at 15:38 Comment(0)
P
6

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

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

Pentode answered 10/6, 2009 at 15:42 Comment(1)
Agree. A very powerful combination.Stempien
C
5

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.

I'd assume other IDEs can do similar.

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

Comestible answered 10/6, 2009 at 15:39 Comment(0)
G
4
  • Eclipse would work great

  • A Java Decompiler would 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-code directly without need for a decompiler.

Gentilis answered 10/6, 2009 at 16:0 Comment(0)
B
4

You can use the library WALA to 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:

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 here it does Dalvik (e.g. Android Apps) as well.

Biff answered 16/8, 2013 at 11:13 Comment(1)
Interesting approach, though some classes are not being loaded. I tried using IClassHierarchy cha = ClassHierarchy.makeWithPhantom(scope); though I got a couple of exceptions. Perhaps, I would need to configure some more dependent libraries, but not completely sure if this might be the problem.Ethnomusicology
H
2

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.

Hannus answered 6/12, 2011 at 10:28 Comment(0)
H
0

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

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

Homogamy answered 1/4, 2012 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.