Java reflection: accessing private fields and methods
来源:互联网 发布:江苏跨省网络诈骗案件 编辑:程序博客网 时间:2024/05/17 08:47
Origin: http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html
Despite the common belief it is actually possible to access private fields and methods of other classes via Java Reflection. It is not even that difficult. This can be very handy during unit testing. This text will show you how.
Note: This only works when running the code as a standalone Java application, like you do with unit tests and regular applications. If you try to do this inside a Java Applet, you will need to fiddle around with the SecurityManager. But, since that is not something you need to do very often, it is left out of this text so far.
Here is a list of the topics covered in this text:
- Accessing Private Fields
- Accessing Private Methods
Accessing Private Fields
To access a private field you will need to call the Class.getDeclaredField(String name)
orClass.getDeclaredFields()
method. The methods Class.getField(String name)
andClass.getFields()
methods only return public fields, so they won't work. Here is a simple example of a class with a private field, and below that the code to access that field via Java Reflection:
public class PrivateObject { private String privateString = null; public PrivateObject(String privateString) { this.privateString = privateString; }}
PrivateObject privateObject = new PrivateObject("The Private Value");Field privateStringField = PrivateObject.class. getDeclaredField("privateString");privateStringField.setAccessible(true);String fieldValue = (String) privateStringField.get(privateObject);System.out.println("fieldValue = " + fieldValue);
This code example will print out the text "fieldValue = The Private Value", which is the value of the private field privateString
of the PrivateObject
instance created at the beginning of the code sample.
Notice the use of the method PrivateObject.class.getDeclaredField("privateString")
. It is this method call that returns the private field. This method only returns fields declared in that particular class, not fields declared in any superclasses.
Notice the line in bold too. By calling Field.setAcessible(true)
you turn off the access checks for this particular Field
instance, for reflection only. Now you can access it even if it is private, protected or package scope, even if the caller is not part of those scopes. You still can't access the field using normal code. The compiler won't allow it.
Accessing Private Methods
To access a private method you will need to call the Class.getDeclaredMethod(String name, Class[] parameterTypes)
or Class.getDeclaredMethods()
method. The methodsClass.getMethod(String name, Class[] parameterTypes)
and Class.getMethods()
methods only return public methods, so they won't work. Here is a simple example of a class with a private method, and below that the code to access that method via Java Reflection:
public class PrivateObject { private String privateString = null; public PrivateObject(String privateString) { this.privateString = privateString; } private String getPrivateString(){ return this.privateString; }}
PrivateObject privateObject = new PrivateObject("The Private Value");Method privateStringMethod = PrivateObject.class. getDeclaredMethod("getPrivateString", null);privateStringMethod.setAccessible(true);String returnValue = (String) privateStringMethod.invoke(privateObject, null); System.out.println("returnValue = " + returnValue);
This code example will print out the text "returnValue = The Private Value", which is the value returned by the method getPrivateString()
when invoked on the PrivateObject
instance created at the beginning of the code sample.
Notice the use of the methodPrivateObject.class.getDeclaredMethod("privateString")
. It is this method call that returns the private method. This method only returns methods declared in that particular class, not methods declared in any superclasses.
Notice the line in bold too. By calling Method.setAcessible(true)
you turn off the access checks for this particular Method
instance, for reflection only. Now you can access it even if it is private, protected or package scope, even if the caller is not part of those scopes. You still can't access the method using normal code. The compiler won't allow it.
Author notes: Sometimes, especially when designing a tool which accepts customizable configurations, we need to create an object whose type is determined at runtime, and invoke some methods of this object. Clearly we can not create an object this way: Type obj = new Type(); as type is unknown until runtime. Java reflection helps!
- Java reflection: accessing private fields and methods
- Java Reflection - Private Fields and Methods
- Java Reflection - Private Fields and Methods
- 非深入探寻Java反射机制: Private Fields and Private Methods
- Java Reflection Tutorial for Classes, Methods, Fields, Constructors, Annotations and much more
- Java Reflection - Fields
- Java Reflection - Methods
- Day2-6.Fields and methods
- ssh离线查询错误:Error accessing field [private java.lang.String 包名.BaseDict.dictId] by reflection
- Java核心思想学习笔记002(Static Fields and Methods)
- Java Reflection 教程(4):Fields 成员变量
- Java Reflection 教程(5):Methods 类方法
- Java RTTI and Reflection
- Private Methods
- Java class and methods.
- OpenERP Server Developers Documentation之Objects, Fields and Methods
- Accessing Related Fields in PeopleCode
- django fields lookup methods
- 学习struts2
- vimrc
- 使用Settings Bundle为程序添加设置项
- C++(五) access函数判断文件是否存在
- 禅道程序员的10条原则
- Java reflection: accessing private fields and methods
- c# winform登录路由器的方法
- 制作精简Linux(精华)
- 3个数排序
- 软件设计之框架
- struts2笔记
- 数据库学习2012.06.07
- 如何让笔记本的无线与有线网卡不冲突
- 官方地址