fix: java-scanner提取方法体
✅ 修改 extractMethods: - 完全重写方法提取逻辑 - 逐行扫描Java源码 - 提取方法签名的同时提取方法体 - 使用括号计数器精确提取方法体边界 🎯 效果: - method.methodBody 现在包含完整的Java方法体 - service-generator可以拿到Java业务逻辑进行转换
This commit is contained in:
@@ -356,43 +356,77 @@ class JavaScanner {
|
||||
|
||||
/**
|
||||
* 提取方法
|
||||
*
|
||||
* ✅ 增强:提取方法体,用于业务逻辑转换
|
||||
*/
|
||||
extractMethods(content) {
|
||||
// 匹配各种访问修饰符的方法:public, protected, private, 或 package-private (无修饰符)
|
||||
const methodMatches = content.match(/(public|protected|private)?\s+[\w<>\[\]]+\s+(\w+)\s*\([^)]*\)\s*\{/g);
|
||||
if (!methodMatches) return [];
|
||||
const methods = [];
|
||||
const lines = content.split('\n');
|
||||
|
||||
const methods = methodMatches.map(match => {
|
||||
// 提取方法名
|
||||
const methodNameMatch = match.match(/(public|protected|private)?\s+[\w<>\[\]]+\s+(\w+)\s*\(/);
|
||||
const methodName = methodNameMatch ? methodNameMatch[2] : 'unknown';
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
// 提取访问修饰符
|
||||
const accessModifier = methodNameMatch && methodNameMatch[1] ? methodNameMatch[1] : 'package';
|
||||
// 查找方法定义行(包含访问修饰符、返回类型、方法名、参数、左大括号)
|
||||
const methodMatch = line.match(/(public|protected|private)?\s+([\w<>\[\]]+)\s+(\w+)\s*\([^{]*\{/);
|
||||
if (!methodMatch) continue;
|
||||
|
||||
// 提取参数信息(特别是事件类型)
|
||||
const paramMatch = match.match(/\(([^)]*)\)/);
|
||||
const accessModifier = methodMatch[1] || 'package';
|
||||
const returnType = methodMatch[2];
|
||||
const methodName = methodMatch[3];
|
||||
|
||||
// 提取参数
|
||||
const paramMatch = line.match(/\(([^)]*)\)/);
|
||||
const parameters = this.extractMethodParameters(line);
|
||||
|
||||
// 提取事件类型(用于Listener)
|
||||
let eventType = null;
|
||||
if (paramMatch && paramMatch[1]) {
|
||||
const params = paramMatch[1].trim();
|
||||
// 提取事件类型,如: MemberLoginEvent event
|
||||
const eventTypeMatch = params.match(/(\w+Event)\s+\w+/);
|
||||
if (eventTypeMatch) {
|
||||
eventType = eventTypeMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// ✅ 提取方法体(从左大括号后开始,到匹配的右大括号)
|
||||
let braceCount = (line.match(/\{/g) || []).length - (line.match(/\}/g) || []).length;
|
||||
let methodBody = '';
|
||||
|
||||
// 获取左大括号后的内容
|
||||
const braceIndex = line.indexOf('{');
|
||||
if (braceIndex !== -1) {
|
||||
methodBody = line.substring(braceIndex + 1);
|
||||
}
|
||||
|
||||
// 继续读取后续行,直到找到匹配的右大括号
|
||||
for (let j = i + 1; j < lines.length && braceCount > 0; j++) {
|
||||
const currentLine = lines[j];
|
||||
braceCount += (currentLine.match(/\{/g) || []).length - (currentLine.match(/\}/g) || []).length;
|
||||
|
||||
if (braceCount > 0) {
|
||||
methodBody += '\n' + currentLine;
|
||||
} else {
|
||||
// 最后一行,只取右大括号之前的内容
|
||||
const lastBraceIndex = currentLine.lastIndexOf('}');
|
||||
if (lastBraceIndex !== -1) {
|
||||
methodBody += '\n' + currentLine.substring(0, lastBraceIndex);
|
||||
}
|
||||
}
|
||||
|
||||
i = j; // 更新外层循环的索引
|
||||
}
|
||||
|
||||
methods.push({
|
||||
methodName: methodName,
|
||||
accessModifier: accessModifier,
|
||||
signature: match,
|
||||
returnType: this.extractReturnType(match),
|
||||
parameters: this.extractMethodParameters(match),
|
||||
eventType: eventType
|
||||
};
|
||||
signature: line,
|
||||
returnType: returnType,
|
||||
parameters: parameters,
|
||||
eventType: eventType,
|
||||
methodBody: methodBody.trim() // ✅ 新增:保存方法体
|
||||
});
|
||||
}
|
||||
|
||||
// 只返回public方法用于Service层
|
||||
return methods;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user