Does it indicate somewhere that the number of anonymous classes starts at 1?

This answer to other questions offers, as a workaround, to search for anonymous classes through reflection, just try all the names starting with ...$1 and count until no more can be found. Is it guaranteed to find all inner classes, or can there be cases when they start with 0 or some numbers are not taken into account (for some reason)?

+4
source share
3 answers

JLS 13.1 indicates:

A class or interface must be named with its binary name, which must satisfy the following restrictions:

  • 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 including type, followed by $, followed by a non-empty sequence of digits.

So, theoretically, it should not start with 1, but it should be something like EnclosingClass$N , where N is a number.

+6
source

According to this answer :

Please note that the exact name of files containing anonymous inner classes is not standardized and may change. But in practice, I have not yet seen any other circuit than described here.

So, I think there is no such guarantee.

+2
source

In this link to the java.sun.com tutorial, he explains the anonymous classes from page 243 with examples that follow the described naming convention. For example * $ 1.class, * $ 2.class, etc. The top of page 246 says:

The names of anonymous classes at runtime are also displayed in the output of the program. They are also names that are used to refer to their respective class files. Anonymous classes are not so anonymous.

Given that this tutorial is on the Oracle website, he is pretty sure that this is an agreement that will not change. Hope this helps :)

0
source

Source: https://habr.com/ru/post/1432321/


All Articles