java.lang.InstantiationException: cn.tisson.datasync.DataSyncManager$StaffRefresherat java.lang.Class.newInstance0(Class.java:357)at java.lang.Class.newInstance(Class.java:325)at cn.tisson.datasync.DataSyncManager.(DataSyncManager.java:46)at cn.tisson.datasync.DataSyncThread.init(DataSyncThread.java:48)at cn.tisson.datasync.DataSyncThread.(DataSyncThread.java:36)at cn.tisson.datasync.DataSyncServer.service(DataSyncServer.java:46)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:601)at cn.tisson.common.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:141)at cn.tisson.service.ModuleThread.run(ModuleThread.java:65)at java.lang.Thread.run(Thread.java:722)[11-21 11:41:13][ERROR][cn.tisson.datasync.DataSyncManager][Thread-1]class cn.tisson.datasync.DataSyncManager$StaffRefresher 不能被实例化.
网络上有好多说于关于没有默认无参构造方法的原因,但我的却不是这个原因!
我推测的是因为当前的类属于非静态内部类,然后发现的外部类没有初始化所造成的.
但真正的原因却是:一个非静态的嵌套类的构造器,在编译的时候会将一个隐藏的参数作为它的第一个参数,这个参数表示它的直接外围实例。如果使用反射创建内部类,则要传递个隐藏参数的唯一方法就是使用java.lang.reflect.Constructor。
Constructor c = Inner.class.getConstructor(Outer.class);//获取带参数的内部类构造函数
System.out.println(c.newInstance(Outer.this));//反射时还需传进外围类
/** * 初始化 */ public void init() { latches = new HashSet<AwaitObject>(); refreshers = new HashSet<Runnable>(); for (Class c : ClassUtils.getInnerClasses(manager.getClass(), Refresher.class)) { try { this.refreshers.add((Runnable) c.newInstance()); this.latches.add(new AwaitObject()); } catch (InstantiationException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. logger.error(c + " 不能被实例化, ", e.getMessage()); } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. logger.error(c + " 不能被访问, ", e.getMessage()); } } }
修改后:/** * 初始化 */ public void init() { latches = new HashSet<AwaitObject>(); refreshers = new HashSet<Runnable>(); for (Class c : ClassUtils.getInnerClasses(manager.getClass(), Refresher.class)) { try { Constructor constructor = c.getConstructor(DataSyncManager.class); this.refreshers.add((Runnable)constructor.newInstance(DataSyncManager.this)); this.latches.add(new AwaitObject()); } catch (InstantiationException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. logger.error(c + " 不能被访问, ", e.getMessage()); } catch (NoSuchMethodException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. logger.error(c + " 没找到此方法--:getConstructor, ", e.getMessage()); } catch (InvocationTargetException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. logger.error(c + " 不能被实例化, ", e.getMessage()); } } }
正常运行...