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

Discussion in 'Java' started by anita1766@yahoo.com, Jun 5, 2005.

  1. Guest

    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
    , Jun 5, 2005
    #1
    1. Advertising

  2. Edwin Martin Guest

    wrote:
    > 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

    --
    http://www.bitstorm.org/edwin/en/
    Edwin Martin, Jun 5, 2005
    #2
    1. Advertising

  3. Esmond Pitt Guest

    Edwin Martin wrote:
    > wrote:
    >
    >> I'd like to be able to the Class int.class, boolean.class, etc., as the

    >
    >
    > 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.
    Esmond Pitt, Jun 5, 2005
    #3
  4. Re: How can I get the primitive type from an Object provided itisPrimitive.

    writes:

    > 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();
    }
    ---

    Hope this helps.
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jun 5, 2005
    #4
  5. Chris Smith Guest

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

    Lasse Reichstein Nielsen <> wrote:
    > ---
    > 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
    Chris Smith, Jun 5, 2005
    #5
  6. Guest

    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
    , Jun 7, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    8
    Views:
    3,310
    Leor Zolman
    May 14, 2004
  2. Replies:
    7
    Views:
    601
    Victor Bazarov
    May 9, 2005
  3. Replies:
    2
    Views:
    425
  4. Daniel Pitts
    Replies:
    7
    Views:
    474
  5. Jeremy
    Replies:
    1
    Views:
    363
    John Saunders [MVP]
    Jul 18, 2007
Loading...

Share This Page