Skip to content

Commit

Permalink
https://github.com/manifold-systems/manifold/issues/509
Browse files Browse the repository at this point in the history
- simplify naming scheme from <parent>_Xxx to Xxx_N where N reflects depth
  • Loading branch information
rsmckinney committed Mar 1, 2024
1 parent d969211 commit 1f99cad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public void testNonIdentifierStart()

public void testDuplicateNestedType()
{
DuplicateNestedType.nested.inBetween.inBetween_nested nn = DuplicateNestedType.nested.inBetween.inBetween_nested.builder().build();
DuplicateNestedType.nested.inBetween.nested_2 nn = DuplicateNestedType.nested.inBetween.nested_2.builder().build();
nn.setName( "hi" );
assertEquals( "hi", nn.getName() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static class State

private State( String name, JsonSchemaType parent, IFile file )
{
_name = avoidDuplicateNestedClassName( parent, parent, name );
_name = avoidDuplicateNestedClassName( parent, name );
_parent = parent;
_file = file;

Expand All @@ -97,19 +97,48 @@ private State( String name, JsonSchemaType parent, IFile file )
}

// Java does not permit an inner class to have the same name as any class in its direct ancestry
private String avoidDuplicateNestedClassName( JsonSchemaType ancestor, JsonSchemaType parentOfName, String name )
private String avoidDuplicateNestedClassName( JsonSchemaType ancestor, String name )
{
if( ancestor == null || parentOfName == null || name == null || "definitions".equalsIgnoreCase( name ) )
return avoidDuplicateNestedClassName( ancestor, name, 0 );
}
private String avoidDuplicateNestedClassName( JsonSchemaType ancestor, String name, int dupSuffix )
{
if( ancestor == null || name == null || "definitions".equalsIgnoreCase( name ) )
{
if( dupSuffix > 0 )
{
// make a different name by adding a suffix indicating the rank of duplicate names descending from the
// originating ancestor, also avoids conflicts with names above the ancestor that may match this pattern
name = name + '_' + dupSuffix;
}
return name;
}
String ancestorName = ancestor.getName();
if( ancestorName != null && ancestorName.equals( name ) )
if( ancestorName != null )
{
// make a different name by qualifying the duplicate nested name with the parent name
return parentOfName.getName() + "_" + name;
dupSuffix = nestedNameConflictCount( ancestorName, name, dupSuffix );
if( ancestorName.equals( name ) )
{
dupSuffix = 2;
}
}
return avoidDuplicateNestedClassName( ancestor.getParent(), name, dupSuffix );
}

private int nestedNameConflictCount( String ancestorName, String name, int dupSuffix )
{
if( ancestorName.startsWith( name + '_' ) )
{
try
{
int count = Integer.parseInt( ancestorName.substring( name.length() + 1 ) );
return Math.max( count, dupSuffix );
}
catch( Exception ignore )
{
}
}
return avoidDuplicateNestedClassName( ancestor.getParent(), parentOfName, name );
return dupSuffix;
}
}

Expand Down

0 comments on commit 1f99cad

Please sign in to comment.