Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

resolver wrapper for primitive

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would you need this for?
 
Arto Pastinen
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.. =)
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. But why:
System.out.println(int.class.getClass()); // returns "class java.lang.Class"
??
reply
    Bookmark Topic Watch Topic
  • New Topic