Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying Method Headers in Source Code Files 1

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
0
0
US
I need to programmatically read/parse java source code files and identify method headers within it. Although I could probably use a doclet to aid in the process, it would be much simpler to create my own functionality to identify a method header. Following are my ideas, please comment on them. (My ideas are a starting point -- I need your opinions too.)

The line must contain a “(“ and also a “)” and cannot end with a semi colon, thus distinguishing it from a method call. The line cannot also have the words “if” “for” or “while” thus distinguishing it from other language elements that use parentheses.

If the line meets the preceding criteria and also has any of the access modifiers public, protected, and private, or the “static” modifier, then this is guranteed to be a method header.

If the line also a return type such as void, then this is guranteed to be a method header. (Actually trying to use return types to identify a method would be difficult because a method can return a programmer-defined type, any of the primitive types, or any of the hundreds of types in the Java API, all the possibilities for which could be difficult to account for.)

If the preceding does not yield a result, then you need to make sure that the line does not end in a semi colon, does not contains the words “if” “for” or “while” and also examine the contents of the “()”as follows:

If there is other than whitespace between “()” there would have to be at least one pair of elements (type and parameter name) separated by a space. Each pair of elements has to be separated by a comma. There can be no semi colons in the “()” – this will help distinguish between a method header and the elements in a for loop. There can be no colon in the “()” – this will rule out the jdk 1.5.0 enhanced for loops.

Thank you very much.
 
You don't say why you need to do this - but the Java Reflection API can interrogate classes at runtime to determine there methods, return types, parameters etc.

It might be a bit easier ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj,

Thanks for helping. What I'm trying to do is programmatically add information to source code files. Specifically, I'll be adding information to existing javadoc comments for methods.

All I will have is:
1) My parsing application
2) a set of source code files (entirely separate from my app) that I'll parse and then add the information to.

Before I can add to the Javadoc comments, I need to identify methods in the source code files. (Let me know if this does not make sense.)

I glanced at information about the Reflection API and it seems to operate on runtime objects. Like I said all I'll have is my app and the source code files. Based on this, I don't think the Reflection API will work for me. Would you agree? Let me know if you have more information.

Thanks and regards.
 
Ah, no well reflection will not help here.

As for parsing the file, just use a BufferedReader, and read through each line, checking if the line contains an access modifier. If it does, check it does not have a ";" then keep reading until you capture a "{" - then substring from the beginning of the modifier to the "{", which should be you're entire method declaration.

If you have lines of code like this :

public static int xxx
= 1;

... you will need to make it a bit cleverer, but apart from that, thats all you should need to do.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks sedj. I'll give that a try.
 
and the definition of what a line looks like that defines a method is also flawed.
A method declaration can span multiple lines, and for short methods might contain the method body as well.
 
to be fair, I did say using a BufferedReader and keep reading lines until a "{" is found ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I don't think it's a good idea to build your own Java source interpreter, that would be too much work.

If you know beforehand the structure of the files, use that knowledge. If not, I'd use the existing opensource APIs for this.

JavaModel for JDev or JDT for Eclipse.


Cheers,
Dian
 
Thanks stefanwagner, sedj, Dianecht, and jwenting for your posts. I agree that the more one examines the problem, the more complicated it gets. I'll check out the web sites you suggested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top