Skip to content

How to Calculate LOC for BlockStmt #18915

Closed
@Edisonwudi

Description

@Edisonwudi

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

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

Metadata

Metadata

Assignees

Labels

JavabugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions