0
我有下面的類:Constructor.newInstance():java.lang.IllegalArgumentException異常:參數類型不匹配
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
誰能幫助我?
我確保參數類型都與匹配[Configuration.class,String.class,字符串[]類] –
確定'getConfiguration() '返回與構造函數參數相同的'Configuration'類?你檢查過'packagesToScan'確實是一個'String'嗎? – Mena
你也明確地將你的班級轉換爲班級。如果'Container'是一個類,它是否有匹配的構造函數? –
Mena