2

How I can get constructor reflectively, if its param is Object ... objects.

My constructor:

  public MyClass ( Object ... objects )
  {
    if ( ! ( objects == null ) )
    {
      if ( objects.length > 0 && objects [ 0 ] instanceof Long )
      {
        setLatency ( ( Long ) objects [ 0 ] ) ;
      }
    }
  }

How I get it now:

Class< ? > clazz = Class.forName ( "MyClass" ) ;

Constructor< ? > clazzConstructor = clazz.getConstructor ( Object [ ].class ) ;

What I try to do:

Long latency = 1000L ;

MyClass myInstance = ( MyClass ) clazzConstructor.newInstance ( latency ) ;

And I get java.lang.IllegalArgumentException: argument type mismatch

If latency == null, everything works.

3
  • How about newInstance(new Object[]{latency})? Commented Jun 25, 2012 at 14:01
  • I tried, but it still don't work. Commented Jun 25, 2012 at 14:03
  • 1
    @Banthar It would work but for the fact that newInstance() itself is varargs. Commented Jun 25, 2012 at 14:09

2 Answers 2

8

Your constructor is expecting an object array but you're passing a single Long to it.

Wrapping latency into an object array will work, although be careful as newInstance() itself is expecting Object ..., and if you pass it nothing but an Object[], it will interpret it as a list of arguments. So you'll have to do something like this:

MyClass myInstance = ( MyClass ) clazzConstructor.newInstance ( (Object)new Object[] { latency } ) ;

or

MyClass myInstance = ( MyClass ) clazzConstructor.newInstance ( new Object[] { new Object[] { latency } } ) ;

The first one "fools" the compiler into wrapping your object array into another, the second one does it explicitly instead.

(Passing null only worked because null is null, no matter what the declared type of the parameter is.)

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't a Long an Object though?
@skynorth Where it says Constructor< ? > clazzConstructor = clazz.getConstructor ( Object[].class ) ;
@Shakesbeer I tried both solutions and they worked. How does it fail for you?
7

Try it this way

MyClass myInstance = (MyClass) clazzConstructor
        .newInstance(new Object[] { new Object[] { latency } });

newInstance(Object ... initargs) needs array of objects as arguments. Buy your fist argument in constructor is also array of object Object... o so you need to wrap it again in Object array.

I tested it with this code and in seems to work fine

class MyClass {
    private Long latency;

    public void setLatency(Long latency) {
        this.latency = latency;
    }
    public Long getLatency() {
        return latency;
    }

    public MyClass(Object... objects) {
        if (!(objects == null)) {
            if (objects.length > 0 && objects[0] instanceof Long) {
                setLatency((Long) objects[0]);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Class<?> clazz = MyClass.class;

        Constructor<?> clazzConstructor = clazz.getConstructor(Object[].class);
        System.out.println(clazzConstructor);

        Long latency = 1000L ;
        MyClass myInstance = (MyClass) clazzConstructor
                .newInstance(new Object[] { new Object[] { latency } });
        System.out.println(myInstance);
        System.out.println(myInstance.getLatency());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.