1

How can i create an instance of the given generic type class with a subtype Class<Foo<Bar>> type = ??

Its easy for simple generic such as: Class<Foo> type = Foo.class; but how to do this here?

final Class<Foo<Bar>> type = null; // ??? 

We cannot do something like Foo<Bar>.class which is possible in c# as far as i know.

I want to use this type in a generic function which works for all simple generics but not if nested.

Foo<Bar> fooBar = getRecord(type, 1); static T get(Class<T> type, int id) { // load from database return session.get(type, id); } 

Complex example

The production example is more complex having a DAO class with many methods that rely on the generic type.

interface Entity {} class Foo<T extends Entity> extends Entity {} class Bar extends Entity {} class EntityDAO<T extends Entity> { Class<T> type; Session session; public EntityDAO(Class<T> type) { this.type = type; } public T get(int id) { return session.get(type, id); } } public class TestSession { public <T> T get(final Class<T> type, final int id) { Objects.requireNonNull(type); return null; } 

}

void static main(String[] args) { final Class<Foo<Bar>> type = null; // how to set type here? EntityDAO<Foo<Bar>> fooBarDAO = new EntityDAO<>(type); Foo<Bar> entity = fooBarDAO.get(1); } 

Currently i am just using the main type Foo.class loosing the Bar.class information for the DAO.

5
  • 1
    You'll need either a TypeToken (which can store the generic type info for you, a la TypeToken<Foo<Bar>>), or you'll need to send the generic paremeter as well e.g. getRecord(Foo.class, Bar.class, 1);. This is a limitation of java's generics. If you can provide a more concrete example, there may be alternative solutions. Commented Jul 7, 2022 at 16:11
  • 1
    If you have an instance of this type you could cast it to the specific class: (Class<Foo<Bar>>) foo.getClass();. But note that depending on how you want to implement this, it might not be any use in your case. Commented Jul 7, 2022 at 16:24
  • @Rogue the TypeToken is a nice example, which will need some factoring but will consider this. I edited the question with a second more complex example. Commented Jul 7, 2022 at 16:35
  • @Rogue I enhanced my example and using the TypeToken does not work cause in my EntityDAO i still pass the type as an argument to the constructor so the token just delegates the problem. The moment i need the class as a "variable" i am stuck. Commented Jul 8, 2022 at 15:47
  • 1
    @Japhei your first comment was correct answer and it works so you can add it as an answer. At runtime the <Bar> information is lost but it does not matter cause i only need the main Foo entity to access database entity table. Just for cleaner code i need Foo<Bar>. A alternate solution is to add constructor public <V extends T> EntityDAO(Class<V> type) and do casting inside and just pass Foo.class. I removed my comments. Commented Jul 9, 2022 at 17:55

2 Answers 2

1

With any workaround using Class<Foo<Bar>> type; as a variable the <Bar> is lost at runtime but still we can achieve the EntityDAO class to be as generic to support Foo<Bar> for easier code allow type to extend T inside the constructor / method and cast like:

@SuppressWarnings("unchecked") public <V extends T> TestDAO(final Class<V> type) { this.type = (Class<T>) type; } 

Then use it:

void static main(String[] args) { // <Bar> information lost at runtime but not important EntityDAO<Foo<Bar>> fooBarDAO = new EntityDAO<>(Foo.class); // dao perfectly generic for Foo<Bar> Foo<Bar> entity = fooBarDAO.get(1); } 

This way we can use it without a dummy instance as @Japhei stated in his comment (which works with the original code) but not always a dummy instance is easy to create or available.

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

Comments

0

If you have an instance of this type you could cast it to the specific class:

Foo<Bar> foo = new Foo<>(); type = (Class<Foo<Bar>>) foo.getClass(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.