4

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)?

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 1
    According to http://stackoverflow.com/questions/1075207/what-are-the-1-in-class-file, "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." – Petar Minchev Sep 04 '12 at 16:16
  • 1
    I guess this is an answer - you should post it as such, so that I can accept. – Björn Pollex Sep 04 '12 at 16:18
  • 1
    As far as I know there's no such guarantee. However in theory the information can be extracted from the `InnerClasses` attribute of the parent class file. Whether this works in practice, I don't know. – biziclop Sep 04 '12 at 16:18
  • 1
    I tried this approach ([Apache BCEL](http://commons.apache.org/bcel/) makes this easy enough), but I wanted to see if there is a way that works directly with just class-loaders. – Björn Pollex Sep 04 '12 at 16:19
  • 1
    So it works, that's good to know. :) I agree with the first comment, in practice I haven't seen any implementation that deviated from the `$1...$n` schema. – biziclop Sep 04 '12 at 16:21
  • here what I found little bit old :o) http://jcp.org/aboutJava/communityprocess/maintenance/JLS/innerclasses.pdf – HRgiger Sep 04 '12 at 16:24

3 Answers3

6

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 2
    Note that this also means that scanning through `$1...$n` will only return the directly enclosed inner classes. Which is technically correct but in practice people would expect the inner classes of inner classes to be included as well, which requires several nested rounds of scanning. – biziclop Sep 04 '12 at 16:27
2

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.

Community
  • 1
  • 1
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
0

In this link to a java.sun.com tutorial, it explains anonymous classes from page 243 with example outputs which follow the naming convention described.e.g. *$1.class, *$2.class etc. On the top of page 246 it says:

The names of the anonymous classes at runtime are also shown in the program output. They are also the names used to designate their respective class files. Anonymous classes are not so anonymous after all.

Considering that this tutorial is on the Oracle website, It's almost certain that this is a convention that will not vary. Hope this helps :)

Chris B
  • 925
  • 4
  • 14