samedi 18 juillet 2015

java.lang.IllegalArgumentException: wrong number of arguments

for (Iterator<SpecifierObjects> iterator = instances.iterator(); iterator
            .hasNext();) {
        InstanceSpecifierObject instanceSpecifierObject = (InstanceSpecifierObject) iterator
                .next();
        try {
            Class<?> cast = Class.forName(instanceSpecifierObject
                    .getInterfaceName());
            Class<?> clazz = Class.forName(instanceSpecifierObject
                    .getClassName());
            objects.put(instanceSpecifierObject.getObjectName(),
                    cast.cast(clazz.getConstructor(String[].class)
                            .newInstance(
                                    instanceSpecifierObject
                                            .getFilesName())));
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | NoSuchMethodException
                | SecurityException e) {
            e.printStackTrace();
        }
    }

The error log is

    instances=E://External/workspace/CoreLibs/src/instanceSpecification.xml
E://External/workspace/CoreLibs/src/instanceSpecification.xml
java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.framew.confinit.ApplicationResource.createInstancesFromSpecfication(ApplicationResource.java:86)
    at com.framew.confinit.ApplicationResource.importResource(ApplicationResource.java:57)
    at com.framew.confinit.ApplicationResource.main(ApplicationResource.java:39)

instanceSpecifierObject.getFilesName() returns String[] of size 2. I even tried typecasting to Object[] before passing to the newInstance() method but doesn't work.

instanceSpecifierObject.getInterfaceName() returns org.springframework.context.ApplicationContext.

instanceSpecifierObject.getClassName() returns org.springframework.context.support.ClassPathXmlApplicationContext.

I need to pass String[] argument to constructor of the class for which a new Instance to created.

Aucun commentaire:

Enregistrer un commentaire