Whole file
terryyin/lizard
The author described this change as “fix(java): do not report control structures as methods”. It counts as a record because the checks below fail on the code as it stood at 0b97a9ae8 and pass on a3ed8d6c6, with nothing else changed between the two runs.
Projectterryyin/lizard
Fix saved2026-08-08
Sharing licenceMIT · LICENSE.txt
Change size+10 −0
What the code was meant to do, written into the code itself as a save note
fix(java): do not report control structures as methods
The change
| 15 | 15 | 'void', 'boolean', 'byte', 'char', 'short', 'int', 'long', 'float', 'double', 'var', | |
| 16 | 16 | }) | |
| 17 | 17 | _JAVA_BRACE_COUNT = {'{': 1, '}': -1} | |
| 18 | + | # JLS 3.9: reserved keywords can never be a method name, so "keyword (...) { ... }" | |
| 19 | + | # is a statement, not a declaration. Without this, a control structure written at | |
| 20 | + | # a level where no class body state is active (e.g. a static initializer block at | |
| 21 | + | # the top of the token stream) is reported as a method. See issue #312. | |
| 22 | + | _JAVA_STATEMENT_KEYWORDS = frozenset({ | |
| 23 | + | 'if', 'else', 'for', 'while', 'do', 'switch', 'catch', 'try', 'finally', | |
| 24 | + | 'synchronized', 'return', 'throw', 'assert', 'break', 'continue', 'instanceof', | |
| 25 | + | }) | |
| 18 | 26 | ||
| 19 | 27 | ||
| 20 | 28 | def _java_record_begins_type_declaration(last_token, after_unqualified_annotation): | |
| ⋯ | |||
| 138 | 146 | self._state = self._state_decorator | |
| 139 | 147 | return | |
| 140 | 148 | if self._try_start_a_class(token, use_after_annotation): | |
| 149 | + | return | |
| 150 | + | if token in _JAVA_STATEMENT_KEYWORDS: | |
| 141 | 151 | return | |
| 142 | 152 | if not self.in_record_constructor: # Only process as potential function if not in record constructor | |
| 143 | 153 | super(JavaStates, self)._state_global(token) | |
The check that tells the two apart
fail→pass·test/test_languages/testJava.py::TestJava::test_issue_312_control_structures_are_not_methods
fail→pass·test/test_languages/testJava.py::TestJava::test_issue_312_structure_in_static_block
Check file test/test_languages/testJava.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it0b97a9ae8e77b8d559e7ef187ad4b4e3ef02924f
Broken version dated2026-06-15
Modulelizard_languages.java
Units changedJavaStates
Fingerprint122821113ad051a5
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in terryyin/lizard
- 2026-07-02HalsteadClassifier
- 2026-07-02fix(csv): emit columns for extensions with multiple FUNCTION_INFO fields
- 2026-06-15fix(golike): register generic functions with [...] type params
- 2026-06-15fix(java): enhance parsing of generic and qualified type names in anonymous classes
- 2026-06-11fix(script): stop a '#' comment continuing past a trailing backslash (#317)
- 2026-06-02fix(objc): handle nested parens in block / function-pointer param types