using System.Text;
using CodePlay.Core.Interfaces;
using CodePlay.Core.Common;
namespace CodePlay.Core.Converters;
///
/// Java 代码生成器
///
public class JavaCodeGenerator : ICodeGenerator
{
private readonly StringBuilder _output = new();
private int _indentLevel;
///
/// 从语法树生成 Java 代码
///
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;
}
}