java反射调用方法时,Class . can not access a member of class . with modifiers "private" 等异常解决
来源:互联网 发布:淘宝企业店铺委托书 编辑:程序博客网 时间:2024/05/25 08:13
先看javabean类:目的使用反射调用私有方法:
package com.imooc.reflect;public class Student { private void add(){ System.out.println("增加数据!"); } private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(int id, String name) { super(); this.id = id; this.name = name; } public Student() { }}
main测试方法
public static void main(String[] args) throws Exception { Class c1=Student.class; Object obj=(Object)c1.newInstance(); Method add = c1.getDeclaredMethod("add"); add.invoke((Student)obj); }
这时,就会报Class . can not access a member of class . with modifiers “private” 异常。
解决方案:添加一行代码:add.setAccessible(true);
Method add = c1.getDeclaredMethod("add");add.setAccessible(true);add.invoke((Student)obj);
当然最后调用时也可以写成:add.invoke(obj);
0 0
- java反射调用方法时,Class . can not access a member of class . with modifiers "private" 等异常解决
- Java-Struts2:Can not access a member of class with modifiers "public"
- Gson解析内部类时报can not access a member of class xxx with modifiers "final"
- xxx can not access a member of class xxxx with modifiers ""
- can not access a member of with modifiers "public"
- can not access a member of with modifiers "public"
- java 问题之一“can not access a member of with modifiers
- Class ognl.OgnlRuntime can not access a member of class
- java.lang.IllegalAccessException:Class org.apache.axis.providers.java.JavaProvider can not access a member of class MyMath with
- Caused by: java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class
- java.lang.IllegalAccessException: Class javax.el.BeanELResolver can not access a member of class...
- Caused by: java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of class
- Class ognl.OgnlRuntime can not access a member of问题
- IllegalAccessException: Class A can not access a member of class B 的一种原因分析与解决
- struts2--java.lang.IllegalAccessException: Class ognl.OgnlRuntime can not access a member of...问题解决方案
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- 安全问题
- Java NIO之MappedByteBuffer,高效文件内存映射
- 36.In your production database, data manipulation language (DML) operations are executed on the
- HDOJ 1070 Milk ( 喝牛奶问题 )
- Java-Vector
- java反射调用方法时,Class . can not access a member of class . with modifiers "private" 等异常解决
- 无向图 tarjan 总结
- 分享:AndroidStudio翻译插件:ECTranslation
- Linux文件权限设置
- Centos6.5下虚拟机Linux 关于g++,gdb,ctags安装以及vim的配置
- python实战之实现excel读取、统计、写入
- 基础html5
- Java设计模式—责任链模式
- 2016多校第一场1004 hdu 5726 GCD