15

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.

Community
  • 1
  • 1
user515655
  • 989
  • 2
  • 10
  • 24
  • Great question. I know two ways: Counting (which is error prone) and giving the class a name (i.e. turning an anonymous class into an inner class) which kind of ruins the benefits of the anonymous class. Plus when I think about Java 8 ... there should be a better way. – Aaron Digulla Apr 27 '15 at 09:06
  • 4
    Two more ways to find the declaration: `Class.forName("SomeClass$5").getEnclosingClass/Method/Constructor()` (but may be ambiguous if there are many anonymous classes in the same scope). `javap -v SomeClass$5.class` and check the LineNumberTable attributes of the methods (but may not be present if the .class files were compiled without debug information or obfuscated). (These don't help getting Eclipse or VisualVM to find your code automatically, and so don't answer the question as posed.) – Jeffrey Bosboom Apr 28 '15 at 04:05
  • 1
    Just info about YourKit Java Profiler: "...Take note of the extremely useful ability to locate the code of anonymous classes and their methods, which is a very difficult thing to do manually." [link]{https://www.yourkit.com/docs/80/help/eclipse.jsp} – Anton Danilov May 06 '15 at 15:40
  • @JeffreyBosboom your idea is great, why not make an answer out of it? – Bastian Voigt Jul 08 '15 at 17:50
  • @BastianVoigt I felt it didn't answer the question (about using Eclipse), but they are useful workarounds. I'll write an answer. – Jeffrey Bosboom Jul 08 '15 at 20:42

1 Answers1

4

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

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92