using CodePlay.Core.Interfaces; using CodePlay.Core.Models; using CodePlay.Core.Common; namespace CodePlay.Core.Converters; /// /// C# 到 Java 转换策略 /// public class CSharpToJavaStrategy : IConversionStrategy { /// /// 源语言 /// public LanguageType SourceLanguage => LanguageType.CSharp; /// /// 目标语言 /// public LanguageType TargetLanguage => LanguageType.Java; private readonly List _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"), }); } /// /// 映射类型 /// 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 "); } /// /// 转换语法节点 /// 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(node.Metadata), Parent = node.Parent, Children = new List(), 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; } } /// /// 类型映射 /// public class TypeMapping { public string SourceType { get; set; } public string TargetType { get; set; } public TypeMapping(string source, string target) { SourceType = source; TargetType = target; } }