核心功能实现: - 实现 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 <[email protected]>
187 lines
5.1 KiB
C#
187 lines
5.1 KiB
C#
using System.Text;
|
|
using CodePlay.Core.Interfaces;
|
|
using CodePlay.Core.Common;
|
|
|
|
namespace CodePlay.Core.Converters;
|
|
|
|
/// <summary>
|
|
/// Java 代码生成器
|
|
/// </summary>
|
|
public class JavaCodeGenerator : ICodeGenerator
|
|
{
|
|
private readonly StringBuilder _output = new();
|
|
private int _indentLevel;
|
|
|
|
/// <summary>
|
|
/// 从语法树生成 Java 代码
|
|
/// </summary>
|
|
public string Generate(Interfaces.SyntaxTree syntaxTree)
|
|
{
|
|
_output.Clear();
|
|
_indentLevel = 0;
|
|
|
|
// 生成 JavaDoc
|
|
foreach (var doc in syntaxTree.Documentation)
|
|
{
|
|
_output.AppendLine("/**");
|
|
_output.AppendLine($" * {doc.Content}");
|
|
_output.AppendLine(" */");
|
|
}
|
|
|
|
// 生成代码
|
|
GenerateNode(syntaxTree.Root);
|
|
|
|
return _output.ToString();
|
|
}
|
|
|
|
private void GenerateNode(Interfaces.SyntaxNode node)
|
|
{
|
|
switch (node.Type)
|
|
{
|
|
case SyntaxNodeType.CompilationUnit:
|
|
GenerateCompilationUnit(node);
|
|
break;
|
|
case SyntaxNodeType.Namespace:
|
|
GenerateNamespace(node);
|
|
break;
|
|
case SyntaxNodeType.Class:
|
|
GenerateClass(node);
|
|
break;
|
|
case SyntaxNodeType.Method:
|
|
GenerateMethod(node);
|
|
break;
|
|
case SyntaxNodeType.Property:
|
|
GenerateProperty(node);
|
|
break;
|
|
case SyntaxNodeType.Field:
|
|
GenerateField(node);
|
|
break;
|
|
default:
|
|
GenerateDefault(node);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void GenerateCompilationUnit(Interfaces.SyntaxNode node)
|
|
{
|
|
foreach (var child in node.Children)
|
|
{
|
|
GenerateNode(child);
|
|
}
|
|
}
|
|
|
|
private void GenerateNamespace(Interfaces.SyntaxNode node)
|
|
{
|
|
// 提取 package 声明
|
|
var packageLine = node.Text.StartsWith("package ")
|
|
? node.Text.Split(';')[0] + ";"
|
|
: $"package com.codeplay.converted;";
|
|
|
|
_output.AppendLine(packageLine);
|
|
_output.AppendLine();
|
|
|
|
foreach (var child in node.Children)
|
|
{
|
|
GenerateNode(child);
|
|
}
|
|
}
|
|
|
|
private void GenerateClass(Interfaces.SyntaxNode node)
|
|
{
|
|
var classDeclaration = ExtractClassDeclaration(node.Text);
|
|
_output.AppendLine(Indent(classDeclaration));
|
|
_output.AppendLine(Indent("{"));
|
|
_indentLevel++;
|
|
|
|
foreach (var child in node.Children)
|
|
{
|
|
if (child.Type != SyntaxNodeType.Unknown)
|
|
{
|
|
GenerateNode(child);
|
|
}
|
|
}
|
|
|
|
_indentLevel--;
|
|
_output.AppendLine(Indent("}"));
|
|
_output.AppendLine();
|
|
}
|
|
|
|
private void GenerateMethod(Interfaces.SyntaxNode node)
|
|
{
|
|
var methodSignature = ExtractMethodSignature(node.Text);
|
|
_output.AppendLine(Indent(methodSignature));
|
|
_output.AppendLine(Indent("{"));
|
|
_indentLevel++;
|
|
|
|
// 生成方法体(简化处理)
|
|
var methodBody = ExtractMethodBody(node.Text);
|
|
if (!string.IsNullOrWhiteSpace(methodBody))
|
|
{
|
|
_output.AppendLine(Indent(methodBody));
|
|
}
|
|
|
|
_indentLevel--;
|
|
_output.AppendLine(Indent("}"));
|
|
_output.AppendLine();
|
|
}
|
|
|
|
private void GenerateProperty(Interfaces.SyntaxNode node)
|
|
{
|
|
var propertyDeclaration = node.Text;
|
|
_output.AppendLine(Indent(propertyDeclaration));
|
|
}
|
|
|
|
private void GenerateField(Interfaces.SyntaxNode node)
|
|
{
|
|
var fieldDeclaration = node.Text;
|
|
_output.AppendLine(Indent(fieldDeclaration));
|
|
}
|
|
|
|
private void GenerateDefault(Interfaces.SyntaxNode node)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(node.Text))
|
|
{
|
|
_output.AppendLine(Indent(node.Text));
|
|
}
|
|
|
|
foreach (var child in node.Children)
|
|
{
|
|
GenerateNode(child);
|
|
}
|
|
}
|
|
|
|
private string Indent(string text)
|
|
{
|
|
var indent = new string(' ', _indentLevel * 4);
|
|
return indent + text;
|
|
}
|
|
|
|
private string ExtractClassDeclaration(string text)
|
|
{
|
|
// 简化处理:替换 class 修饰符
|
|
return text.Replace("public class", "public class")
|
|
.Replace("abstract class", "public abstract class")
|
|
.Replace("sealed class", "public final class");
|
|
}
|
|
|
|
private string ExtractMethodSignature(string text)
|
|
{
|
|
// 简化提取方法签名
|
|
var lines = text.Split('\n');
|
|
return lines.FirstOrDefault(l => l.Trim().Length > 0 && !l.Trim().StartsWith("{"))?.Trim() ?? text;
|
|
}
|
|
|
|
private string ExtractMethodBody(string text)
|
|
{
|
|
var startIndex = text.IndexOf('{');
|
|
var endIndex = text.LastIndexOf('}');
|
|
|
|
if (startIndex >= 0 && endIndex > startIndex)
|
|
{
|
|
return text.Substring(startIndex + 1, endIndex - startIndex - 1).Trim();
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
}
|