-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
265 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.