Skip to content

generator should warn about type names matching enclosing package names #982

Closed
@jonpryor

Description

@jonpryor

Background

Context: Eric Lippert's Fabulous Adventures in Coding, Do not name a class the same as its namespace:

Context: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces

x DO NOT use the same name for a namespace and a type in that namespace.

In C#, it's "bad" to have a type which has the same name as its parent namespace. This can result in unintentional ambiguity when attempting to use a type, and headscratching error messages:

type name Foo does not exist in Foo.Foo

This is easy to avoid in "normal" hand-written C# code, but this pattern is extremely common in Android bindings because we PascalCase the original Java package names.

Thus, consider com.google.zxing.aztec.decoder.Decoder: our default naming algorithm would produce Com.Google.Zxing.Aztec.Decoder.Decoder, which triggers our "DO NOT use…" scenario of having a type with the same name as its enclosing namespace.

We don't need to stretch to external bindings for this; core Android hits this as well:

The typical fix is to "pluralize" the containing namespace, e.g. turning android.gesture into Android.Gestures.

Currently, it's a manual effort to "know about" and look for this problem when adding or updating bindings, and we don't always remember to do this check.

Proposal

Update generator to check the enclosing package/namespace name and see if it matches the type name, in a case-insensitive manner. If we find a type within a matching namespace, we should emit a warning.

The warning should take post-managedName values into consideration, so that the warning can be reasonably fixed.

The warning message text should describe how to fix the warning.

The warning should use a "unique" number so that the warning can be disabled, in case the warning cannot be fixed.

Example:

warning BG1234: Type `Example.Example` has a type name which matches the enclosing namespace name.
warning BG1234: Please consider updating Metadata.xml to contain:
warning BG1234:   <ns-replace source="example" replacement="Examples" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    generatorIssues binding a Java library (generator, class-parse, etc.)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions