5

I am developing a plugin for an RCP application. Within the plugin.xml, I need to register certain classes at a given extension point. One of these classes is an anonymous (?) class defined like this:

package de.me.mypackage;

import org.something.AnotherClass;

public class ClassOne {
  ...
  public static AnotherClass<ClassOne> getThat() {
    return new AnotherClass<ClassOne>() {
      ...
    };
  }

}

Is there any way to reference AnotherClass<ClassOne> within the plugin.xml?

I already tried something like de.me.mypackage.ClassOne$AnotherClass but that does not work. Do I have to declare that class within its own file to be able to reference it?

Vlad
  • 18,195
  • 4
  • 41
  • 71
Antje Janosch
  • 1,154
  • 5
  • 19
  • 37

3 Answers3

2

As far as I know, it would have a numeric index:

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

After compiling, you get:

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

That said, you can't rely on the numbering in case the source file is modified.

Can you instead define a static inner class, like:

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(...);
  }
}
Vlad
  • 18,195
  • 4
  • 41
  • 71
  • Thanks for all suggestions! @Vlad: Creating a static inner class seems to be the best solution for my case and it works. I could then reference to that class by 'de.me.mypackage.ClassOne$MyClass'. – Antje Janosch Dec 30 '15 at 09:05
1

I need to say the obvious here - you should make it a named class if you want to refer to it. Whether you can access it otherwise is a technical curiosity (that I don't happen to know the answer to), not something you should actually do in production.

djechlin
  • 59,258
  • 35
  • 162
  • 290
0

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

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152