12 Replies Latest reply: Jun 13, 2011 7:19 AM by Gen.Java RSS

    Compatible primitives

    Gen.Java
      Hi everyone,

      I need to see if two classes are primitive compatibles. For example, if one class is int.class and the other is Integer.class, then they are compatible.

      To do that, I compare (ignore case) the simple names of both classes, and hanlde int and char specially.

      Is there a better way ?

      Thanks
        • 1. Re: Compatible primitives
          EJP
          You are probably deeply involved in Reflection if you're asking this question, in which case you should take a look at java.beans.Statement and java.beans.Expression, which already handle all this stuff correctly.
          • 2. Re: Compatible primitives
            802316
            If you are using reflection and you use Field.get() it will always give you an Object, replacing primitives with wrappers.
            That way you have only Object comparison to worry about.
            • 3. Re: Compatible primitives
              Gen.Java
              EJP wrote:
              You are probably deeply involved in Reflection if you're asking this question, in which case you should take a look at java.beans.Statement and java.beans.Expression, which already handle all this stuff correctly.
              In a previous thread, a long time ago - sorry I couldn't locate it, I discussed reflection and I remember I said that Expression doesn't handle varargs. Also, there was another case that Expression couldn't handle. I hope I can locate this thread to tell you about it.

              Anyway, if you have an overloaded method and some of them use varargs, you have to know which one you need to call to be able to group the vararg values before sending them to Expression. If, however, you don't know which overloaded method to call, then Expression cannot help. And you have to implement Reflection the hard way.

              Anyway, I don't want to discuss Reflection anymore. I just need help with this problem I just posted.
              • 4. Re: Compatible primitives
                Gen.Java
                Peter Lawrey wrote:
                If you are using reflection and you use Field.get() it will always give you an Object, replacing primitives with wrappers.
                That way you have only Object comparison to worry about.
                Well, Field.getType() will give you the exact type (primitive, not wrapper).

                Again, I don't want to discuss Reflection. so, please let us just concentrate on the problem posted.
                • 5. Re: Compatible primitives
                  EJP
                  @Gen.Java I don't remember the discussion. I'm not crazy about your solution. A Map<Class<?>, Class<?>> is safer.
                  • 6. Re: Compatible primitives
                    DarrylBurke
                    Gen.Java wrote:
                    Well, Field.getType() ...

                    Again, I don't want to discuss Reflection. ...
                    What's the fully qualified name of the class <tt>Field</tt>?

                    db
                    • 7. Re: Compatible primitives
                      Gen.Java
                      Darryl Burke wrote:
                      What's the fully qualified name of the class <tt>Field</tt>?

                      db
                      java.lang.reflect.Field
                      • 8. Re: Compatible primitives
                        Gen.Java
                        EJP wrote:
                        A Map<Class<?>, Class<?>> is safer.
                        Do you mean store all possible combinations in this map. such as:

                        int.class,Integer.class,
                        Integer.class,int.class,
                        float.class,Float.class,
                        Float.class,float.class,...
                        • 9. Re: Compatible primitives
                          EJP
                          Yes
                          • 10. Re: Compatible primitives
                            Gen.Java
                            EJP wrote:
                            Yes
                            @EJP, thank you. I tried it and it is really a very simple, safe and fast solution.

                            I think my next challange is to find an easy way to initialize this map. It is easy to use Arrays.asList() to initialize a List with values stored in an array. But I cannot find a similar way for a Map.
                            • 11. Re: Compatible primitives
                              EJP
                              There isn't a way via a data table. I use anonymous initializers in cases like this:
                              {
                                map.put(int.class, Integer.class);
                                map.put(short.class, Short.class);
                                // ...
                              }
                              • 12. Re: Compatible primitives
                                Gen.Java
                                Good - thank you