public static string Parse(string code, JavaConversionOptions options = null)
        {
            try
            {
                string result = JavaToCSharpConverter.ConvertText(code, options);

                return result;
            }
            catch (ParseException e)
            {
                if (Regex.IsMatch(e.Message, method_regex))
                {
                    string w = "public class foo {\n" + code + "\n}";
                    return Parse(w,
                        new JavaConversionOptions
                        {
                            IncludeNamespace = false,
                            IncludeUsings = false,
                            IncludeClass = false
                        });
                }
            }

            return null;

        }
Exemple #2
0
        static void Main(string[] args)
        {
            if (args == null || args.Length < 2)
            {
                Console.WriteLine("Usage: JavaToCSharpCli.exe [pathToJavaFile] [pathToCsOutputFile]");
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("Java input file doesn't exist!");
                return;
            }

            var javaText = File.ReadAllText(args[0]);

            // HACK for testing
            var options = new JavaConversionOptions()
                .AddPackageReplacement("org\\.apache\\.lucene", "Lucene.Net")
                .AddUsing("Lucene.Net")
                .AddUsing("Lucene.Net.Support")
                .AddUsing("Lucene.Net.Util");

            var parsed = JavaToCSharpConverter.ConvertText(javaText, options);

            File.WriteAllText(args[1], parsed);
            Console.WriteLine("Done!");
        }
        public static string ConvertText(string javaText, JavaConversionOptions options = null)
        {
            if (options == null)
                options = new JavaConversionOptions();

            options.ConversionStateChanged(ConversionState.Starting);

            var context = new ConversionContext(options);

            var textBytes = Encoding.UTF8.GetBytes(javaText ?? string.Empty);

            using (var stringreader = new MemoryStream(textBytes))
            using (var wrapper = new ikvm.io.InputStreamWrapper(stringreader))
            {
                options.ConversionStateChanged(ConversionState.ParsingJavaAST);

                var parsed = JavaParser.parse(wrapper);

                options.ConversionStateChanged(ConversionState.BuildingCSharpAST);

                var types = parsed.getTypes().ToList<TypeDeclaration>();
                var imports = parsed.getImports();
                if (imports == null || imports.isEmpty())
                    options.IncludeUsings = false;

                var package = parsed.getPackage();
                if (package == null)
                    options.IncludeNamespace = false;

                var usings = new List<UsingDirectiveSyntax>();

                //foreach (var import in imports)
                //{
                //    var usingSyntax = Syntax.UsingDirective(Syntax.ParseName(import.getName().toString()));
                //    usings.Add(usingSyntax);
                //}

                if (options.IncludeUsings)
                {
                    foreach (var ns in options.Usings)
                    {
                        var usingSyntax = Syntax.UsingDirective(Syntax.ParseName(ns));
                        usings.Add(usingSyntax);
                    }
                }

                var rootMembers = new List<MemberDeclarationSyntax>();
                NamespaceDeclarationSyntax namespaceSyntax = null;

                if (options.IncludeNamespace)
                {
                    string packageName = package.getName().toString();

                    foreach (var packageReplacement in options.PackageReplacements)
                    {
                        packageName = packageReplacement.Replace(packageName);
                    }

                    packageName = TypeHelper.Capitalize(packageName);

                    namespaceSyntax = Syntax.NamespaceDeclaration(Syntax.ParseName(packageName));
                }

                foreach (var type in types)
                {
                    if (type is ClassOrInterfaceDeclaration)
                    {
                        var classOrIntType = type as ClassOrInterfaceDeclaration;

                        if (classOrIntType.isInterface())
                        {
                            var interfaceSyntax = VisitInterfaceDeclaration(context, classOrIntType, false);

                            if (options.IncludeNamespace)
                                namespaceSyntax = namespaceSyntax.AddMembers(interfaceSyntax);
                            else if (options.IncludeClass)
                                rootMembers.Add(interfaceSyntax);
                        }
                        else
                        {
                            var classSyntax = VisitClassDeclaration(context, classOrIntType, false);

                            if (options.IncludeNamespace)
                                namespaceSyntax = namespaceSyntax.AddMembers(classSyntax);
                            else if (options.IncludeClass)
                                rootMembers.Add(classSyntax);
                            else
                                rootMembers.AddRange(classSyntax.Members.OfType<MethodDeclarationSyntax>());
                        }
                    }
                }

                if (options.IncludeNamespace)
                    rootMembers.Add(namespaceSyntax);

                var root = Syntax.CompilationUnit(
                    externs: null,
                    usings: Syntax.List(usings.ToArray()),
                    attributeLists: null,
                    members: Syntax.List<MemberDeclarationSyntax>(rootMembers))
                    .NormalizeWhitespace();

                var tree = SyntaxTree.Create(root);

                options.ConversionStateChanged(ConversionState.Done);

                return tree.GetText().ToString();
            }
        }
Exemple #4
0
        public static string ConvertText(string javaText, JavaConversionOptions options = null)
        {
            if (options == null)
            {
                options = new JavaConversionOptions();
            }

            options.ConversionStateChanged(ConversionState.Starting);

            var context = new ConversionContext(options);

            var textBytes = Encoding.UTF8.GetBytes(javaText ?? string.Empty);

            using (var stringreader = new MemoryStream(textBytes))
                using (var wrapper = new ikvm.io.InputStreamWrapper(stringreader))
                {
                    options.ConversionStateChanged(ConversionState.ParsingJavaAST);

                    var parsed = JavaParser.parse(wrapper);

                    options.ConversionStateChanged(ConversionState.BuildingCSharpAST);

                    var types   = parsed.getTypes().ToList <TypeDeclaration>();
                    var imports = parsed.getImports().ToList <ImportDeclaration>();
                    var package = parsed.getPackage();

                    var usings = new List <UsingDirectiveSyntax>();

                    //foreach (var import in imports)
                    //{
                    //    var usingSyntax = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(import.getName().toString()));
                    //    usings.Add(usingSyntax);
                    //}

                    if (options.IncludeUsings)
                    {
                        foreach (var ns in options.Usings.Where(x => !string.IsNullOrWhiteSpace(x)))
                        {
                            var usingSyntax = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(ns));
                            usings.Add(usingSyntax);
                        }
                    }

                    var rootMembers = new List <MemberDeclarationSyntax>();
                    NamespaceDeclarationSyntax namespaceSyntax = null;

                    if (options.IncludeNamespace)
                    {
                        string packageName = package.getName().toString();

                        foreach (var packageReplacement in options.PackageReplacements)
                        {
                            packageName = packageReplacement.Replace(packageName);
                        }

                        packageName = TypeHelper.Capitalize(packageName);

                        namespaceSyntax = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(packageName));
                    }

                    foreach (var type in types)
                    {
                        if (type is ClassOrInterfaceDeclaration)
                        {
                            var classOrIntType = type as ClassOrInterfaceDeclaration;

                            if (classOrIntType.isInterface())
                            {
                                var interfaceSyntax = VisitInterfaceDeclaration(context, classOrIntType, false);

                                if (options.IncludeNamespace)
                                {
                                    namespaceSyntax = namespaceSyntax.AddMembers(interfaceSyntax);
                                }
                                else
                                {
                                    rootMembers.Add(interfaceSyntax);
                                }
                            }
                            else
                            {
                                var classSyntax = VisitClassDeclaration(context, classOrIntType, false);

                                if (options.IncludeNamespace)
                                {
                                    namespaceSyntax = namespaceSyntax.AddMembers(classSyntax);
                                }
                                else
                                {
                                    rootMembers.Add(classSyntax);
                                }
                            }
                        }
                    }

                    if (options.IncludeNamespace)
                    {
                        rootMembers.Add(namespaceSyntax);
                    }

                    var root = SyntaxFactory.CompilationUnit(
                        externs: new SyntaxList <ExternAliasDirectiveSyntax>(),
                        usings: SyntaxFactory.List(usings.ToArray()),
                        attributeLists: new SyntaxList <AttributeListSyntax>(),
                        members: SyntaxFactory.List <MemberDeclarationSyntax>(rootMembers))
                               .NormalizeWhitespace();

                    var tree = SyntaxFactory.SyntaxTree(root);

                    options.ConversionStateChanged(ConversionState.Done);

                    return(tree.GetText().ToString());
                }
        }
        public void Convert()
        {
            var options = new JavaConversionOptions();
            options.ClearUsings();

            foreach (var ns in _usings)
            {
                options.AddUsing(ns);
            }

            options.IncludeUsings = _includeUsings;
            options.IncludeNamespace = _includeNamespace;
            options.IncludeComments = _includeComments;
            options.UseDebugAssertForAsserts = _useDebugAssertForAsserts;

            options.WarningEncountered += options_WarningEncountered;
            options.StateChanged += options_StateChanged;

            Task.Run(() =>
            {
                try
                {
                    var csharp = JavaToCSharpConverter.ConvertText(JavaText, options);

                    Dispatcher.CurrentDispatcher.Invoke(() => this.CSharpText = csharp);
                }
                catch (Exception ex)
                {
                    Dispatcher.CurrentDispatcher.Invoke(() =>
                    {
                        MessageBox.Show("There was an error converting the text to C#: " + ex.Message);
                    });
                }
            });
        }
 public ConversionContext(JavaConversionOptions options)
 {
     PendingAnonymousTypes = new Queue<ClassDeclarationSyntax>();
     UsedAnonymousTypeNames = new HashSet<string>();
     this.Options = options;
 }
 public ConversionContext(JavaConversionOptions options)
 {
     PendingAnonymousTypes  = new Queue <ClassDeclarationSyntax>();
     UsedAnonymousTypeNames = new HashSet <string>();
     Options = options;
 }