This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes resolver wrapper for primitive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "resolver wrapper for primitive" Watch "resolver wrapper for primitive" New topic

resolver wrapper for primitive

Arto Pastinen
Ranch Hand

Joined: Dec 13, 2002
Posts: 79

Is there method in standard api to get correct wrapper class for primitive class?

Something like:
Class wrapperClass = getWrapperClass(int.class); // java.lang.Integer
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I don't think so, no; but you could write your own such method easily enough, yes? Just load a HashMap with the right answers (like theMap.put(int.class, Integer.class) and then just do theMap.get(int.class) to find the right answer.

[Jess in Action][AskingGoodQuestions]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
What would you need this for?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Arto Pastinen
Ranch Hand

Joined: Dec 13, 2002
Posts: 79

Yes i wrote method to resolve correct wrapper.

I am making new web MVC architecture, where model object's functionality is defined by java 1.5's metadata functionality, and i used cglib to do some modification for these classes at runtime, but i had some problems with cglib/metadata directives, so i started to write new dynamic code generation library (top over asm), and i made AOP style after method functionality, and i need to got correct wrapper class for method's primitive arguments.

Of course i could debug cglib, but i also wanted to learn java vm stuff, so why not.. =))

I asked this functionality because IF somewhere in future there is new primitive type in java, then i don't have to update my code to support it, and still there is method "isPrimitive" in java.lang.Class, so i just wonder if there is method to get wrapper..
Of course it isn't big job to change code, but still i want to do thing's right if there is possibility for it.. =)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
There's no such thing as "int.class". Several earlier posts have referred to this. The correct thing is "Integer.TYPE". There's a TYPE for each primitive, in its associated wrapper.

I don't know of a built-in way to get the right wrapper class from a primitive TYPE class. However, it's fairly easy to achieve the required result using a few if-else - ugly but effective.

If working with arrays, java.lang.Array has many useful methods which do that sort of thing.
[ February 11, 2005: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Arto Pastinen
Ranch Hand

Joined: Dec 13, 2002
Posts: 79
Ok. But why:
System.out.println(int.class.getClass()); // returns "class java.lang.Class"
I agree. Here's the link:
subject: resolver wrapper for primitive
jQuery in Action, 3rd edition