I am trying to inherit following parameter in a java interface (an example from Spring Data JPA, but the question is generally about parameters to annotations):
public interface ItemRepository<T extends Item> extends BaseRepository<T> {
public final String type = null;
@Query("select distinct i from " + type + " i " +
"join i.tags t " +
"join fetch i.locale where t = ?1")
public List<T> findByTag(Tag t);
}
so that in inherited interfaces I could have just:
public interface EventRepository extends ItemRepository<Event> {
public final static String type = "Event";
}
But unfortunately the string value of variable “type” is associated to the variable too late, so when the annotation is created, the value is still null. Can I force the compiler to associate the variable from the child interface?
Thanks
In short, no. You can’t force the compiler to ‘associate the variable from the child interface’.
The Java compiler needs to be able to determine all annotation values at compile time. It needs to write out into the class file for
ItemRepositorya constant value to put in the@Queryannotation. What constant value would you imagine this would be for your code?As it happens, the compiler can determine from your code a value for this annotation at compile time. However, it’s not exactly the value you want. (I am assuming that you’ve accidentally omitted the
staticmodifier from the fieldtypeinItemRepository– I believe your code wouldn’t compile otherwise. It also wouldn’t compile if you replaced yourtypefield with agetType()method.)Your code looks like it can determine at compile time the annotation values for the subinterfaces. The problem is that it’s not possible to determine at compile time the annotation value for the superinterface
ItemRepository.