3

Sorry for the unclear question but I am truly lost why we need isPrimitive() in the first place since I cannot use it (sorry I just cannot use it when I need it ;( sad face here).

After reading posts here and there, I found somes usages as

int.class.isPrimitive()

But I'd like to have something as

boolean isTrue = true;
System.out.println(isTrue.class.isPrimitive());
System.out.println(Boolean.valueOf(isTrue).getClass().isPrimitive());

I am trying to check the types while traversing the fields of an object; what I can do now is to

private static boolean isPrimitiveWrapper(Object obj) {
    return obj.getClass() == Boolean.class ||
            obj.getClass() == Byte.class ||
            obj.getClass() == Character.class ||
            obj.getClass() == Short.class ||
            obj.getClass() == Integer.class ||
            obj.getClass() == Long.class ||
            obj.getClass() == Float.class ||
            obj.getClass() == Double.class;
}

But after checking around, I think there should be something wrong with it but I don't know what it is.

Any use cases for that will be really appreciated ;)

I am trying to be not too paranoid...trying pretty hard already

Hearen
  • 7,420
  • 4
  • 53
  • 63
  • may be useful when you use reflection api – Akash Shah Mar 28 '19 at 11:45
  • I am actually using reflection now to do the traversing but its usage seems too restrictive and I am quite confused at it. – Hearen Mar 28 '19 at 11:46
  • 1
    It's not really clear at the moment what you're trying to achieve. You're taking the class of an *object*, and that can never be a primitive - but if you were to use the type *of a field* it could easily be primitive. – Jon Skeet Mar 28 '19 at 11:47
  • I am trying to use reflection to traverse all the fields within a class and as long as it's a printable value (private fields and all fields inherited included even the super parent class Object), I will just print it out. Kind of an object details dumper/printer. – Hearen Mar 28 '19 at 11:48
  • Everything is printable in java by default FYI. The toStrings override can change how it prints. [Object#toString](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) EDIT: So it shouldn't matter what class or primitive field and if the field is null, "null" will print. – Mr00Anderson Mar 28 '19 at 11:49
  • @Hearen you mean by using reflection? then how this question is related? – Akash Shah Mar 28 '19 at 11:52
  • @mr00anderson, no, that's not working. it will print lots of messy fields. What I want is just eight primitives and strings. – Hearen Mar 28 '19 at 11:52
  • @akashshah I need to check the type is what I want (primitives/wrappers & string) – Hearen Mar 28 '19 at 11:53
  • What your asking is already accomplish-able. I see no issue or question. Just start trying out what you know already. – Mr00Anderson Mar 28 '19 at 11:54
  • I am **not** using `isPrimitive()` and I cannot use it that's why I am asking why there is a `isPrimitive()`. In my case, is there no chance to use it? – Hearen Mar 28 '19 at 11:55
  • 1
    This explains why the method exist. When you used it what happen? Why can you not use it? [Class#isPrimative()](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isPrimitive--) EDIT: The answer is already been answer on all those post. You have to combine a wrapper check, a regular check and a string check. – Mr00Anderson Mar 28 '19 at 11:57
  • Sorry for my paranoid, just lost there...my questions listed all your askings, I suppose. – Hearen Mar 28 '19 at 11:59
  • then in which case we will use it? – Hearen Mar 28 '19 at 12:01

1 Answers1

9

As the primitive types cannot be dealt with as Object in some cases, like arrays, it is nice as first discriminator.

Object cloneObject(Object obj) {
    Class<?> type = obj == null ? Object.class : obj.getClass();
    if (type.isArray()) {
        Class<?> elemType = type.getComponentType();
        if (!elemType.isPrimitive()) {
            Object[] copy = ...
        } else {
            // Must distinguish between int/double/boolean/...
            ... int[] ... double[] ...
        }
    }

Object inta = new int[] { 2, 3, 5, 7 };
int[] pr = (int[]) cloneObject(inta);
Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Looks great! Upvoted ;) I came across it just now. Is there any other cases possible? – Hearen Mar 28 '19 at 12:12
  • 2
    Actually what is missing in the reflection API is getting the wrapper Object class of a primitive class, a bit like what you had above. Or for your own serialisation to bytes. – Joop Eggen Mar 28 '19 at 12:18
  • 4
    @JoopEggen reminds me on [this Q&A](https://stackoverflow.com/q/52988458/2711488), e.g. `MethodHandles.zero(primitiveType).invoke().getClass()` would do. But, of course, that’s not the simplest API one can imagine… – Holger Mar 28 '19 at 12:39
  • 1
    @Holger indeed better sample code. We'll see what primitive types will become in the next java, `List` and such. – Joop Eggen Mar 28 '19 at 12:46