Java Reflection - Private Fields and Methods
2015-05-31 11:51
561 查看
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.
Reflection:
This code example will print out the text "fieldValue = The Private Value", which is the value of the private field
created at the beginning of the code sample.
Notice the use of the method
class, not fields declared in any superclasses.
Notice the line in bold too. By calling
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.
The methods
Here is a simple example of a class with a private method, and below that the code to access that method via Java Reflection:
This code example will print out the text "returnValue = The Private Value", which is the value returned by the method
created at the beginning of the code sample.
Notice the use of the method
class, not methods declared in any superclasses.
Notice the line in bold too. By calling
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.
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.
Accessing Private Fields
To access a private field you will need to call theClass.getDeclaredField(String name)or
Class.getDeclaredFields()method. The methods
Class.getField(String name)and
Class.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
privateStringof the
PrivateObjectinstance
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
Fieldinstance, 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 theClass.getDeclaredMethod(String name, Class[] parameterTypes)or
Class.getDeclaredMethods()method.
The methods
Class.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
PrivateObjectinstance
created at the beginning of the code sample.
Notice the use of the method
PrivateObject.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
Methodinstance, 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.
相关文章推荐
- Java Reflection - Fields
- Java Reflection - Methods
- Java Reflection - Classes
- Java Reflection - Constructors
- 反射0-Java Reflection Tutorial
- Java 多线程详解
- Java语法错误之-执行不到的代码(Unreachable code)
- java范型的应用
- java中的各种数据类型在内存中存储的方式
- 杭电ACM2019java做法
- 杭电ACM2019java做法
- JAVA -- 线程的sleep()方法为什么是静态的
- JAVA -- 线程的sleep()方法为什么是静态的
- JAVA -- 线程的sleep()方法为什么是静态的
- hdfs查看集群目录报javaIO异常
- OSS实现多文件多线程的断点下载(java)
- Java知多少(78)Java向量(Vector)及其应用
- java null空判断问题
- Vijava 学习笔记 删除虚拟机(delete Virtual Machine)
- 基本类型与包装类的区别