File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes finding number of function/method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "finding number of function/method" Watch "finding number of function/method" New topic
Author

finding number of function/method

Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Hi all,
I want to write a java module on finding the number of function/methods exists in a Java file.
can anybody help me....
thanks
Suddhasattwa
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Suddhasattwa Mukherjee:
Hi all,
I want to write a java module on finding the number of function/methods exists in a Java file.
can anybody help me....
thanks
Suddhasattwa


Do you mean a Java source code file or a Java class file ?


Joanne
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
I want to read a java source file and generate a text file regarding the number of functions/methods in it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
You need a lexical analyser or java source code parser then. Try searching the forums for these terms - I believe some have been suggested in the past.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
But I want to develop the code by own.Can you tell me where can I have the example code or some solution in this regard.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

As Joanne says, search for similar terms in this forum, I think you'll find and open source tool that does this . You can analyse their code to help get you started if you really want to write your own (is this a homework assignment)?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Maybe some of the existing ones are open source in which case you can look at their code. Otherwise google those terms and see if there are any samples around.
As this is purely a learning exercise for you, you need to learn to do some research. Or if you have done some research, tell us what you found so that we don't tell you stuff you already know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Check out javap; just calling javap <full class name> will show you all of its fields and methods.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Rob Prime:
Check out javap; just calling javap <full class name> will show you all of its fields and methods.


But that works on the class file. OP wants to parse a source file which could include several classes.
Plus he wants to write it himself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
You need a compilers book, eg Aho Lam Sethi and Ullman.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Originally posted by Rob Prime:
Check out javap; just calling javap <full class name> will show you all of its fields and methods.


Thanks for the suggestion.But it works with only .class file.So it won't fulfill my intension.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Originally posted by Joanne Neal:
Maybe some of the existing ones are open source in which case you can look at their code. Otherwise google those terms and see if there are any samples around.
As this is purely a learning exercise for you, you need to learn to do some research. Or if you have done some research, tell us what you found so that we don't tell you stuff you already know.

Thanks for all the suggestions.Yes, this is a purely learning exercise for me or you can tell assignement.I have not done any research till now.I will google some word I get here in forum.In the mean time if anyone of you can help me in this regard with materials or links it would be very helpful for me.

Thanks
Suddhasattwa
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Another option would be to use the Doclet API of the javadoc tool to get the declared methods from java source files.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Originally posted by Garrett Rowe:
Another option would be to use the Doclet API of the javadoc tool to get the declared methods from java source files.

I have gone through it.But how to get com.sun.javadoc package installed?I am not able to compile ListParams.java file written in the link.
Thanks
Suddhasattwa
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

I have gone through it.But how to get com.sun.javadoc package installed?


The com.sun.javadoc package is in tools.jar in the lib folder of your Java installation. You have to add that to the classpath to compile the example.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
I am successful in compiling the code.But when I run the command

it gives me the error

Can you help me out?
Another question what is reason to give java.util at end of the command?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Try this:

Add the following file to a folder:


Compile it using the following command:


Then add this test Java class to the same folder (you don't have to compile it):


Then execute this command:


You should get an output similar to this:
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Thanks for the help.But it all goes fine except at the end the same error is showing without the expected display.
javadoc:error -cannot find doclet class ListMethodNames
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Thanks for the help.But it all goes fine except at the end the same error is showing without the expected display.
javadoc:error -cannot find doclet class ListMethodNames
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Suddhasattwa Mukherjee:
Thanks for the help.But it all goes fine except at the end the same error is showing without the expected display.

Is your doclet class file in the same directory that you're in when you attempt to invoke the javadoc tool?
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
yes.ListMethodNamesjava and .class as also TesClass.java all the file are in same directory.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Please reply.i am yet to solve the problem.Anyone of the idea/findings of free source code of any java source code parser that generate xml as output?Please reply.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
ListMethodNamesjava and .class as also TesClass.java all the file are in same directory.


But are is that the directory you've navigated to when you invoke the javadoc tool from the command line?

I'm not sure what to tell you. It works for me just as I have described. There has to be something going on.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Thanks anyway for all the help.I put the java file in D:\NewFolder.Compile it there from command line.Then I put the TestClass.java file there and execute the command
Not only it gives error in my system but also in one of my friends system.So I think it may be some configuration problem in our machine,it is running in your system.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Just a stab in the dark here. Try:
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Thanks.It works.but what is the reason that it previously didnot work?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'm glad it works. I have no idea why it didn't work the other way.
Suddhasattwa Mukherjee
Ranch Hand

Joined: May 28, 2008
Posts: 52
Can you help me one thing also.I want to execute the command
from another java class.How could I do that?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Look here.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding number of function/method
 
Similar Threads
method to convert positive number to negative
How do i call my function in JSP
Power of a number ?
Avoiding dynamic memory allocation in AI board games.
convert Postiive number to negative..