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:
monkeycode-ai
2026-06-03 04:13:35 +00:00
parent a0971cf974
commit ae4de8a116
12 changed files with 1086 additions and 20 deletions
@@ -0,0 +1,149 @@
using CodePlay.Core.Interfaces;
using CodePlay.Core.Models;
using CodePlay.Core.Common;
namespace CodePlay.Core.Converters;
/// <summary>
/// C# 到 Java 转换策略
/// </summary>
public class CSharpToJavaStrategy : IConversionStrategy
{
/// <summary>
/// 源语言
/// </summary>
public LanguageType SourceLanguage => LanguageType.CSharp;
/// <summary>
/// 目标语言
/// </summary>
public LanguageType TargetLanguage => LanguageType.Java;
private readonly List<TypeMapping> _typeMappings = new();
public CSharpToJavaStrategy()
{
InitializeTypeMappings();
}
private void InitializeTypeMappings()
{
_typeMappings.AddRange(new[]
{
new TypeMapping("System.String", "java.lang.String"),
new TypeMapping("System.Int32", "int"),
new TypeMapping("System.Int64", "long"),
new TypeMapping("System.Boolean", "boolean"),
new TypeMapping("System.Double", "double"),
new TypeMapping("System.Single", "float"),
new TypeMapping("System.Object", "Object"),
new TypeMapping("System.Collections.Generic.List", "java.util.ArrayList"),
new TypeMapping("System.Collections.Generic.Dictionary", "java.util.HashMap"),
new TypeMapping("System.Collections.Generic.IEnumerable", "java.util.stream.Stream"),
new TypeMapping("System.Array", "java.util.Arrays"),
new TypeMapping("System.Console", "System.out"),
new TypeMapping("System.DateTime", "java.time.LocalDateTime"),
new TypeMapping("System.TimeSpan", "java.time.Duration"),
new TypeMapping("System.Exception", "Exception"),
new TypeMapping("System.ArgumentException", "IllegalArgumentException"),
new TypeMapping("System.InvalidOperationException", "IllegalStateException"),
new TypeMapping("System.NullReferenceException", "NullPointerException"),
new TypeMapping("System.Threading.Tasks.Task", "java.util.concurrent.CompletableFuture"),
});
}
/// <summary>
/// 映射类型
/// </summary>
public string MapType(string sourceType)
{
var mapping = _typeMappings.FirstOrDefault(m => sourceType.Contains(m.SourceType));
return mapping?.TargetType ?? sourceType
.Replace("var ", "Object ")
.Replace("public ", "public ")
.Replace("private ", "private ")
.Replace("protected ", "protected ");
}
/// <summary>
/// 转换语法节点
/// </summary>
public Interfaces.SyntaxNode ConvertNode(Interfaces.SyntaxNode node, ConversionContext context)
{
var newNode = new Interfaces.SyntaxNode
{
Type = node.Type,
Text = ConvertText(node.Text, context),
Metadata = new Dictionary<string, object?>(node.Metadata),
Parent = node.Parent,
Children = new List<Interfaces.SyntaxNode>(),
IsUnconvertible = node.IsUnconvertible,
TodoDescription = node.TodoDescription
};
foreach (var child in node.Children)
{
var convertedChild = ConvertNode(child, context);
convertedChild.Parent = newNode;
newNode.Children.Add(convertedChild);
}
return newNode;
}
private string ConvertText(string text, ConversionContext context)
{
var result = text;
// 命名空间转 package
if (result.StartsWith("namespace "))
{
result = result.Replace("namespace ", "package ")
.Replace("{", ";");
}
// using 转 import
if (result.StartsWith("using "))
{
result = result.Replace("using ", "import ")
.Replace(";", ";");
}
// 类型映射
foreach (var mapping in _typeMappings)
{
result = result.Replace(mapping.SourceType, mapping.TargetType);
}
// C# 特定语法处理
result = result.Replace("base.", "super.")
.Replace("this.", "this.")
.Replace("null", "null")
.Replace("true", "true")
.Replace("false", "false");
// 属性转方法
result = System.Text.RegularExpressions.Regex.Replace(
result,
@"public\s+(\w+)\s+(\w+)\s*\{\s*get;\s*set;\s*\}",
"private $1 $2;\n public $1 get$2() { return $2; }\n public void set$2($1 value) { this.$2 = value; }"
);
return result;
}
}
/// <summary>
/// 类型映射
/// </summary>
public class TypeMapping
{
public string SourceType { get; set; }
public string TargetType { get; set; }
public TypeMapping(string source, string target)
{
SourceType = source;
TargetType = target;
}
}