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)?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With