You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently analyzing Java code to identify opportunities for refactoring switch statements. One crucial metric I need is the number of lines of code (LOC). However, I've run into a problem: the predicates getNumberOfLinesOfCode() and getNumberOfCommentLines() don't work for BlockStmt, as noted in #10199. Additionally, I've found that getEndLine() and getStartLine() return the same value for both SwitchStmt and BlockStmt. This makes it impossible to directly calculate the LOC for switch blocks. Is there a recommended way to calculate the LOC for BlockStmt?
Error Examples and Results
Example 1: Using getNumberOfLinesOfCode() on BlockStmt
from BlockStmt b
select b, b.getNumberOfLinesOfCode().toString() as loc
Result:
No results returned, even though BlockStmt clearly contains code.
Example 2: Using getEndLine() and getStartLine() on SwitchStmt
from SwitchStmt s
select s, s.getLocation().getStartLine() as start, s.getLocation().getEndLine() as end
Input Code:
void process(int x) {
switch(x) { // Line 2
case 1: break; // Line 3
case 2: break; // Line 4
} // Line 5
}
Result:
start = 2, end = 2 (both values are the same, even though the switch block spans multiple lines).
The text was updated successfully, but these errors were encountered:
Thanks for the report. I'll take a look at this as time permits; in the meantime, consider looking for child statements using the getAStmt() predicate -- for example, max([block, block.getAStmt()].getLocation().getEndLine())
Thank you very much for your prompt and helpful response! Your suggestion to use getAStmt() and recursively calculate the maximum end line number has completely resolved my issue. If the issue of BlockStmt not being able to correctly use getNumberOfLinesOfCode() and getEndLine() can be further resolved, I believe more developers will benefit from it.
Description of the issue
I'm currently analyzing Java code to identify opportunities for refactoring switch statements. One crucial metric I need is the number of lines of code (LOC). However, I've run into a problem: the predicates getNumberOfLinesOfCode() and getNumberOfCommentLines() don't work for BlockStmt, as noted in #10199. Additionally, I've found that getEndLine() and getStartLine() return the same value for both SwitchStmt and BlockStmt. This makes it impossible to directly calculate the LOC for switch blocks. Is there a recommended way to calculate the LOC for BlockStmt?
Error Examples and Results
Example 1: Using getNumberOfLinesOfCode() on BlockStmt
Result:
No results returned, even though BlockStmt clearly contains code.
Example 2: Using getEndLine() and getStartLine() on SwitchStmt
Input Code:
Result:
start = 2, end = 2 (both values are the same, even though the switch block spans multiple lines).
The text was updated successfully, but these errors were encountered: