Annotation class with inner class

I create my enumerations through reflection , for this I add to each enumeration an inner class that implements an abstract factory . Now I want to access this inner class to call the method:

@Factory(FooFactory.class)
public enum Foo {

     FOO, BAR;

     public class FooFactory implements AbstractFactory<Foo> {

          public Foo create(String value) {
               return valueOf(value.toUpperCase());
          }
     }
}

Definition @Factory:

@Retention(RetentionPolicy.RUNTIME)
public @interface Factory {

        Class<?> value();
}

In doing so, however, I get the following error :

Class cannot be resolved for type FooFactory.java

When I try @Factory(Foo$FooFactory.class), I get an error :

Nested Foo $ FooFactory cannot be assigned using its binary name.

So is it possible to refer to a nested class at all?

+5
source share
3

...

@Factory(Foo.FooFactory.class)

.

+7

, .

, :

public static class FooFactory implements AbstractFactory<Foo> {

      public static Foo create(String value) {
           return valueOf(value.toUpperCase());
      }
 }

: Foo.valueOf(value) . ( ).

Factory.java

import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
public @interface Factory {
        Class<?> value();
}

FooEnum.java

@Factory(FooEnum.FooFactory.class)
public enum FooEnum {
    FOO, BAR;
    public static class FooFactory  {

          public static FooEnum create(String value) {
               return valueOf(value.toUpperCase());
          }
     }
}

FooEnumMain.java

public class FooEnumMain {
    public static void main(String[] args) {
        FooEnum f = FooEnum.FooFactory.create("foo");
        System.out.println(f);
    }
}
+4

At the time of annotation submission, FooFactory is undefined, so you must specify the full path:

@Factory(Foo.FooFactory.class)
+3
source

All Articles