Anonymous class reference?

I am developing a plugin for an RCP application. Within plugin.xml I need to register specific classes at a given extension point. One of these classes is an anonymous (?) Class defined as follows:

 package de.me.mypackage; import org.something.AnotherClass; public class ClassOne { ... public static AnotherClass<ClassOne> getThat() { return new AnotherClass<ClassOne>() { ... }; } 

}

Is there a way to reference AnotherClass<ClassOne> in the plugin.xml file?

I already tried something like de.me.mypackage.ClassOne$AnotherClass , but this does not work. Do I have to declare this class in my own file in order to be able to reference it?

+5
source share
3 answers

As far as I know, it will have a numerical index:

 class Bla { public static void main(String[] args) { (new Runnable() { public void run() { System.out.println(getClass().getName()); // prints Bla$1 } }).run(); } } 

After compilation, you get:

 $ ls *.class Bla$1.class Bla.class 

However, you cannot rely on numbering if you change the source file.

Can you define an inner class static , for example:

 public class ClassOne { public static class MyClass extends AnotherClass<ClassOne> { public MyClass(/* arguments you would pass in getThat()? */) { ... } ... } public static AnotherClass<ClassOne> getThat() { return new MyClass(...); } } 
+2
source

I need to say the obvious here - you have to make it a named class if you want to reference it. Regardless of whether you have access to it, otherwise it is a technical curiosity (that I do not know the answer), and not what you really should do in the workplace.

+1
source

The dollar sign comes into play only in the binary class name; in the Java source, just use de.me.mypackage.ClassOne.AnotherClass.class .

0
source

Source: https://habr.com/ru/post/1239434/


All Articles