# -*- coding: utf-8 -*- import os import re base_path = r"c:\Users\13603\Desktop\健身房项目\gym-manage\gym-manage-api\gym-member\src\main\java\cn\novalon\gym\manage\member" count = 0 for root, dirs, files in os.walk(base_path): for file in files: if file.endswith('.java'): filepath = os.path.join(root, file) with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # 匹配模式:两个连续的注释块(类文档 + 作者注释) pattern = r'(/\*\*[\s\S]*?\*/)\s*\n(/\*\*\s*\n\s*\*\s*@author\s+.+\s*\n\s*\*\s*@date\s+\d{4}-\d{2}-\d{2}\s*\n\s*\*/)' match = re.search(pattern, content) if match: class_doc = match.group(1) author_doc = match.group(2) # 提取作者信息 author_match = re.search(r'@author\s+(.+)', author_doc) date_match = re.search(r'@date\s+(\d{4}-\d{2}-\d{2})', author_doc) if author_match and date_match: author = author_match.group(1).strip() date = date_match.group(1).strip() # 提取类文档的内容(去掉 /** 和 */) class_doc_content = re.sub(r'/\*\*\s*\n|\s*\*/', '', class_doc).strip() # 清理每行开头的多余星号和空格 class_doc_lines = class_doc_content.split('\n') cleaned_lines = [] for line in class_doc_lines: cleaned_line = re.sub(r'^\s*\*\s?', '', line).strip() if cleaned_line: cleaned_lines.append(cleaned_line) class_doc_content = '\n * '.join(cleaned_lines) # 构建合并后的注释 merged_doc = f"""/** * {class_doc_content} * * @author {author} * @date {date} */""" # 替换原文 new_content = content[:match.start()] + merged_doc + content[match.end():] with open(filepath, 'w', encoding='utf-8') as f: f.write(new_content) count += 1 print(f"已合并: {file}") else: # 检查是否有单独的作者注释在类文档之前 pattern2 = r'/\*\*\s*\n\s*\*\s*@author\s+.+\s*\n\s*\*\s*@date\s+\d{4}-\d{2}-\d{2}\s*\n\s*\*/\s*\n(/\*\*[\s\S]*?\*/)' match2 = re.search(pattern2, content) if match2: author_doc = match2.group(0).split('\n/**')[0] + '\n/**' class_doc = '/**' + match2.group(1) # 提取作者信息 author_match = re.search(r'@author\s+(.+)', author_doc) date_match = re.search(r'@date\s+(\d{4}-\d{2}-\d{2})', author_doc) if author_match and date_match: author = author_match.group(1).strip() date = date_match.group(1).strip() # 提取类文档的内容 class_doc_content = re.sub(r'/\*\*\s*\n|\s*\*/', '', class_doc).strip() # 清理每行开头的多余星号和空格 class_doc_lines = class_doc_content.split('\n') cleaned_lines = [] for line in class_doc_lines: cleaned_line = re.sub(r'^\s*\*\s?', '', line).strip() if cleaned_line: cleaned_lines.append(cleaned_line) class_doc_content = '\n * '.join(cleaned_lines) # 构建合并后的注释 merged_doc = f"""/** * {class_doc_content} * * @author {author} * @date {date} */""" # 替换原文 new_content = content[:match2.start()] + merged_doc + content[match2.end():] with open(filepath, 'w', encoding='utf-8') as f: f.write(new_content) count += 1 print(f"已合并: {file}") print(f"\n完成!共合并 {count} 个文件")