feat: 完成 C# 解析器测试、C# 转 Java 转换器和 Web API 控制器
核心功能实现: - 实现 CSharpToJavaStrategy 转换策略(包含类型映射) - 实现 CSharpToJavaConverter 转换器 - 实现 JavaCodeGenerator 代码生成器 - 实现 ConversionService 转换服务 - 实现 ConversionController Web API 控制器 - 注册 Swagger 文档和依赖注入 测试覆盖: - CSharpParserTests: 8 个测试用例 - CSharpToJavaConverterTests: 5 个测试用例 - 共 13 个测试全部通过 任务完成: - Task 1.2: 配置项目依赖 - Task 1.3: 建立基础架构 - Task 2.1: 实现 C# 解析器和测试 - Task 2.4: 实现 C# → Java 转换器 - Task 4.1: 创建 ASP.NET Core Web API Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
using CodePlay.Core.Interfaces;
|
||||
using CodePlay.Core.Models;
|
||||
using CodePlay.Core.Common;
|
||||
using CodePlay.Core.Parsers;
|
||||
|
||||
namespace CodePlay.Core.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// C# 到 Java 代码转换器
|
||||
/// </summary>
|
||||
public class CSharpToJavaConverter : IConverter
|
||||
{
|
||||
private readonly CSharpToJavaStrategy _strategy;
|
||||
private readonly ICodeGenerator _codeGenerator;
|
||||
|
||||
public CSharpToJavaConverter()
|
||||
{
|
||||
_strategy = new CSharpToJavaStrategy();
|
||||
_codeGenerator = new JavaCodeGenerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换语法树
|
||||
/// </summary>
|
||||
public async Task<ConversionResult> ConvertAsync(
|
||||
Interfaces.SyntaxTree syntaxTree,
|
||||
LanguageType targetLanguage,
|
||||
ConversionOptions? options = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = new ConversionResult
|
||||
{
|
||||
Success = false,
|
||||
Warnings = new List<ConversionWarning>(),
|
||||
Report = new ConversionReport()
|
||||
};
|
||||
|
||||
if (targetLanguage != LanguageType.Java)
|
||||
{
|
||||
result.ErrorMessage = "This converter only supports C# to Java conversion";
|
||||
return result;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var context = new ConversionContext
|
||||
{
|
||||
SourceLanguage = LanguageType.CSharp,
|
||||
TargetLanguage = LanguageType.Java,
|
||||
Options = options
|
||||
};
|
||||
|
||||
// 转换根节点
|
||||
var convertedRoot = _strategy.ConvertNode(syntaxTree.Root, context);
|
||||
|
||||
// 创建新的语法树
|
||||
var convertedTree = new Interfaces.SyntaxTree
|
||||
{
|
||||
Language = LanguageType.Java,
|
||||
Root = convertedRoot,
|
||||
SourceCode = syntaxTree.SourceCode
|
||||
};
|
||||
|
||||
// 保留注释
|
||||
if (options?.KeepComments == true)
|
||||
{
|
||||
convertedTree.Documentation = syntaxTree.Documentation
|
||||
.Select(d => new SyntaxDocumentation
|
||||
{
|
||||
ElementName = d.ElementName,
|
||||
Content = ConvertDocumentation(d.Content),
|
||||
Format = DocFormat.JavaDoc
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
// 生成 Java 代码
|
||||
var generatedCode = _codeGenerator.Generate(convertedTree);
|
||||
|
||||
result.TransformedCode = generatedCode;
|
||||
result.Success = true;
|
||||
|
||||
// 生成报告
|
||||
if (result.Report != null)
|
||||
{
|
||||
result.Report.LinesConverted = syntaxTree.SourceCode?.Split('\n').Length ?? 0;
|
||||
result.Report.ClassesConverted = CountClasses(syntaxTree.Root);
|
||||
result.Report.MethodsConverted = CountMethods(syntaxTree.Root);
|
||||
result.Report.TodoItems = context.TodoItems;
|
||||
result.Report.Issues = context.Issues;
|
||||
result.Report.TransformationLog = context.Logs;
|
||||
}
|
||||
|
||||
context.Logs.Add(new TransformationLog
|
||||
{
|
||||
Timestamp = DateTime.UtcNow,
|
||||
Operation = "Conversion",
|
||||
Details = "C# to Java conversion completed",
|
||||
Level = LogLevel.Info
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.Success = false;
|
||||
}
|
||||
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
private string ConvertDocumentation(string xmlDocContent)
|
||||
{
|
||||
// 简单的 XML Doc 到 JavaDoc 转换
|
||||
return xmlDocContent
|
||||
.Replace("///", " *")
|
||||
.Replace("<summary>", "")
|
||||
.Replace("</summary>", "")
|
||||
.Replace("<param name=", "@param ")
|
||||
.Replace("<returns>", "@return")
|
||||
.Replace("</returns>", "")
|
||||
.Replace("<exception cref=", "@throws ");
|
||||
}
|
||||
|
||||
private int CountClasses(Interfaces.SyntaxNode node)
|
||||
{
|
||||
int count = 0;
|
||||
if (node.Type == SyntaxNodeType.Class) count++;
|
||||
count += node.Children.Sum(CountClasses);
|
||||
return count;
|
||||
}
|
||||
|
||||
private int CountMethods(Interfaces.SyntaxNode node)
|
||||
{
|
||||
int count = 0;
|
||||
if (node.Type == SyntaxNodeType.Method) count++;
|
||||
count += node.Children.Sum(CountMethods);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user