Get .class attribute for general class

I am trying to extend the following class using a constructor (from the Ektorp library):

public class CouchDbRepositorySupport<T extends CouchDbDocument> implements GenericRepository<T> { ... protected CouchDbRepositorySupport(Class<T> type, CouchDbConnector db) { ... } 

Here is my implementation:

 public class OrderRepository extends CouchDbRepositorySupport<Order<MenuItem>> { public OrderRepository(CouchDbConnector db) { super(Order<MenuItem>.class, db); 

The problem with the Order<MenuItem>.class . The Java compiler tells me:

  Syntax error on token ">", void expected after this 

I tried with (Order<MenuItem>).class Order.class , Order.class and new Order<MenuItem>().getClass() without any luck.

What can I do to get the .class attribute for a generic class?

+6
source share
2 answers

If you change your type to publish an internal type, you can get it like this:

 public class CouchDbRepositorySupport<C, T extends CouchDbRepositorySupport<C>> implements GenericRepository<T> { ... protected CouchDbRepositorySupport(Class<C> type, CouchDbConnector db) { ... } public class OrderRepository extends CouchDbRepositorySupport<MenuItem, Order<MenuItem>> { public OrderRepository(CouchDbConnector db) { super(MenuItem.class, db); 

You have several options for how you declare a parent class; this is just one example.

Disclaimer: I did it manually without an IDE, so there might be some minor syntax issues with it, but the concept should work.

+1
source

The correct syntax is:

 Order.class 

In Java, Order<MenuItem>.class and Order.class are the same class at runtime; general type information (parameter of type <MenuItem> ) is lost at runtime due to erasure - a serious limitation of a Java-type system.

+2
source

Source: https://habr.com/ru/post/915847/


All Articles