How do I find an anonymous inner class if I have only been given the name of the class Class$N
when using Eclipse, without going through the code and counting each anonymous class? Is there a 'jump to anonymous class declaration' feature where I can enter the $suffix number?
The Search->Java feature was not helpful.
I am using VisualVM to find CPU hotspots. My hotspot is in an anonymous class named SomeClass$5. VisualVM cannot find the source for my class and I (and others) cannot find how to attach the source to VisualVM, shown here and here. The launcher plugin does not resolve this.
Counting the anonymous declarations is not an option because in the long-run the risk of human error sorting through hundreds of lines can result in a lot of time wasted trying to work on the wrong anonymous class just to find out it was the wrong class.
As a workaround I'm testing the classes with 'System.out.println("this="+this.getClass().getName());'
in a method to ensure it is the right one, but there's got to be a better way.
An anonymous inner class is an inner class which is declared without any class name at all. In other words, a nameless inner class is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the same as the class name.
An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class. Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.
Anonymous classes are inner classes with no name. Since they have no name, we can't use them in order to create instances of anonymous classes. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may either extend an existing class or implement an interface.
In Eclipse IDE, you can type CTRL + SHIFT + T in Windows or *nix or Command + SHIFT + T in Mac OSX to prompt an Open Type dialog box to find details about a specified Java class.
I don't know how to do this in Eclipse, but I can offer two other ways to find the declaration.
You can call Class.forName("SomeClass$5")
, then call one of Class.getEnclosingMethod or Class.getEnclosingConstructor or Class.getEnclosingClass to find its immediately enclosing definition. This may be ambiguous if multiple anonymous classes are in the same enclosing member, but it gives you somewhere to start looking.
You can disassemble the compiled class file using the javap tool with javap -v SomeClass$5.class
. If the class file was compiled with debug information, and the anonymous class contained at least one method definition, the LineNumberTable
attributes of those method definition(s) will indicate where they were defined in the source file. The SourceFile
attribute will contain the source file name. The EnclosingMethod
attribute will name the enclosing method (if there is one), and the InnerClasses
attribute will have information about enclosing and inner classes (including unrelated classes that are merely used, so be careful when interpreting).
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