Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding number of function/method

 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to read a java source file and generate a text file regarding the number of functions/methods in it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out javap; just calling javap <full class name> will show you all of its fields and methods.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a compilers book, eg Aho Lam Sethi and Ullman.
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be to use the Doclet API of the javadoc tool to get the declared methods from java source files.
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.ListMethodNamesjava and .class as also TesClass.java all the file are in same directory.
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a stab in the dark here. Try:
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.It works.but what is the reason that it previously didnot work?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad it works. I have no idea why it didn't work the other way.
 
Suddhasattwa Mukherjee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look here.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic