BeanUtils访问静态内部类属性问题
BeanUtils(commons-beanutils-1.8.0)访问静态内部类属性访问不到,抛出的异常更加莫名其妙,非静态内部类完全没问题,不知道这是不是BeanUtils的Bug,哪位仁兄能解释下,下面是测试代码
@Test public void testBeanUtils() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{ User user = new User() ; user.setUsername("wenzuojing"); String value = BeanUtils.getProperty(user, "username") ; Assert.assertEquals(value, "wenzuojing"); } static class User{ private String username ; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
抛出异常
java.lang.NoSuchMethodException: Property 'username' has no getter method in class 'class org.wzj.htmlunit.TestMain$User' at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1318) at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762) at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715) at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741) at org.apache.commons.beanutils.BeanUtils.getProperty(BeanUtils.java:382) at org.wzj.htmlunit.TestMain.testBeanUtils(TestMain.java:32) 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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在回复有点晚了吧, 哈哈
你给内部类User加个public就可以了
使用BeanUtils工具包去操作类中的方法,这个被操作的类必须是public修饰的类。你定义的class不是被public修饰的,所以报错。你试试把User这个类另外定义在一个文件中试试就可以了。我也是今天遇到这个问题查了资料才知道的。希望能帮到你。