[英]How can I find an anonymous inner class in Eclipse given only its synthetic name (Class$N)?
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? 如果在使用Eclipse时只给出
Class$N
的名称,而不通过代码并计算每个匿名类,我如何找到一个匿名内部类? Is there a 'jump to anonymous class declaration' feature where I can enter the $suffix number? 是否有“跳转到匿名类声明”功能,我可以在其中输入$后缀号码?
The Search->Java feature was not helpful. Search-> Java功能没有帮助。
I am using VisualVM to find CPU hotspots. 我正在使用VisualVM来查找CPU热点。 My hotspot is in an anonymous class named SomeClass$5.
我的热点是一个名为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 .
VisualVM无法找到我的类的源代码,而我(以及其他人)无法找到如何将源附加到VisualVM,如此处和此处所示。 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());'
作为一种解决方法,我使用
'System.out.println("this="+this.getClass().getName());'
测试类'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. 在一种确保它是正确的方法,但必须有一个更好的方法。
I don't know how to do this in Eclipse, but I can offer two other ways to find the declaration. 我不知道如何在Eclipse中执行此操作,但我可以提供另外两种方法来查找声明。
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. 您可以调用
Class.forName("SomeClass$5")
,然后调用Class.getEnclosingMethod或Class.getEnclosingConstructor或Class.getEnclosingClass中的一个来查找其立即封闭的定义。 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
. 您可以使用带有
javap -v SomeClass$5.class
的javap工具来反汇编编译的类文件。 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. 如果使用调试信息编译类文件,并且匿名类包含至少一个方法定义,那么这些方法定义的
LineNumberTable
属性将指示它们在源文件中的定义位置。 The SourceFile
attribute will contain the source file name. SourceFile
属性将包含源文件名。 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). EnclosingMethod
属性将命名封闭方法(如果有),并且InnerClasses
属性将包含有关封闭和内部类的信息(包括仅使用的无关类,因此在解释时要小心)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.