Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Calculate LOC for BlockStmt #18915

Closed
Edisonwudi opened this issue Mar 4, 2025 · 3 comments
Closed

How to Calculate LOC for BlockStmt #18915

Edisonwudi opened this issue Mar 4, 2025 · 3 comments
Assignees
Labels
bug Something isn't working Java

Comments

@Edisonwudi
Copy link

Edisonwudi commented Mar 4, 2025

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

@Edisonwudi Edisonwudi added the question Further information is requested label Mar 4, 2025
@mbg mbg added bug Something isn't working Java labels Mar 4, 2025
@mbg
Copy link
Member

mbg commented Mar 4, 2025

Hi @Edisonwudi 👋🏻

Thanks for this report. That does look wrong to me. I will pass this on to the relevant engineering team.

@smowton
Copy link
Contributor

smowton commented Mar 4, 2025

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

@mbg mbg removed the question Further information is requested label Mar 4, 2025
@Edisonwudi
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Java
Projects
None yet
Development

No branches or pull requests

3 participants