5 Replies - 1113 Views - Last Post: 24 March 2010 - 10:35 PM Rate Topic: -----

#1 doha786   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

need help to search 'method(s)' in Java file

Posted 24 March 2010 - 02:06 AM

hi,

i want to build a program that can read java files and find all the METHODS in those files and print it all..
suppose, in one java file contains methods like these:
public ReturnValues(Object first, Object second) {
		.....
	}
	
	public Object getFirst() {
		return first;	}
	
	public Object getSecond() {
		return second; 	}
}

public int total(int x, int y){
...
}

public void result(){
return; }




the output should like this:

Quote

Return Value:............ Method Name:......... Parameter: ...............


anybody pls tell me or give some suggestion how can i build this program ?

This post has been edited by doha786: 24 March 2010 - 02:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: need help to search 'method(s)' in Java file

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: need help to search 'method(s)' in Java file

Posted 24 March 2010 - 06:32 AM

I am not sure, but id i understood correctly, you can use a Method Object.
here is the Method class API:
http://java.sun.com/...ect/Method.html
Was This Post Helpful? 1
  • +
  • -

#3 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,618
  • Joined: 25-December 08

Re: need help to search 'method(s)' in Java file

Posted 24 March 2010 - 06:32 AM

you will need to create a BufferedReader and parse through the java file. While going through the line you are going to want tokenize your strings not based on lines, but by key punctuation like { } and ;

As you do this you will need to create some regular expression patterns to recognize keywords { public, class, void, boolean, etc.. } and also things like method names are always end with the opening paren "XXXX ("

All in all you essentially have to recreate a parser, unless you believe your RegEx from the second paragraph is good enough on its own to do it by itself
Was This Post Helpful? 1
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: need help to search 'method(s)' in Java file

Posted 24 March 2010 - 08:23 AM

Get your program to run javac to compile the class.

Load the new class.

Use reflection to get all the methods.

Extract the data you want from the Method object.
Was This Post Helpful? 1
  • +
  • -

#5 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4933
  • View blog
  • Posts: 20,259
  • Joined: 24-September 08

Re: need help to search 'method(s)' in Java file

Posted 24 March 2010 - 08:44 AM

I wrote a documentation tutorial with javac included. Check uner my "my contributions" thingy in my profile! I cba >_> ^__^ Hope it helps with what you're wanting.
Was This Post Helpful? 1
  • +
  • -

#6 doha786   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

Re: need help to search 'method(s)' in Java file

Posted 24 March 2010 - 10:35 PM

Thanks a lot all of you for giving good suggestions...

But the fact is i'm just new in java, so i haven't learned and experienced few things that how to code like 'reflection',getMethod(String, Class[]), getDeclaredMethods(), getDeclaredMethod(String, Class[]).... anyway, i'm gonna learn..

besides, i'hv an idea..
since my program will be a very small search engine that will find the method names from java files, so to make it simple if the program search those methods names which are public..then i think i can make like this:
String target="public";
String returnValue;
String methodName;
..
returnValue = scan.findWithinHorizon(target,0); 
methodeName = scan.findWithinHorizon(returnValue,0);
.. 


i'm bit familiar with this method to find the next word of a target string.. But now, how can i find the parameters that have inside "( ........)" of the method ??

your co-operation is really appreciated..

This post has been edited by doha786: 24 March 2010 - 10:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1