Java Reflection calling constructor with primitive types -


i have method in test framework creates instance of class, depending on parameters passed in:

public void test(object... constructorargs) throws exception {     constructor<t> con;     if (constructorargs.length > 0) {         class<?>[] parametertypes = new class<?>[constructorargs.length];         (int = 0; < constructorargs.length; i++) {             parametertypes[i] = constructorargs[i].getclass();           }         con = clazz.getconstructor(parametertypes);     } else {         con = clazz.getconstructor();     } } 

the problem is, doesn't work if constructor has primitive types, follows:

public range(string name, int lowerbound, int upperbound) { ... }  .test("a", 1, 3); 

results in:

java.lang.nosuchmethodexception: range.<init>(java.lang.string, java.lang.integer, java.lang.integer) 

the primitive ints auto-boxed in object versions, how them calling constructor?

use integer.type instead of integer.class.

as per javadocs, "the class instance representing primitive type int."

you can use int.class. it's shortcut integer.type. not classes, primitive types can type.class in java.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -