symbol lister for java (like nm)

H

horos22

All,

I'm looking for something the equivalent of nm in java.

I want to be able to give a utility either the name of a class file,
or the name of a jar file, have that utility go into the jar file and
give a listing of all the symbols in that file (like nm does for C). I
know that c++filt demangles these symbols, but I'm not sure how to get
a list of mangled symbols in the first place to demangle!

Anyways, I would have thought this would be a fairly simple (and
generic) request, but apparently not - a google search for 'symbol
list tool java' and 'nm for java' returns nothing straightforward.

So - is there such a tool available? And, if so, where?

Thanks much,

Ed
 
J

Joshua Cranmer

I want to be able to give a utility either the name of a class file,
or the name of a jar file, have that utility go into the jar file and
give a listing of all the symbols in that file (like nm does for C). I
know that c++filt demangles these symbols, but I'm not sure how to get
a list of mangled symbols in the first place to demangle!

javap -private? That will list all members in a given Java class:

jcranmer@quetzalcoatl ~ $ javap -private java.lang.Object
Compiled from "Object.java"
public class java.lang.Object{
public java.lang.Object();
private static native void registerNatives();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
protected native java.lang.Object clone() throws
java.lang.CloneNotSupportedException;
public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait(long) throws
java.lang.InterruptedException;
public final void wait(long, int) throws
java.lang.InterruptedException;
public final void wait() throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
static {};
}
 
T

Tom Anderson

javap -private? That will list all members in a given Java class:

Doesn't do local variable (and parameter) names. For that, you also need
the -l flag. Note that this doesn't work for abstract methods (including
everything in interfaces), which don't record the names of their
parameters.

As far as i know, there's no way to recover the name of a loop label from
a class file. But that's the only symbol i can think of for which that's
true. Oh, and type parameter names, although those are stored in the class
file somewhere.

tom
 
J

Joshua Cranmer

Doesn't do local variable (and parameter) names. For that, you also need
the -l flag. Note that this doesn't work for abstract methods (including
everything in interfaces), which don't record the names of their
parameters.

Well, nm just lists symbol names, which is basically just the method
name (so you only get full signatures in C++ due to mangling). It
appears that one of the options also dumps debugging information
embedded in the file, which would be akin to -l.
As far as i know, there's no way to recover the name of a loop label
from a class file. But that's the only symbol i can think of for which
that's true. Oh, and type parameter names, although those are stored in
the class file somewhere.

If you're referring to the generics signatures, that's -s. I would
expect that -s -l would dump out LocalVariableTypeTables as well as
LocalVariableTables, but I've never read the javap source code in that
much detail to confirm.
 
T

Tom Anderson

Well, nm just lists symbol names, which is basically just the method name (so
you only get full signatures in C++ due to mangling).

Ah, i didn't realise that. For some reason, i thought you got variable
names etc. Am i thinking of ctags?
It appears that one of the options also dumps debugging information
embedded in the file, which would be akin to -l.


If you're referring to the generics signatures, that's -s.

Really? Code:

class Test {
public <T> T pick(boolean flag, T one, T two) {
T result;
if (flag) result = one;
else result = two;
return result;
}
}

Conversation:

$ javap -s -classpath . Test
Compiled from "Test.java"
class Test extends java.lang.Object{
Test();
Signature: ()V
public java.lang.Object pick(boolean, java.lang.Object, java.lang.Object);
Signature: (ZLjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
}

Mind you:

$ java -version
java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b02)
Java HotSpot(TM) Server VM (build 1.5.0_16-b02, mixed mode)

So maybe that does work with 1.6, just not with 1.5.
I would expect that -s -l would dump out LocalVariableTypeTables as well
as LocalVariableTables, but I've never read the javap source code in
that much detail to confirm.

I'll try it next time i'm near a 1.6 machine.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top