0

There is this function

Transformers.aliasToBean(RoomInfoDTO.class);

Now I would to use the above function in my generic method. How do I pass the class into the method and use it ?

I tried the following but it doesn't work

passThis(PurchaseHistoryDTO.class);
....

function passThis(Class<?> passedClass){

Transformers.aliasToBean(passedClass.getClass());

}

Somehow the function takes the class as class java.lang.Class.

5
  • 1
    Don't call getClass(), just pass the Class. Commented Oct 18, 2013 at 4:14
  • Well, passedClass is instance of Class so getClass() should return class java.lang.Class. Nothing abnormal here. Why do you call getClass() on the first place? Commented Oct 18, 2013 at 4:14
  • @Pshemo the point is he didn't know that, i can assume he was caling get class because he didn't know how else to do it or he didn't know he didn't actually have to do anything. Commented Oct 18, 2013 at 4:16
  • @SeanF Yes, you are right, but I wanted to hear that from OP and make him to think about it a little and ask himself "...why do I call getClass() here? I already have Class that represents my type." Commented Oct 18, 2013 at 4:20
  • @Pshemo, I did add the getClass() because I assume it would be equivalent with .class. Anyway thank you guys for the explanation Commented Oct 18, 2013 at 5:03

3 Answers 3

5

Just pass the argument directly without calling getClass on it. passedClass is a Class object, so calling getClass on it will obviously return java.lang.Class. Just do:

function passThis(Class<?> passedClass){

     Transformers.aliasToBean(passedClass); //just pass the class, not the class's class

} 

Also, I'm assuming that "function" is shorthand/psuedocode; that won't compile in java. substitute it with a return type, probably void.

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

Comments

2

As you already passed class object PurchaseHistoryDTO.class in

Class<?> passedClass argument, 

just pass "passedClass in the method like :

Transformers.aliasToBean(passedClass);

It should work.

Comments

2

You're passing in the class, so you can operate on it directly.

void passThis(Class<?> passedClass) {

  Transformers.aliasToBean(passedClass);

}

You also don't say function in Java. You might be thinking of JavaScript.

On another note, there is basically no point to using a generic class as you have it in your example. Passing in simply (Class passedClass) would work just as well.

So the question is why you went the generics route. It could be you want something more like this:

<T> T passThis(Class<T> passedClass){

        return Transformers.aliasToBean(passedClass);

}

Just depends on what you are trying to accomplish.

Comments

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.