Skip to content

Treat leading and trailing $ in a class name as part of a top-level class name in ClassPath - #8740

Closed
LubabaKhalid wants to merge 1 commit into
google:masterfrom
LubabaKhalid:classpath-dollar-top-level
Closed

LubabaKhalid wants to merge 1 commit into
google:masterfrom
LubabaKhalid:classpath-dollar-top-level

Conversation

@LubabaKhalid

Copy link
Copy Markdown
Contributor

ClassInfo.isTopLevel() (and so ClassPath.getTopLevelClasses()) assumed that any $ in a class name marks a nested class, so generated top-level classes like $AutoValue_Foo were silently dropped. getSimpleName() had the same problem, returning AutoValue_Foo.

This ignores $ characters at the start and end of the part of the name after the package when looking for the nested-class separator, as suggested in the issue discussion. It's still a heuristic, but it handles the common case of generated class names.

Added tests for isTopLevel() and getSimpleName().

Fixes #3349

… class name in `ClassPath`.

`ClassInfo.isTopLevel()` (and so `ClassPath.getTopLevelClasses()`) assumed that any `$` in a
class name marks a nested class, so generated top-level classes like `$AutoValue_Foo` were
silently dropped. `getSimpleName()` had the same problem, returning `AutoValue_Foo`.

Ignore `$` characters at the start and end of the part of the name after the package when
looking for the nested-class separator, as suggested in the issue discussion. This is still a
heuristic, but it handles the common case of generated class names.
@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@LubabaKhalid

Copy link
Copy Markdown
Contributor Author

@eamonnmcmanus @cpovirk could you take a look when you have a chance? This implements the leading/trailing-$ heuristic suggested in the issue discussion.

copybara-service Bot pushed a commit that referenced this pull request Oct 21, 2026
…of a top-level class name.

`ClassInfo.isTopLevel()` (and so `ClassPath.getTopLevelClasses()`) assumed that any `$` in a class name marks a nested class, so generated top-level classes like `$AutoValue_Foo` were silently dropped. `getSimpleName()` had the same problem, returning `AutoValue_Foo`.

_Consecutive_ `$` characters generally appear in runtime-generated classes, in which case they are outside the domain of `ClassPath`. However, they can appear at compile time, too, such as in desugared Android code—though, in the specific example of Android, `ClassPath` may well not work anyway. In any case, such classes don't represent true nested classes.

This new logic is all still based on heuristics, but it handles the common cases of generated class names.

Fixes #8740
Fixes #3349 well enough in practice.

RELNOTES=n/a
PiperOrigin-RevId: 999520161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ClassPath.getTopLevelClasses() does not return a top-level class with $ character

1 participant