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.
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.