How can I get the primitive type from an Object provided it isPrimitive.

A

anita1766

I'd like to be able to the Class int.class, boolean.class, etc., as the
case maybe, given an object which may or maynot be of primitive type.
I can ofcourse check if the object isPrimitive, but from what I can see
the only way to get the primitive Class type of an object is through
Integer.TYPE or Boolean.Type etc., I cannot do that because I really
dont know what kind of primitive I have and I DONT WANT to do a case
statement. I wish there was a way to cast it to an interface like
IPrimitive or something which implemented geType() or whatever.

There has to be an easy way of doing this. Seems like a very basic
requirement.
So to sum it up for those that dont read long ramblings..
I have
Object obj
I need to botain int.class, boolean.class etc., from obj in a generic
way with no comparisons to all the primitive type wrappers.

Thanks
Anita
 
E

Edwin Martin

I'd like to be able to the Class int.class, boolean.class, etc., as the

There is not such thing as int.class.
case maybe, given an object which may or maynot be of primitive type.
I can ofcourse check if the object isPrimitive, but from what I can see
the only way to get the primitive Class type of an object is through
Integer.TYPE or Boolean.Type etc., I cannot do that because I really
dont know what kind of primitive I have and I DONT WANT to do a case
statement. I wish there was a way to cast it to an interface like
IPrimitive or something which implemented geType() or whatever.

You can do:

if (object instanceof java.lang.Number) {
...
}

Then you check for: BigDecimal, BigInteger, Byte, Double, Float,
Integer, Long and Short.

Edwin Martin
 
E

Esmond Pitt

Edwin said:
There is not such thing as int.class.

The Java Language Specification # 15.8.2 says there is and so will your
compiler if you try it.
You can do:

if (object instanceof java.lang.Number) {
...
}

Then you check for: BigDecimal, BigInteger, Byte, Double, Float,
Integer, Long and Short.

Well, if he has a primitive type, BigDecimal &c are not the answer, he
is interested in int.class, float.class &c.
 
L

Lasse Reichstein Nielsen

I'd like to be able to the Class int.class, boolean.class, etc., as the
case maybe, given an object which may or maynot be of primitive type.

I'm not sure I can parse that sentence and see what you want to be
able to. Anyway, if you have an object, you already know that it is not
a primitive type. The primitive types, int, boolean, etc., are exactly
the ones that are not objects.
I can ofcourse check if the object isPrimitive, but from what I can see
the only way to get the primitive Class type of an object is through
Integer.TYPE or Boolean.Type etc., I cannot do that because I really
dont know what kind of primitive I have and I DONT WANT to do a case
statement.

By reading your summary below, I think I now understand the problem:
You have an object that is an instance of one of the primitive type
wrapper classes, and you want to find the corresponding primitive
type's Class object.

You don't want an eight-way switch (boolean, byte, short, char, int,
long, float, double). I don't see why not, though :)
I wish there was a way to cast it to an interface like
IPrimitive or something which implemented geType() or whatever.

There isn't one, although I do agree that there should be :).
Instead of doing the eight-way switch, you can use reflection
to pick out the "TYPE" property.

---
public static Class<?> primitiveClass(Object wrappedObject) {
Class<?> objectClass = wrappedObject.getClass();
java.lang.reflect.Field field;
try {
field = objectClass.getField("TYPE");
} catch (NoSuchFieldException e) {
return objectClass; // not a wrapper class! Exception instead?
}

Class<?> primitiveClass;
try {
primitiveClass = (Class<?>) field.get(null);
} catch (IllegalArgumentException e) {
// shoudn't happen
e.printStackTrace();
throw new Error("TYPE not static field of class", e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new Error("TYPE not accessibe field of class", e);
}
if (!primitiveClass.isPrimitive()) {
throw new IllegalArgumentException("Arguemnt not a wrapper class");
}
return primitiveClass;
}
---
There has to be an easy way of doing this. Seems like a very basic
requirement.

.... but there isn't.
So to sum it up for those that dont read long ramblings..
I have
Object obj
I need to botain int.class, boolean.class etc., from obj in a generic
way with no comparisons to all the primitive type wrappers.

Are you sure you don't want the comparison instead? It's shorter,
easier to read, and probably even more efficient:
---
public static Class<?> primitiveClass(Object wrappedObject) {
if (wrappedObject instanceof Boolean) { return Boolean.TYPE; }
else if (wrappedObject instanceof Number) {
if (wrappedObject instanceof Byte) { return Byte.TYPE; }
else if (wrappedObject instanceof Short) { return Short.TYPE; }
else if (wrappedObject instanceof Character) { return Character.TYPE; }
else if (wrappedObject instanceof Integer) { return Integer.TYPE; }
else if (wrappedObject instanceof Long) { return Long.TYPE; }
else if (wrappedObject instanceof Float) { return Float.TYPE; }
else if (wrappedObject instanceof Double) { return Double.TYPE; }
}
return wrappedObject.getClass();
}
 
C

Chris Smith

Lasse Reichstein Nielsen said:
---
public static Class<?> primitiveClass(Object wrappedObject) {
if (wrappedObject instanceof Boolean) { return Boolean.TYPE; }
else if (wrappedObject instanceof Number) {
if (wrappedObject instanceof Byte) { return Byte.TYPE; }
else if (wrappedObject instanceof Short) { return Short.TYPE; }
else if (wrappedObject instanceof Character) { return Character.TYPE; }
else if (wrappedObject instanceof Integer) { return Integer.TYPE; }
else if (wrappedObject instanceof Long) { return Long.TYPE; }
else if (wrappedObject instanceof Float) { return Float.TYPE; }
else if (wrappedObject instanceof Double) { return Double.TYPE; }
}
return wrappedObject.getClass();
}

One minor nit. In generaly, I'd recommend using the literal syntax,
"boolean.class", "byte.class", etc. instead of "Boolean.TYPE" and kin.
The literal syntax is cleaner and probably more obvious to the great
majority of Java programmers, and anyone doing modern software
development is unlikely to be using a compiler old enough that the older
compatibility form is required.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

anita1766

Thanks for all your replies.

I was thinking of going the reflection way and looking up type. But it
looks really ugly... and I dont want the case either. So I finally did
something equivalent of a case. The place where I get the original
object stores a ref to its primitive type, I just get it from there.
sheesh!

Does anyone have any idea why they made this thing so hard to figure
out ? I mean what is the point of asking an object if it is a primitive
or not (isPrimitive()), if I cannot access it primitive "xyz.class"
without hard-coding ? Ofcourse I can see some of its uses, but not
having the other mapping just makes it so much less useful.
It would have been so simple to make all the Primitive types inherit
from a "Primitive" interface with just one thing, a TYPE field.

Anita
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top