1

Is there a way in Java to determine if Autoboxing was done or not?

For example

void functionInt(Integer i) {
//Determine if Integer was passed or int was passed. Is it possible?
}

int i = 1;
int ii = new Integer(1);
functionInt(i);
functionInt(ii);

Can the callee differentiate these 2 calls?

vikky.rk
  • 3,989
  • 5
  • 29
  • 32

1 Answers1

4

functionInt will always be passed an Integer, and there is no way to determine if that Integer was created as a result of autoboxing or not.

What you could do is create overloaded functions:

void functionInt(Integer i);
void functionInt(int i);

/edit

If you have the following class:

public class Foo {
    public Foo(int primitive) {
        System.out.println("Created!");
    }
}

You can call Foo.class.getConstructor(int.class).newInstance(new Integer(5)) without any problems. It should be fairly simple to check for a Constructor that has int.class as a parameter instead of java.util.Integer if one with java.util.Integer cannot be found.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I want to create a function that uses refection to call the corresponding constructor on a Class. I pass arguments as Object Array, and later I need to call the constructor that takes primitive int instead of Integer. Otherwise I will have to a lot of processing to determine the correct constructor. – vikky.rk Jul 15 '12 at 00:23
  • 2
    @vikky.rk If this is your exact problem, then you should have asked that as your question. See my edit. – Jeffrey Jul 15 '12 at 00:39
  • My intention is to create a function " public static T createInstance(Class cObj, Obj... initArgs)" which creates a new instance of type T with initArgs being the constructor arguments. So I need to match the corresponding constructor and call Constructor.newInstance(initArgs). Matching is where I am facing problems differentiating Integer vs int . – vikky.rk Jul 15 '12 at 01:30
  • @vikky.rk And I outlined a way for you to do so in my edit. You see if you can find a `Constructor` that takes `java.util.Integer`, and if you can't, you search for one that would take `int.class` instead. – Jeffrey Jul 15 '12 at 01:34
  • Yes I know about int.class and searching constructors. I just wanted to see if there was an easy way to generate the Class> cObjs from initArgs, and pass it to getConstructor(cObjs).newInstance() instead of doing 'getConstructors' and searching for the right constructor. If there were no primitive types, all i have to do is to do cObjs[i] = initArg[i].getClass(). Or if I could determine if initArg[i] was autoboxed, then I could do cObjs[i] = int.class (for an Integer). Without that I will have to process all the constructors. I was just trying to see if I can avoid that. – vikky.rk Jul 15 '12 at 01:54
  • 2
    @vikky.rk Another thing you should probably consider is that `Class.getConstructor` doesn't consider the class hierarchy: If your `Class` has `public Foo(Object)`, `Foo.class.getConstructor(Integer.class)` will throw a `NoSuchMethodException` even though `new Foo(new Integer(5))` would compile. – Jeffrey Jul 15 '12 at 02:19
  • Agreed. Thats why I asked this Question :) http://stackoverflow.com/questions/11488861/java-reflection-library-that-has-a-function-to-create-newinstance-for-any-class – vikky.rk Jul 15 '12 at 02:26
  • FYI: There's no "int.class". It's Integer.TYPE is the class you'd use when searching for the constructor that takes the primitive value. – Matt Jul 15 '12 at 06:41
  • @Matt No, there is an `int.class`. Try it. – Jeffrey Jul 15 '12 at 13:20