Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / November 2007

Tip: Looking for answers? Try searching our database.

Obtaining methods signature

Thread view: 
Dmitry Marienko - 21 Nov 2007 16:24 GMT
Hi !
  I'm dumb on simple question. How can I get  method's signature in my  
java program in "JVM notation" ?

 For example for class

  class A {
     void foo(int a,int b) { ... }
  }

 I would like to get  string .../A/foo(II)V  for method 'foo' . I haven't  
found any appropriate methods to do it.

--dima
Joshua Cranmer - 21 Nov 2007 17:44 GMT
> Hi !
>   I'm dumb on simple question. How can I get  method's signature in my
[quoted text clipped - 10 lines]
>
> --dima

If you have easy access the bytecode, the program `javap' can print out
the signature for the method (foo(II)V).

Barring that, it is pretty easy to transform a
java.lang.reflect.Method's toString() to a JVM internal String.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

A Pietu Pohjalainen - 23 Nov 2007 12:35 GMT
>    I'm dumb on simple question. How can I get  method's signature in my  
> java program in "JVM notation" ?
[quoted text clipped - 4 lines]
>   I would like to get  string .../A/foo(II)V  for method 'foo' . I haven't  
> found any appropriate methods to do it.

I believe that this information is not available in the standard Java
API. Haven't followed late changes though..

To get this info, I'm using the Apache BCEL library
(http://jakarta.apache.org/bcel/):

$ cat SignatureLister.java
import java.io.*;

import org.apache.bcel.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.util.*;

public class SignatureLister {
   public static void main(String args[]) throws IOException {
       ClassParser parser  = new ClassParser(args[0]);
       JavaClass   clazz   = parser.parse();
       Method[]    methods = clazz.getMethods();

       for(int i=0; i<methods.length; i++) {
           System.out.print(clazz.getClassName());
           System.out.print(".");
           System.out.print(methods[i].getName());
           System.out.println(methods[i].getSignature());
       }    
   }
}

$ javac -classpath bcel-5.2.jar SignatureLister.java

$ $ java -cp bcel-5.2.jar:. SignatureLister A.class
A.<init>()V
A.foo(II)V

br,
Pietu
Dmitry Marienko - 23 Nov 2007 13:11 GMT
Hi !
 Thanks, Joshua and Pietu,
  Unfortunately,  I'm restricted in using any extern packages in this  
project, so I have written simple class for translation method's signature  
from general notation to jvm.

>>    I'm dumb on simple question. How can I get  method's signature in my
>> java program in "JVM notation" ?
[quoted text clipped - 40 lines]
> A.<init>()V
> A.foo(II)V

--dima


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.