I need some help in java. Can someone help me how to count the number of statement present under conditional statements (if, if, else if, else) from a java file with the help of another java file.
To explain more better, I have a java file which includes If conditions, else if conditions, While loops also. I am writing an utility to read the java file and i want to count the number of lines present under conditional statements(if, if, else if, else).
First you have to write a parser for the Java language. Then you have to take the results of that parse, which will probably be some kind of tree structure in which (for example) an if-statement is represented by a particular kind of node (the "if") which has an attribute (which must be a boolean expression) and a list of children which are statements.
I'm assuming that when you say "the number of statement present under" an if-statement you mean the number of statements in the block controlled by the if-statement for that description. The answer then would be simply the number of children of that node. (I wouldn't have used the word "under" in that case but at any rate if you meant something different, then you would have to do something different in your code.)
What you say later suggests that you think that you can also count the number of lines. Consider that you can have several statements on one line, and conversely that one statement can extend over more than one line. For example:
So you have to clean up your requirements a bit to decide whether you want to count statements or lines. If you want to count lines, your parse tree will have to keep track of what is on what line.
Joined: Dec 02, 2010
Thanks for the reply. Yes, i am talking about the number of statements in the block controlled by the if-statement for that description.
How can we acheive that.. is there any sample code for this kind of requirement.