反编译单个 .class 或 .jar 文件 Args: file_path: 要反编译的文件路径(.class 或 .jar) output_dir: 输出目录,默认为文件所在目录下的 decompiled 文件夹 save_to_file: 是否直接保存到文件系统(推荐),默认为 True。设为 False 时会返回反编译内容 Returns: 反编译结果信息或内容
(
file_path: str,
output_dir: Optional[str] = None,
save_to_file: bool = True
)
| 114 | |
| 115 | @mcp.tool() |
| 116 | def decompile_file( |
| 117 | file_path: str, |
| 118 | output_dir: Optional[str] = None, |
| 119 | save_to_file: bool = True |
| 120 | ) -> str: |
| 121 | """ |
| 122 | 反编译单个 .class 或 .jar 文件 |
| 123 | |
| 124 | Args: |
| 125 | file_path: 要反编译的文件路径(.class 或 .jar) |
| 126 | output_dir: 输出目录,默认为文件所在目录下的 decompiled 文件夹 |
| 127 | save_to_file: 是否直接保存到文件系统(推荐),默认为 True。设为 False 时会返回反编译内容 |
| 128 | |
| 129 | Returns: |
| 130 | 反编译结果信息或内容 |
| 131 | """ |
| 132 | file_path = os.path.abspath(file_path) |
| 133 | |
| 134 | if not os.path.isfile(file_path): |
| 135 | return f"错误:文件不存在 - {file_path}" |
| 136 | |
| 137 | ext = os.path.splitext(file_path)[1].lower() |
| 138 | if ext not in (".class", ".jar"): |
| 139 | return f"错误:不支持的文件类型 - {ext},仅支持 .class 和 .jar" |
| 140 | |
| 141 | # 确定输出目录 |
| 142 | if output_dir is None: |
| 143 | output_dir = os.path.join(os.path.dirname(file_path), "decompiled") |
| 144 | output_dir = os.path.abspath(output_dir) |
| 145 | os.makedirs(output_dir, exist_ok=True) |
| 146 | |
| 147 | # 获取 CFR |
| 148 | try: |
| 149 | cfr_path = ensure_cfr() |
| 150 | except Exception as e: |
| 151 | return f"错误:无法获取 CFR 反编译器 - {str(e)}" |
| 152 | |
| 153 | # 执行反编译 |
| 154 | success, message = run_cfr(cfr_path, file_path, output_dir) |
| 155 | |
| 156 | if success: |
| 157 | # 统计生成的文件 |
| 158 | java_files = list(Path(output_dir).rglob("*.java")) |
| 159 | result = { |
| 160 | "success": True, |
| 161 | "message": "反编译完成", |
| 162 | "file_count": len(java_files), |
| 163 | "output_dir": output_dir, |
| 164 | "source_file": file_path |
| 165 | } |
| 166 | |
| 167 | if save_to_file: |
| 168 | return ( |
| 169 | f"✅ 反编译成功\n" |
| 170 | f"源文件: {file_path}\n" |
| 171 | f"输出目录: {output_dir}\n" |
| 172 | f"生成文件数: {len(java_files)}\n" |
| 173 | f"提示: 反编译结果已保存到文件系统" |
nothing calls this directly
no test coverage detected