analyze_class
Analyze Java class structures, methods, and fields by scanning Maven projects and decompiling JAR files to provide accurate class information for code generation.
Instructions
分析Java类的结构、方法、字段等信息
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| className | Yes | 要分析的Java类全名 | |
| projectPath | Yes | Maven项目根目录路径 |
Input Schema (JSON Schema)
{
"properties": {
"className": {
"description": "要分析的Java类全名",
"type": "string"
},
"projectPath": {
"description": "Maven项目根目录路径",
"type": "string"
}
},
"required": [
"className",
"projectPath"
],
"type": "object"
}
Implementation Reference
- src/index.ts:32-34 (registration)Tool capability registration in server capabilitiesanalyze_class: { description: '分析Java类的结构、方法、字段等信息', },
- src/index.ts:100-113 (schema)Input schema for analyze_class toolinputSchema: { type: 'object', properties: { className: { type: 'string', description: '要分析的Java类全名', }, projectPath: { type: 'string', description: 'Maven项目根目录路径', }, }, required: ['className', 'projectPath'], },
- src/index.ts:128-129 (registration)Tool name registration in CallToolRequestHandler switch statementcase 'analyze_class': return await this.handleAnalyzeClass(args);
- src/index.ts:209-248 (handler)Primary MCP tool handler for analyze_class: extracts args, ensures index exists, delegates to analyzer, formats analysis into text responseprivate async handleAnalyzeClass(args: any) { const { className, projectPath } = args; // 检查索引是否存在,如果不存在则先创建 await this.ensureIndexExists(projectPath); const analysis = await this.analyzer.analyzeClass(className, projectPath); let result = `类 ${className} 的分析结果:\n\n`; result += `包名: ${analysis.packageName}\n`; result += `类名: ${analysis.className}\n`; result += `修饰符: ${analysis.modifiers.join(' ')}\n`; result += `父类: ${analysis.superClass || '无'}\n`; result += `实现的接口: ${analysis.interfaces.join(', ') || '无'}\n\n`; if (analysis.fields.length > 0) { result += `字段 (${analysis.fields.length}个):\n`; analysis.fields.forEach(field => { result += ` - ${field.modifiers.join(' ')} ${field.type} ${field.name}\n`; }); result += '\n'; } if (analysis.methods.length > 0) { result += `方法 (${analysis.methods.length}个):\n`; analysis.methods.forEach(method => { result += ` - ${method.modifiers.join(' ')} ${method.returnType} ${method.name}(${method.parameters.join(', ')})\n`; }); result += '\n'; } return { content: [ { type: 'text', text: result, }, ], }; }
- Core helper method implementing class analysis: finds JAR, runs javap, returns structured ClassAnalysisasync analyzeClass(className: string, projectPath: string): Promise<ClassAnalysis> { try { // 1. 获取类文件路径 const jarPath = await this.scanner.findJarForClass(className, projectPath); if (!jarPath) { throw new Error(`未找到类 ${className} 对应的JAR包`); } // 2. 直接使用 javap 分析JAR包中的类 const analysis = await this.analyzeClassWithJavap(jarPath, className); return analysis; } catch (error) { console.error(`分析类 ${className} 失败:`, error); throw error; } }
- Type schema for ClassAnalysis output structure used by the analyzerexport interface ClassAnalysis { className: string; packageName: string; modifiers: string[]; superClass?: string; interfaces: string[]; fields: ClassField[]; methods: ClassMethod[]; }