db536cfb2c
核心修复: - 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败) - 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements) - 修复 PropertyConverter init-only 属性组索引 新增转换器 (C# 8-13 特性): - NullCoalescingConverter: ??、?.、??= 运算符转换 - SwitchExpressionConverter: switch 表达式→if-else 链 - PrimaryConstructorConverter: 主构造函数→传统构造函数 增强: - LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等 - AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误 - NamingConverter: PascalCase→camelCase 命名转换 - DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录 - XML Doc→JavaDoc 格式转换与注释保留 新增测试: - CSharpToJavaEdgeCaseTests: 16 个边界测试 - CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试 - 从 164 增加到 179 总测试 (168 通过, 0 失败) 新增文件: - Pipeline/Converters/NullCoalescingConverter.cs - Pipeline/Converters/SwitchExpressionConverter.cs - Pipeline/Converters/PrimaryConstructorConverter.cs - Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs - Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs - Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
139 lines
4.6 KiB
C#
139 lines
4.6 KiB
C#
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.Select(i => new IssueInfo { Description = i.Description, Severity = i.Severity, Line = i.Line, Suggestion = i.Suggestion }).ToList();
|
|
result.Report.TransformationLog = context.Logs.Select(l => new TransformationLogEntry { Timestamp = l.Timestamp, Operation = l.Operation, Details = l.Details, Level = l.Level.ToString() }).ToList();
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|