#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 批量修改Java文件中的行尾注释脚本 将行尾注释改为单独占行的注释 """ import os import re import sys from pathlib import Path def find_java_files(root_path): """查找所有Java文件""" java_files = [] for root, dirs, files in os.walk(root_path): for file in files: if file.endswith('.java'): java_files.append(os.path.join(root, file)) return java_files def process_file(file_path): """处理单个Java文件""" try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() original_content = content changes = [] # 分行处理 lines = content.split('\n') new_lines = [] for i, line in enumerate(lines): # 检查是否包含行尾注释 # 模式1: 代码行后跟 // 注释 match1 = re.match(r'^(\s*)(.*?)(;)\s*(//\s*(.*))\s*$', line) if match1: indent = match1.group(1) code_part = match1.group(2) semicolon = match1.group(3) comment_part = match1.group(4) comment_text = match1.group(5) # 添加单独的注释行 new_lines.append(f'{indent}{comment_part}') # 添加代码行 new_lines.append(f'{indent}{code_part}{semicolon}') changes.append(f'第{i+1}行: {line.strip()} -> 拆分为注释行和代码行') continue # 模式2: 代码行后跟 /* 注释 */ match2 = re.match(r'^(\s*)(.*?)(;)\s*(/\*\s*(.*?)\s*\*/)\s*$', line) if match2: indent = match2.group(1) code_part = match2.group(2) semicolon = match2.group(3) comment_text = match2.group(5) # 添加单独的注释行(转换为//格式) new_lines.append(f'{indent}// {comment_text}') # 添加代码行 new_lines.append(f'{indent}{code_part}{semicolon}') changes.append(f'第{i+1}行: {line.strip()} -> 拆分为注释行和代码行') continue # 模式3: 其他行尾注释情况(不以分号结尾) match3 = re.match(r'^(\s*)(.*?)\s*(//\s*(.*))\s*$', line) if match3 and not line.strip().startswith('//'): indent = match3.group(1) code_part = match3.group(2).strip() comment_part = match3.group(3) comment_text = match3.group(4) # 确保不是整行注释 if code_part and not code_part.startswith('//'): # 添加单独的注释行 new_lines.append(f'{indent}{comment_part}') # 添加代码行 new_lines.append(f'{indent}{code_part}') changes.append(f'第{i+1}行: {line.strip()} -> 拆分为注释行和代码行') continue # 模式4: 其他行尾注释情况(/* */格式,不以分号结尾) match4 = re.match(r'^(\s*)(.*?)\s*(/\*\s*(.*?)\s*\*/)\s*$', line) if match4 and not line.strip().startswith('/*'): indent = match4.group(1) code_part = match4.group(2).strip() comment_text = match4.group(4) # 确保不是整行注释 if code_part and not code_part.startswith('/*'): # 添加单独的注释行(转换为//格式) new_lines.append(f'{indent}// {comment_text}') # 添加代码行 new_lines.append(f'{indent}{code_part}') changes.append(f'第{i+1}行: {line.strip()} -> 拆分为注释行和代码行') continue # 没有匹配的模式,保持原样 new_lines.append(line) # 如果有变化,写入文件 if changes: new_content = '\n'.join(new_lines) with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) return len(changes), changes else: return 0, [] except Exception as e: print(f"处理文件 {file_path} 时出错: {e}") return 0, [] def main(): """主函数""" # 项目根目录 root_path = '/Users/leocoder/leocoder/develop/templates/coder-common-thin/coder-common-thin-backend' # 查找所有Java文件 java_files = find_java_files(root_path) print(f"找到 {len(java_files)} 个Java文件") print("开始处理...") total_changes = 0 modified_files = [] for file_path in java_files: change_count, changes = process_file(file_path) if change_count > 0: total_changes += change_count modified_files.append({ 'file': file_path, 'changes': change_count, 'details': changes }) print(f"✓ 修改了 {file_path} - {change_count} 处更改") print("\n" + "="*80) print("修改报告") print("="*80) print(f"总共修改了 {len(modified_files)} 个文件") print(f"总共修改了 {total_changes} 处行尾注释") if modified_files: print("\n详细修改列表:") for file_info in modified_files: print(f"\n文件: {file_info['file']}") print(f"修改数量: {file_info['changes']}") for detail in file_info['details'][:5]: # 只显示前5个变化 print(f" - {detail}") if len(file_info['details']) > 5: print(f" ... 还有 {len(file_info['details']) - 5} 个变化") print("\n处理完成!") if __name__ == "__main__": main()