1

I've a method in which I'm passing a generic argument. To execute one required method I need the class instance of the same. How can I get it. There is a workaround which I'm using but I want to know how can we get the Class instance.

The snippet I'm using is:

public <T> T readModelWithId(Class<T> c, Serializable pk) {
    // Some code here.
    T t = (T)session.get(c, pk);
    // Some code here.
    return t;
}

The problem is that I don't want to pass the Class instance in this method as it is same as the return type T which is of generic type.

4 Answers 4

3

There's no way you can get to something like T.class within that method, so you must pass the class argument since you need it to get your model object from the session.

Moreover, this adds a bit of extra "type-safety". You will force the caller to specify the return type as an argument, so the returned type will not just be "anything a caller may expect" (although I agree this makes it redundant).

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

3 Comments

hi @Costi, I agree with your statement, I wanted to remove this extra redundant thing only.. iff there is any way.. :)
There's no way, unfortunately.
Nope. No way. It's not redundant at runtime.
2

You can't do it because Java implements Generics using "type erasure".

http://docs.oracle.com/javase/tutorial/java/generics/erasure.html

Looks like you are doing a generic DAO. I've done something similar here:

http://www.matthews-grout.co.uk/2012/01/my-generic-hibernate-dao.html

6 Comments

i saw your implementation but i see that while instantiating the HibernateDaoImpl class you're passing the Class<T> instance. Which in turn is the same thing.. But I like the way you managed the implementation.. :)
up-boats for you sir, for your kind words.
@DanMatthews-Grout and pratikabu, in addition to having a generic DAO, you may find it useful to use generics in the data model for the ID as well. This is outlined in this article I wrote not so long ago: herman-b.hubpages.com/hub/Java-data-model-with-generic-IDs Feedback about that is always welcome! Thanks
My example does use a Generic ID - exetending Serializable. Nice example though.
actually, Dan's example does not use an ID class, it uses an ID type parameter for the model superclass, so that e.g. some model classes can use a String and others can use a Long. My approach uses an Id class which is parameterized with the type of model object it belongs to (and always uses String underneath, since you don't need to perform calculation with Ids) so that they are type-safe and code completion is improved.
|
0

You cannot do anything with generics that you could not do with non-generics with casts. Consider the non-generic version of your code:

public Object readModelWithId(Class c, Serializable pk) {
    // Some code here.
    Object t = session.get(c, pk);
    // Some code here.
    return t;
}

Now ask the same question, can you get rid of the class instance argument c?

1 Comment

but in this case unnecessary casting will be involved wherever this method is called. and yes we've to pass the class instance here.. got your point..
-1

Generics are more for compile time type safety - you need class instance so compiler does not complain about missing cast. Alternatively you can ditch generics altogether and cast result

2 Comments

agree but I've to use generic as it is saving me lots of unnecessary same methods with different return types..
"you need class instance so compiler does not complain about missing cast" What? No. You need the class instance to pass to session.get(). You don't need any class instance to cast.

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.