Domanda

We have an annotation @Accepts:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Accepts {
  Class[] value();
}

It takes a list of Classes. These are later used to validate in a DSL that a field was passed an instance of the classes listed as acceptable.

Some examples of this annotation in use:

public enum PropertyName {


  @Accepts({Integer.class})
  xCoordinate,


  @Accepts({Integer.class})
  yCoordinate,

  @Accepts({Boolean.class})
  showPermission

  @Accepts({String.class, FieldScript.class, List.class})
  onClick

  /* And So On*/
}

I am adding a new item to this enum called 'value' and it can accept a String or a PropertyResolver. PropertyResolver is an interface defined as below:

public interface PropertyResolver<T> {
    public T getValue(TagContext tagContext);
}

I don't know how to do a .class on PropertyResolver to pass on to @Accepts. Is it possible to do so?

Thanks.

È stato utile?

Soluzione

You will have to do PropertyResolver.class. There will only one Class instance that represents the the class (raw-version).

No such things as PropertyResolver<T>.class or PropertyResolver<Integer>.class exist.

Always, keep in mind that in Java, generics is compile time only feature.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top