简体   繁体   English

如何在Eclipse中找到一个匿名内部类,只给出它的合成名称(Class $ N)?

[英]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.getEnclosingMethodClass.getEnclosingConstructorClass.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.

相关问题 给定类成员名称或对象,如何在Java中找到其声明/封闭类对象? - Given a class member name or object, how can I find its declaring/enclosing class object in java? 如何使用内联匿名 class 声明避免合成访问编译器警告,这是什么意思? - How can I avoid synthetic access compiler warning with inline-anonymous class declaration and what does it mean? 如何将非最终变量传递给匿名内部 class? - How can I pass a non final variable to an anonymous inner class? 如何在Java中将变量传递给匿名内部类? - How can I pass a variable to an anonymous inner class in Java? 如何在NotSerializableException中标识匿名内部类 - How can I identify an anonymous inner class in a NotSerializableException 如何确定一个类名所指示的内部匿名类? - how to figure out which inner anonymous class indicated by a class name? 仅给出.class文件的完整路径,如何加载其Class对象? - Given only the full path to a .class file, how can I load its Class object? 如果匿名内部类有两个外部类,如何获取外部类的引用? - How can I get the outer class's reference if the anonymous Inner Class has two outer class? 我可以在其方法内将对象(指匿名内部类)设置为“ null”吗? - Can I set object (referring to anonymous inner class) to “null” inside its method? 构建一个匿名内部类并调用其方法 - Build an anonymous inner class and call its methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM