Skip to content
Open
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
45 changes: 45 additions & 0 deletions JavaToCSharp.Tests/ConvertNestedTypeInheritanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace JavaToCSharp.Tests;

public class ConvertNestedTypeInheritanceTests
{
[Fact]
public void Extending_Nested_Type_Keeps_Declaring_Type_Qualifier()
{
const string javaCode = """
package com.example;
public class GeneratorFactory {
public abstract static class AbstractXmlFeatureGeneratorFactory {
}
public interface XmlFeatureGeneratorFactory {
}
public class CachedFeatureGeneratorFactory
extends GeneratorFactory.AbstractXmlFeatureGeneratorFactory
implements GeneratorFactory.XmlFeatureGeneratorFactory {
}
}
""";

var parsed = Convert(javaCode);

Assert.Contains(
"public class CachedFeatureGeneratorFactory : GeneratorFactory.AbstractXmlFeatureGeneratorFactory, GeneratorFactory.XmlFeatureGeneratorFactory",
parsed);
}

[Fact]
public void Extending_Simple_Type_Is_Unaffected()
{
const string javaCode = """
package com.example;
public class Square extends Shape {
}
""";

var parsed = Convert(javaCode);

Assert.Contains("public class Square : Shape", parsed);
}

private static string Convert(string javaCode)
=> JavaToCSharpConverter.ConvertText(javaCode, new JavaConversionOptions { IncludeComments = false }) ?? "";
}
9 changes: 9 additions & 0 deletions JavaToCSharp/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ public static TypeSyntax GetSyntaxFromType(ClassOrInterfaceType type)
typeSyntax = SyntaxFactory.ParseTypeName(typeName);
}

// Nested types (e.g. `Outer.Inner`) carry their declaring type as a scope, which
// getNameAsString() above does not include. Prepend it so the declaring type isn't lost.
if (type.getScope().FromOptional<ClassOrInterfaceType>() is { } scope
&& typeSyntax is SimpleNameSyntax simpleName
&& GetSyntaxFromType(scope) is NameSyntax scopeName)
{
return SyntaxFactory.QualifiedName(scopeName, simpleName);
}

return typeSyntax;
}

Expand Down