Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Mar 9, 2022
2 parents 233bc66 + 8b765c6 commit db6e072
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 42 deletions.
49 changes: 43 additions & 6 deletions JavaToCSharp/CommentsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;

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;
Expand Down Expand Up @@ -120,7 +122,6 @@ private static (SyntaxKind kind, string pre, string post) GetCommentInfo(JavaCom
}
}


return result;
}

Expand Down Expand Up @@ -171,6 +172,41 @@ private static JavaAst.Node GetPreviousSibling(JavaAst.Node parentNode, JavaPars
});
}

/// <summary>
/// Convert `JavaAst.Node` code to Comments
/// </summary>
/// <param name="codes"></param>
/// <param name="tag"></param>
/// <param name="hasBlockMark"></param>
/// <returns></returns>
public static IEnumerable<SyntaxTrivia> ConvertToComment(IEnumerable<JavaAst.Node> codes, string tag, bool hasBlockMark = true)
{
var outputs = new List<string>();
foreach (var code in codes)
{
string[] input = code.ToString().Split(new[] { "\r\n" }, StringSplitOptions.None);
outputs.AddRange(input);
}

if (outputs.Count > 0)
{
if (hasBlockMark)
{
yield return SyntaxFactory.Comment($"\r\n");
yield return SyntaxFactory.Comment($"// --------------------");
yield return SyntaxFactory.Comment($"// TODO {tag}");
}

foreach (var t in outputs)
{
yield return SyntaxFactory.Comment($"// {t}");
}

if (hasBlockMark)
yield return SyntaxFactory.Comment($"// --------------------");
}
}

private static IEnumerable<SyntaxTrivia> ConvertDocComment(JavaComments.Comment comment, string post)
{
string[] input = comment.getContent().Split(new[] { "\r\n" }, StringSplitOptions.None);
Expand Down Expand Up @@ -281,20 +317,22 @@ private static void OpenSection(ICollection<string> output, string tag, string t
}

break;

case "param": // <param name="id">label</param>
(id, label) = ParseByFirstWord(text);
output.Add($"<param name=\"{id}\">{label}");
break;

case "exception": // <exception cref="id">label</exception>
(id, label) = ParseByFirstWord(text);
output.Add($"<exception cref=\"{id}\">{label}");
break;

default:
output.Add($"<{tag}>{text}");
break;
}


static (string id, string label) ParseByFirstWord(string text)
{
string id = text.Split()[0];
Expand All @@ -316,19 +354,18 @@ public static SyntaxNode FixCommentsWhitespaces(SyntaxNode node)

return node;


static SyntaxNode InsertEmptyLineBeforeComment(SyntaxNode node)
{
/* For increased readability we change this
*
*
* DoSomething();
* // Comment
* DoSomethingElse();
*
*
* to this
*
* DoSomething();
*
*
* // Comment
* DoSomethingElse();
*/
Expand Down
109 changes: 90 additions & 19 deletions JavaToCSharp/Declarations/EnumDeclarationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,113 @@
using com.github.javaparser.ast;
using System.Collections.Generic;

using com.github.javaparser.ast;
using com.github.javaparser.ast.body;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;

namespace JavaToCSharp.Declarations
{
public class EnumDeclarationVisitor : BodyDeclarationVisitor<EnumDeclaration>
{
public override MemberDeclarationSyntax VisitForClass(ConversionContext context, ClassDeclarationSyntax classSyntax, EnumDeclaration enumDecl)
{
var name = enumDecl.getName();
return VisitEnumDeclaration(context, enumDecl);
}

public override MemberDeclarationSyntax VisitForInterface(ConversionContext context, InterfaceDeclarationSyntax interfaceSyntax, EnumDeclaration declaration)
{
return VisitForClass(context, null, declaration);
}

public static EnumDeclarationSyntax VisitEnumDeclaration(ConversionContext context, EnumDeclaration javai)
{
var name = javai.getName();
context.LastTypeName = name;

var members = enumDecl.getMembers().ToList<BodyDeclaration>();
var classSyntax = SyntaxFactory.EnumDeclaration(name);

var entries = enumDecl.getEntries().ToList<EnumConstantDeclaration>();
var typeConstants = javai.getEntries().ToList<EnumConstantDeclaration>();
if (typeConstants is { Count: > 0 })
{
var useCodeToComment = context.Options.UseUnrecognizedCodeToComment;
var membersCount = typeConstants.Count;
var enumMembers = new List<EnumMemberDeclarationSyntax>(membersCount);
var lastMembersIndex = membersCount - 1;
var showNoPortedWarning = false;
for (int i = 0; i < membersCount; i++)
{
var itemConst = typeConstants[i];
var memberDecl = SyntaxFactory.EnumMemberDeclaration(itemConst.getName())
.WithJavaComments(itemConst);

if (members is {Count: > 0})
context.Options.Warning("Members found in enum " + name + " will not be ported. Check for correctness.", enumDecl.getBegin().line);
if (useCodeToComment)
{
//java-enum `body/args` to `code Comment`
var constArgs = itemConst.getArgs();
var classBody = itemConst.getClassBody();
if (!constArgs.isEmpty() || !classBody.isEmpty())
{
var bodyCodes = CommentsHelper.ConvertToComment(new[] { itemConst }, "enum member body", false);

var enumSyntax = SyntaxFactory.EnumDeclaration(name)
.AddMembers(entries.Select(entry => SyntaxFactory.EnumMemberDeclaration(entry.getName())).ToArray());
if (memberDecl.HasLeadingTrivia)
{
var firstLeadingTrivia = memberDecl.GetLeadingTrivia().Last();
memberDecl = memberDecl.InsertTriviaAfter(firstLeadingTrivia, bodyCodes);
}
else
{
memberDecl = memberDecl.WithLeadingTrivia(bodyCodes);
}

var mods = enumDecl.getModifiers();
showNoPortedWarning = true;
}

//java-enum `method-body` to `code Comment`
if (i == lastMembersIndex)
memberDecl = MembersToCommentTrivia(memberDecl, ref showNoPortedWarning);
}

enumMembers.Add(memberDecl);
}

if (showNoPortedWarning)
context.Options.Warning($"Members found in enum {name} will not be ported. Check for correctness.", javai.getBegin().line);

classSyntax = classSyntax.AddMembers(enumMembers.ToArray());
}

var mods = javai.getModifiers();
if (mods.HasFlag(Modifier.PRIVATE))
enumSyntax = enumSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
if (mods.HasFlag(Modifier.PROTECTED))
enumSyntax = enumSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword));
if (mods.HasFlag(Modifier.PUBLIC))
enumSyntax = enumSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
classSyntax = classSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

return enumSyntax;
}
return classSyntax.WithJavaComments(javai);

public override MemberDeclarationSyntax VisitForInterface(ConversionContext context, InterfaceDeclarationSyntax interfaceSyntax, EnumDeclaration declaration)
{
return VisitForClass(context, null, declaration);
EnumMemberDeclarationSyntax MembersToCommentTrivia(EnumMemberDeclarationSyntax lastMemberDecl, ref bool showNoPortedWarning)
{
var members = javai.getMembers().ToList<BodyDeclaration>();
if (members is { Count: > 0 })
{
var todoCodes = CommentsHelper.ConvertToComment(members, "enum body members");
if (todoCodes != null)
{
var lastMemberTrailingTrivia = lastMemberDecl.GetTrailingTrivia();
if (lastMemberTrailingTrivia.Count > 0)
lastMemberDecl = lastMemberDecl.InsertTriviaAfter(lastMemberTrailingTrivia.Last(), todoCodes);
else
lastMemberDecl = lastMemberDecl.WithTrailingTrivia(todoCodes);

showNoPortedWarning = true;
}
}

return lastMemberDecl;
}
}
}
}
}
8 changes: 7 additions & 1 deletion JavaToCSharp/JavaConversionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public JavaConversionOptions()
{
IncludeNamespace = true;
IncludeUsings = true;
UseUnrecognizedCodeToComment = true;
}

public event EventHandler<ConversionWarningEventArgs> WarningEncountered;
Expand All @@ -32,6 +33,11 @@ public JavaConversionOptions()

public bool UseDebugAssertForAsserts { get; set; }

/// <summary>
/// Unrecognized code is translated into comments
/// </summary>
public bool UseUnrecognizedCodeToComment { get; set; }

public ConversionState ConversionState { get; set; }

public JavaConversionOptions AddPackageReplacement(string pattern, string replacement, RegexOptions options = RegexOptions.None)
Expand Down Expand Up @@ -64,4 +70,4 @@ internal void ConversionStateChanged(ConversionState newState)
StateChanged?.Invoke(this, new ConversionStateChangedEventArgs(newState));
}
}
}
}
2 changes: 1 addition & 1 deletion JavaToCSharp/JavaToCSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.0.2</Version>
<Version>2.1.0</Version>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down
9 changes: 9 additions & 0 deletions JavaToCSharp/JavaToCSharpConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public static string ConvertText(string javaText, JavaConversionOptions options
rootMembers.Add(classSyntax);
}
}
else if (type is EnumDeclaration enumType)
{
var classSyntax = EnumDeclarationVisitor.VisitEnumDeclaration(context, enumType);

if (namespaceSyntax != null)
namespaceSyntax = namespaceSyntax.AddMembers(classSyntax);
else
rootMembers.Add(classSyntax);
}
}

if (namespaceSyntax != null)
Expand Down
5 changes: 4 additions & 1 deletion JavaToCSharpCli/JavaToCSharpCli.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.0.2</Version>
<Version>2.1.0</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JavaToCSharp\JavaToCSharp.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit db6e072

Please sign in to comment.