Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it specified anywhere that the numbers of anonymous classes start at 1?

This answer to another questions suggests, as a workaround for finding anonymous classes via reflection, to simply try all names, starting with ...$1 and counting up until no more can be found. Is this guaranteed to find all inner classes, or could there be cases where they start at 0 or some numbers are left out (for whatever reason)?

like image 889
Björn Pollex Avatar asked Feb 20 '23 06:02

Björn Pollex


2 Answers

The JLS 13.1 specifies:

The class or interface must be named by its binary name, which must meet the following constraints:

  • The binary name of a top level type (§7.6) is its canonical name (§6.7).
  • [...]
  • The binary name of an anonymous class (§15.9.5) consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits.

So in theory, it does not have to start at 1, but it has to be something like EnclosingClass$N where N is a number.

like image 131
assylias Avatar answered Feb 24 '23 10:02

assylias


According to this answer:

Note that the exact name of the files holding anonymous inner classes is not standardized and might vary. But in practice I've yet to see any other scheme than the one described here.

So I guess there is no such guarantee.

like image 40
Petar Minchev Avatar answered Feb 24 '23 11:02

Petar Minchev