Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion JavaToCSharp.Tests/CommentTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.CodeAnalysis.CSharp;
using Xunit.Abstractions;

namespace JavaToCSharp.Tests;
Expand Down
4 changes: 2 additions & 2 deletions JavaToCSharp.Tests/JavaToCSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<WarningsAsErrors>nullable</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,7 +26,7 @@
<ItemGroup>
<ProjectReference Include="..\JavaToCSharp\JavaToCSharp.csproj" />
</ItemGroup>

<ItemGroup>
<IkvmReference Include="../Lib/javaparser-core-3.25.4.jar" />
</ItemGroup>
Expand Down
55 changes: 0 additions & 55 deletions JavaToCSharp.sln

This file was deleted.

12 changes: 12 additions & 0 deletions JavaToCSharp.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Solution>
<Folder Name="/Lib/">
<File Path="Lib/javaparser-core-3.25.4.jar" />
</Folder>
<Folder Name="/Solution Items/">
<File Path=".editorconfig" />
</Folder>
<Project Path="JavaToCSharp.Tests/JavaToCSharp.Tests.csproj" />
<Project Path="JavaToCSharp/JavaToCSharp.csproj" />
<Project Path="JavaToCSharpCli/JavaToCSharpCli.csproj" />
<Project Path="JavaToCSharpGui/JavaToCSharpGui.csproj" />
</Solution>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Avalonia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Cascadia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=initializers/@EntryIndexedValue">True</s:Boolean>
Expand Down
46 changes: 26 additions & 20 deletions JavaToCSharp/CommentsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Microsoft.CodeAnalysis;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using JavaAst = com.github.javaparser.ast;
using JavaComments = com.github.javaparser.ast.comments;
using JavaParser = com.github.javaparser;
using SysRegex = System.Text.RegularExpressions;

namespace JavaToCSharp;

public static class CommentsHelper
public static partial class CommentsHelper
{
private enum CommentPosition
{
Expand All @@ -18,8 +18,8 @@ private enum CommentPosition
}

// Regex: Optional *, capture optional @par, capture optional text (keep leading whitespaces, trim end).
private static readonly SysRegex.Regex _analyzeDocString =
new(@"^(\s*\*)?(\s*(?<param>@[a-z]+))?\s?(?<text>.*?)\s*$", SysRegex.RegexOptions.Compiled);
[GeneratedRegex(@"^(\s*\*)?(\s*(?<param>@[a-z]+))?\s?(?<text>.*?)\s*$", RegexOptions.Compiled)]
private static partial Regex AnalyzeDocStringRegex { get; }

private static readonly Dictionary<string, string> _knownTagsDict = new()
{
Expand Down Expand Up @@ -142,9 +142,14 @@ private static (SyntaxKind kind, string? pre, string? post) GetCommentInfo(
private static List<(JavaComments.Comment c, CommentPosition pos)> GatherComments(JavaAst.Node? node)
{
var result = new List<(JavaComments.Comment c, CommentPosition pos)>();
if (node == null) return result;

if (node is null)
{
return result;
}

var parentNode = node.getParentNode().FromOptional<JavaAst.Node>();

if (parentNode is null)
{
if (node.getComment().FromOptional<JavaComments.Comment>() is { } comment)
Expand All @@ -155,6 +160,7 @@ private static (SyntaxKind kind, string? pre, string? post) GetCommentInfo(
else
{
var unsortedComments = parentNode.getAllContainedComments();

if (unsortedComments.size() != 0)
{
var comments = unsortedComments.OfType<JavaComments.Comment>()
Expand All @@ -163,7 +169,7 @@ private static (SyntaxKind kind, string? pre, string? post) GetCommentInfo(
.ToList();

// Find leading comments
var nodeBegin = node.getBegin().FromOptional<JavaParser.Position>()
var nodeBegin = node.getBegin().FromOptional<JavaParser.Position>()
?? throw new InvalidOperationException("Node did not have a begin position");
var previousSibling = GetPreviousSibling(parentNode, nodeBegin);
int previousPos = previousSibling?.getEnd().FromOptional<JavaParser.Position>()?.line ?? 0;
Expand All @@ -173,7 +179,7 @@ private static (SyntaxKind kind, string? pre, string? post) GetCommentInfo(
// Find trailing comments.
// We consider only comments either appearing on the same line or, if no sibling nodes follow,
// then also comments on the succeeding lines (because otherwise they belong to the next sibling).
var nodeEnd = node.getEnd().FromOptional<JavaParser.Position>()
var nodeEnd = node.getEnd().FromOptional<JavaParser.Position>()
?? throw new InvalidOperationException("Node did not have an end position");

var trailingComments = HasNextSibling(parentNode, nodeEnd)
Expand All @@ -191,7 +197,7 @@ private static (SyntaxKind kind, string? pre, string? post) GetCommentInfo(
comments.Where(c =>
{
var commentBegin = c.getBegin().FromOptional<JavaParser.Position>();
return commentBegin != null && (commentBegin.line == nodeEnd.line && commentBegin.column > nodeEnd.column || commentBegin.line > nodeEnd.line);
return commentBegin is not null && (commentBegin.line == nodeEnd.line && commentBegin.column > nodeEnd.column || commentBegin.line > nodeEnd.line);
})
.Select(c => (c, CommentPosition.Trailing));

Expand All @@ -200,7 +206,7 @@ private static (SyntaxKind kind, string? pre, string? post) GetCommentInfo(
.Where(c =>
{
var commentBegin = c.getBegin().FromOptional<JavaParser.Position>();
return commentBegin != null && commentBegin.line == nodeEnd.line && commentBegin.column > nodeEnd.column;
return commentBegin is not null && commentBegin.line == nodeEnd.line && commentBegin.column > nodeEnd.column;
})
.Select(c => (c, CommentPosition.Trailing));

Expand All @@ -212,7 +218,7 @@ private static bool HasNextSibling(JavaAst.Node parentNode, JavaParser.Position
.Any(sibling =>
{
var siblingBegin = sibling.getBegin().FromOptional<JavaParser.Position>();
return siblingBegin != null && (siblingBegin.line > nodeEnd.line || siblingBegin.line == nodeEnd.line && siblingBegin.column > nodeEnd.column);
return siblingBegin is not null && (siblingBegin.line > nodeEnd.line || siblingBegin.line == nodeEnd.line && siblingBegin.column > nodeEnd.column);
});
}

Expand All @@ -222,7 +228,7 @@ private static bool HasNextSibling(JavaAst.Node parentNode, JavaParser.Position
{
var commentBegin = c.getBegin().FromOptional<JavaParser.Position>();
var commentEnd = c.getEnd().FromOptional<JavaParser.Position>();
return commentBegin != null && commentEnd != null && commentBegin.line > previousPos && (commentEnd.line < nodeBegin.line || commentEnd.line == nodeBegin.line && commentEnd.column < nodeBegin.column);
return commentBegin is not null && commentEnd is not null && commentBegin.line > previousPos && (commentEnd.line < nodeBegin.line || commentEnd.line == nodeBegin.line && commentEnd.column < nodeBegin.column);
})
.Select(c => (c, CommentPosition.Leading));
}
Expand All @@ -236,7 +242,7 @@ private static bool HasNextSibling(JavaAst.Node parentNode, JavaParser.Position
.LastOrDefault(sibling =>
{
var siblingEnd = sibling.getEnd().FromOptional<JavaParser.Position>();
return siblingEnd != null && (siblingEnd.line < nodeBegin.line || siblingEnd.line == nodeBegin.line && siblingEnd.column < nodeBegin.column);
return siblingEnd is not null && (siblingEnd.line < nodeBegin.line || siblingEnd.line == nodeBegin.line && siblingEnd.column < nodeBegin.column);
});
}

Expand All @@ -252,7 +258,7 @@ public static IEnumerable<SyntaxTrivia> ConvertToComment(IEnumerable<JavaAst.Nod
var outputs = new List<string>();
foreach (var code in codes)
{
string[] input = code.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string[] input = code.ToString().Split([Environment.NewLine], StringSplitOptions.None);
outputs.AddRange(input);
}

Expand All @@ -277,14 +283,14 @@ public static IEnumerable<SyntaxTrivia> ConvertToComment(IEnumerable<JavaAst.Nod

private static IEnumerable<SyntaxTrivia> ConvertDocComment(JavaComments.Comment comment, string? post)
{
string[] input = comment.getContent().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string[] input = comment.getContent().Split([Environment.NewLine], StringSplitOptions.None);
var output = new List<string>();
var remarks = new List<string>(); // For Java tags unknown in C#
var currentOutput = output;
string? tag = null;
foreach (string inputLine in input)
{
var match = _analyzeDocString.Match(inputLine);
var match = AnalyzeDocStringRegex.Match(inputLine);
if (match.Success)
{
string paramName = match.Groups["param"].Value;
Expand All @@ -304,7 +310,7 @@ private static IEnumerable<SyntaxTrivia> ConvertDocComment(JavaComments.Comment
remarks.Add(paramName + text);
tag = "remarks";
}
else if (tag == null)
else if (tag is null)
{
tag = "summary";
OpenSection(output, tag, text);
Expand Down Expand Up @@ -358,14 +364,14 @@ private static void CloseSection(IList<string> output, string? tag)
}
else
{
output[output.Count - 1] += xmlEndTag;
output[^1] += xmlEndTag;
}
}
}

private static void TrimTrailingEmptyLines(IList<string> lines)
{
while (lines.Count > 0 && lines[lines.Count - 1].Trim() == "")
while (lines.Count > 0 && lines[^1].Trim() == "")
{
lines.RemoveAt(lines.Count - 1);
}
Expand Down Expand Up @@ -468,7 +474,7 @@ private static SyntaxNode AdjustBlockCommentIndentation(SyntaxNode node)
if (t.IsKind(SyntaxKind.MultiLineCommentTrivia))
{
int indentation = GetIndentation(leading, i) + 1; // Add one to align stars.
string[] lines = t.ToFullString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string[] lines = t.ToFullString().Split([Environment.NewLine], StringSplitOptions.None);
string indentString = new(' ', indentation);
for (int l = 1; l < lines.Length; l++)
{
Expand Down
18 changes: 5 additions & 13 deletions JavaToCSharp/ConversionContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace JavaToCSharp;

public class ConversionContext
public class ConversionContext(JavaConversionOptions options)
{
public ConversionContext(JavaConversionOptions options)
{
PendingAnonymousTypes = new Queue<ClassDeclarationSyntax>();
UsedAnonymousTypeNames = new HashSet<string>();
Options = options;
}
public Queue<ClassDeclarationSyntax> PendingAnonymousTypes { get; } = new();

public Queue<ClassDeclarationSyntax> PendingAnonymousTypes { get; }
public ISet<string> UsedAnonymousTypeNames { get; } = new HashSet<string>();

public ISet<string> UsedAnonymousTypeNames { get; }

public JavaConversionOptions Options { get; }
public JavaConversionOptions Options { get; } = options;

public string? RootTypeName { get; set; }

Expand Down
13 changes: 3 additions & 10 deletions JavaToCSharp/ConversionStateChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using System;
namespace JavaToCSharp;

namespace JavaToCSharp;

public sealed class ConversionStateChangedEventArgs : EventArgs
public sealed class ConversionStateChangedEventArgs(ConversionState newState) : EventArgs
{
public ConversionStateChangedEventArgs(ConversionState newState)
{
NewState = newState;
}

public ConversionState NewState { get; }
public ConversionState NewState { get; } = newState;
}
16 changes: 4 additions & 12 deletions JavaToCSharp/ConversionWarningEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
using System;
namespace JavaToCSharp;

namespace JavaToCSharp;

public class ConversionWarningEventArgs : EventArgs
public class ConversionWarningEventArgs(string message, int javaLineNumber) : EventArgs
{
public ConversionWarningEventArgs(string message, int javaLineNumber)
{
Message = message;
JavaLineNumber = javaLineNumber;
}

public string Message { get; }
public string Message { get; } = message;

public int JavaLineNumber { get; }
public int JavaLineNumber { get; } = javaLineNumber;
}
4 changes: 1 addition & 3 deletions JavaToCSharp/Declarations/AnnotationDeclarationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using com.github.javaparser;
using com.github.javaparser;
using com.github.javaparser.ast.body;
using com.github.javaparser.ast.type;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down
4 changes: 1 addition & 3 deletions JavaToCSharp/Declarations/BodyDeclarationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using com.github.javaparser.ast.body;
using com.github.javaparser.ast.body;
using com.github.javaparser.ast.type;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Range = com.github.javaparser.Range;
Expand Down
Loading