Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

resolver wrapper for primitive

 
Arto Pastinen
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you need this for?
 
Arto Pastinen
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arto Pastinen
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. But why:
System.out.println(int.class.getClass()); // returns "class java.lang.Class"
??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic