1

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@JakubJirutka Wait, really? When are generics not erased?
Hmmm, found something that says that dynamic types are erased, but static types are not. Is that what you were referring to?
@JakubJirutka Ah, that's interesting. Thanks for the info! Learned something new today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.