Constructor.newInstance():java.lang.IllegalArgumentException:参数类型不匹配

时间:2016-01-20 09:36:10

标签: java classloader

我有以下课程:

public class DefaultContainer extends AbstractContainer {

private JobBeanFactory jobBeanFactory;

private ScheduleManager scheduleManager;

/**
 * for local
 * @param classLoader
 * @param packagesToScan
 */
public DefaultContainer(ClassLoader classLoader, String packagesToScan) {
    super(classLoader, packagesToScan);
    this.jobBeanFactory = new DefaultJobBeanFactory();
    Configuration configuration = new Configuration(classLoader);
    this.scheduleManager = new DefaultScheduleManager(configuration, this.jobBeanFactory, getJobScanner().getJobDescriptorList());
}

/**
 * for remote
 * @param configuration
 * @param packagesToScan
 * @param jarUrls
 */
public DefaultContainer(Configuration configuration, String packagesToScan, String... jarUrls) {
    super(packagesToScan, jarUrls);
    this.jobBeanFactory = new DefaultJobBeanFactory();
    this.scheduleManager = new DefaultScheduleManager(configuration, this.jobBeanFactory, getJobScanner().getJobDescriptorList());
}


public ScheduleManager scheduleManager() {
    return scheduleManager;
}

}

然后,我使用反射加载并创建一个类实例。

Class<Container> containerClass = (Class<Container>) classLoader.loadClass("xxxxx.DefaultContainer");
Constructor<Container> containerConstructor = containerClass.getConstructor(Configuration.class, String.class, String[].class);
container = containerConstructor.newInstance(getConfiguration(), packagesToScan, new String[]{jarUrl});

但是当执行newInstance的方法时,我得到一个例外:

com.zuoxiaolong.niubi.job.core.exception.NiubiException: java.lang.IllegalArgumentException: argument type mismatch
at com.zuoxiaolong.niubi.job.cluster.node.AbstractRemoteJobNode.createContainer(AbstractRemoteJobNode.java:100)
at com.zuoxiaolong.niubi.job.cluster.node.AbstractRemoteJobNode.getContainer(AbstractRemoteJobNode.java:67)
at com.zuoxiaolong.niubi.job.cluster.node.MasterSlaveNode.executeOperation(MasterSlaveNode.java:264)
at com.zuoxiaolong.niubi.job.cluster.node.MasterSlaveNode.access$1000(MasterSlaveNode.java:56)
at com.zuoxiaolong.niubi.job.cluster.node.MasterSlaveNode$3.childEvent(MasterSlaveNode.java:253)
at org.apache.curator.framework.recipes.cache.PathChildrenCache$5.apply(PathChildrenCache.java:516)
at org.apache.curator.framework.recipes.cache.PathChildrenCache$5.apply(PathChildrenCache.java:510)
at org.apache.curator.framework.listen.ListenerContainer$1.run(ListenerContainer.java:92)
at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297)
at org.apache.curator.framework.listen.ListenerContainer.forEach(ListenerContainer.java:83)
at org.apache.curator.framework.recipes.cache.PathChildrenCache.callListeners(PathChildrenCache.java:507)
at org.apache.curator.framework.recipes.cache.EventOperation.invoke(EventOperation.java:35)
at org.apache.curator.framework.recipes.cache.PathChildrenCache$9.run(PathChildrenCache.java:759)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.zuoxiaolong.niubi.job.cluster.node.AbstractRemoteJobNode.createContainer(AbstractRemoteJobNode.java:98)
    ... 19 more

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

尝试将String array投射到Object。它应该有所帮助。

containerConstructor.newInstance(getConfiguration(), packagesToScan, (Object) new String[]{ jarUrl });

Here有人遇到同样的问题: