Skip to content

Commit 3ca18d2

Browse files
authored
Captured Java Formatting Knowledge for Claude & Reformatted Some Log Statements (#245)
* initial capture of coding standards * Remove unnecessary CSOFF/CSON from single-line log statements - Removed 23 CSOFF/CSON comment pairs that were incorrectly wrapping single-line log messages with no deliberate alignment formatting - Reformatted the unwrapped log lines to fit within 80 characters using string concatenation breaks and continuation indentation - Files modified: - AbstractSchedulingService.java (13 pairs removed) - AbstractSQLSchedulingService.java (4 pairs removed) - AbstractListenerService.java (6 pairs removed) - Preserved CSOFF/CSON blocks around legitimately formatted output (aligned labels, separator lines, debug dump tables) - Updated java-coding-standards.md to clarify CSOFF/CSON usage: only for deliberately aligned multi-line output, not simple log messages * Exclude .claude/scripts from bearer scan and update checkstyle - Added skip-path for .claude/scripts to bearer workflow to suppress false-positive CWE-22 path traversal findings on developer-only formatting scripts - Updated checkstyle.xml: removed LeftCurly enforcement (brace placement is no longer enforced by checkstyle), added RightCurly same-line rule for catch/finally/else/do-while, allowed same-line case alignment with switch, added CSOFF/CSON suppression filter, added static final generic type pattern to line-length ignore
1 parent 41464ce commit 3ca18d2

9 files changed

Lines changed: 1525 additions & 78 deletions

File tree

.claude/java-coding-standards.md

Lines changed: 774 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Fix brace placement to Allman style for class/interface/enum/method/
4+
constructor definitions. Also moves 'throws' clauses to their own
5+
line (single-indented from method base).
6+
7+
Leaves control flow blocks (if/else/for/while/try/catch/finally/
8+
switch/synchronized/do/lambda) with same-line braces.
9+
"""
10+
11+
import os
12+
import re
13+
import sys
14+
15+
CONTROL_PREFIXES = [
16+
'if ', 'if(',
17+
'else {', 'else{', 'else if ', 'else if(',
18+
'for ', 'for(',
19+
'while ', 'while(',
20+
'do {', 'do{',
21+
'try {', 'try{', 'try (',
22+
'catch ', 'catch(',
23+
'finally {', 'finally{',
24+
'switch ', 'switch(',
25+
'synchronized ', 'synchronized(',
26+
]
27+
28+
CONTINUATION_STARTS = [
29+
'||', '&&', '+', '-', '?', ':', '.',
30+
'|', '&', '^',
31+
]
32+
33+
34+
def is_control_flow_or_special(stripped):
35+
"""Return True if this line should keep same-line braces."""
36+
test = stripped
37+
if test.startswith('}'):
38+
test = test[1:].strip()
39+
40+
for prefix in CONTROL_PREFIXES:
41+
if test.startswith(prefix):
42+
return True
43+
44+
if '-> {' in stripped or '->{' in stripped:
45+
return True
46+
47+
if test == 'static {' or test == 'static{':
48+
return True
49+
50+
if re.search(r'\bnew\s+\w', stripped) and stripped.endswith(') {'):
51+
return True
52+
53+
if re.search(r'=\s*\{$', stripped):
54+
return True
55+
56+
if re.match(r'^[A-Z_][A-Z_0-9]*\s*\(.*\)\s*\{$', test):
57+
return True
58+
59+
return False
60+
61+
62+
def is_class_interface_enum(stripped):
63+
"""Return True if this is a class/interface/enum definition."""
64+
return bool(re.search(
65+
r'\b(class|interface|enum)\s+\w+', stripped
66+
))
67+
68+
69+
def is_continuation_line(stripped):
70+
"""Return True if this line starts with a continuation operator."""
71+
for op in CONTINUATION_STARTS:
72+
if stripped.startswith(op):
73+
return True
74+
return False
75+
76+
77+
def find_base_indent(lines, line_idx, current_indent):
78+
"""Scan backward to find the method's base indentation."""
79+
current_indent_len = len(current_indent)
80+
81+
for j in range(line_idx - 1, -1, -1):
82+
prev = lines[j]
83+
prev_stripped = prev.strip()
84+
85+
if not prev_stripped or prev_stripped.startswith('*') \
86+
or prev_stripped.startswith('//') \
87+
or prev_stripped.startswith('@'):
88+
continue
89+
90+
prev_rstripped = prev.rstrip('\n').rstrip('\r')
91+
prev_indent = prev_rstripped[:len(prev_rstripped)
92+
- len(prev_rstripped.lstrip())]
93+
if len(prev_indent) < current_indent_len:
94+
return prev_indent
95+
96+
return current_indent
97+
98+
99+
def process_file(filepath):
100+
"""Process a single Java file."""
101+
with open(filepath, 'r', encoding='utf-8') as f:
102+
lines = f.readlines()
103+
104+
new_lines = []
105+
changed = False
106+
in_block_comment = False
107+
108+
for i, line in enumerate(lines):
109+
rstripped = line.rstrip('\n').rstrip('\r')
110+
stripped = rstripped.strip()
111+
112+
if '/*' in stripped and '*/' not in stripped:
113+
in_block_comment = True
114+
if '*/' in stripped:
115+
in_block_comment = False
116+
117+
if in_block_comment or stripped.startswith('*') \
118+
or stripped.startswith('//'):
119+
new_lines.append(line)
120+
continue
121+
122+
# Only process lines ending with ' {' or '\t{'
123+
if not rstripped.endswith(' {') \
124+
and not rstripped.endswith('\t{'):
125+
new_lines.append(line)
126+
continue
127+
128+
indent = rstripped[:len(rstripped) - len(rstripped.lstrip())]
129+
130+
# Skip control flow and special blocks
131+
if is_control_flow_or_special(stripped):
132+
new_lines.append(line)
133+
continue
134+
135+
# Skip continuation lines
136+
if is_continuation_line(stripped):
137+
new_lines.append(line)
138+
continue
139+
140+
needs_allman = False
141+
brace_indent = indent
142+
143+
# Case 1: Class/interface/enum definition
144+
if is_class_interface_enum(stripped):
145+
needs_allman = True
146+
brace_indent = indent
147+
148+
# Case 2: Method/constructor with ') throws ... {'
149+
# e.g.: "public void foo() throws Exception {"
150+
elif re.search(
151+
r'\)\s+throws\s+[\w.,\s<>\[\]]+\{$', stripped):
152+
needs_allman = True
153+
# Extract everything before 'throws'
154+
m = re.search(r'^(.*\))\s+(throws\s+.+?)\s*\{$',
155+
rstripped)
156+
if m:
157+
method_part = m.group(1) # "...foo()"
158+
throws_part = m.group(2) # "throws Exception"
159+
# Find base indent
160+
if '(' in method_part.strip():
161+
brace_indent = indent
162+
else:
163+
brace_indent = find_base_indent(
164+
lines, i, indent)
165+
throws_indent = brace_indent + ' '
166+
new_lines.append(method_part + '\n')
167+
new_lines.append(throws_indent + throws_part
168+
+ '\n')
169+
new_lines.append(brace_indent + '{\n')
170+
changed = True
171+
continue
172+
173+
# Case 3: Method/constructor ending with ') {'
174+
elif stripped.endswith(') {'):
175+
needs_allman = True
176+
if '(' in stripped:
177+
brace_indent = indent
178+
else:
179+
brace_indent = find_base_indent(
180+
lines, i, indent)
181+
182+
# Case 4: Throws on a continuation line ending with '{'
183+
# e.g. line is " throws SQLException {"
184+
elif re.match(r'^\s*throws\s+[\w.,\s<>\[\]]+\{$',
185+
rstripped):
186+
needs_allman = True
187+
brace_indent = find_base_indent(lines, i, indent)
188+
189+
if needs_allman:
190+
content = rstripped.rstrip()
191+
if content.endswith(' {'):
192+
content = content[:-2].rstrip()
193+
elif content.endswith('\t{'):
194+
content = content[:-2].rstrip()
195+
elif content.endswith('{'):
196+
content = content[:-1].rstrip()
197+
198+
new_lines.append(content + '\n')
199+
new_lines.append(brace_indent + '{\n')
200+
changed = True
201+
else:
202+
new_lines.append(line)
203+
204+
if changed:
205+
with open(filepath, 'w', encoding='utf-8') as f:
206+
f.writelines(new_lines)
207+
return True
208+
return False
209+
210+
211+
def main():
212+
src_dir = 'src/main/java'
213+
if not os.path.isdir(src_dir):
214+
print(f"ERROR: {src_dir} not found. Run from project root.")
215+
sys.exit(1)
216+
217+
total_files = 0
218+
changed_files = 0
219+
220+
for root, dirs, files in os.walk(src_dir):
221+
for fname in sorted(files):
222+
if not fname.endswith('.java'):
223+
continue
224+
filepath = os.path.join(root, fname)
225+
total_files += 1
226+
if process_file(filepath):
227+
changed_files += 1
228+
print(f" Fixed: {filepath}")
229+
230+
print(f"\nProcessed {total_files} files, "
231+
f"modified {changed_files} files.")
232+
233+
234+
if __name__ == '__main__':
235+
main()

0 commit comments

Comments
 (0)