欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用反射获取与修改private的属性的值 博客分类: java基础 javareflect 

程序员文章站 2024-02-13 18:06:22
...
1.PrivateObject.java

可以看到PrivateObject 里有一个private 的属性String型的 privateString
没有为其实义getter,setter方法。

package org.iteye.bbjava.runtimeinformation;

import java.lang.reflect.Field;

public class PrivateObject {

	private String privateString = null;

	public PrivateObject(String privateString) {
		this.privateString = privateString;
	}

}


2.Test.java
这里将对PrivateObject类进行反射取值,是一种“暴力操作”,相信经常逛论坛的同学们,应该听过这个词。
还可以“暴力修改”。

package org.iteye.bbjava.runtimeinformation;

import java.lang.reflect.Field;

public class Test {
	 public static void main(String []args) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException{
		  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);
	  }
}


3.console infomaction:
引用
fieldValue = The Private Value


4.接下来将对Test.java进行修改,以对“暴力修改”是如何做到的进行说明。
对比上面的Test.java这里多了8行代码,这就是“暴力修改”的关键。
其中  privateStringField.setAccessible(true); 就是对private 属性修改的“权限开关”,当设置为true时,可以修改,为false时会抛出异常,动行时信息将会给出该异常.

package org.iteye.bbjava.runtimeinformation;

import java.lang.reflect.Field;

public class Test {
	 public static void main(String []args) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException{
		  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);

		  
		  privateStringField.setAccessible(true);
		  privateStringField.set(privateObject, "As you see,privateString's value is changed!");
		  String fieldValue1 = (String) privateStringField.get(privateObject);
		  System.out.println("fieldValue = " + fieldValue1);

		  privateStringField.setAccessible(false);
		  privateStringField.set(privateObject, "As you see,privateString's value is changed!");
		  String fieldValue2 = (String) privateStringField.get(privateObject);
		  System.out.println("fieldValue = " + fieldValue2);
	  }
}



5.console infomaction:
引用
fieldValue = The Private Value
fieldValue = As you see,privateString 'value is changed!
Exception in thread "main" java.lang.IllegalAccessException: Class org.iteye.bbjava.runtimeinformation.Test can not access a member of class org.iteye.bbjava.runtimeinformation.PrivateObject with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.reflect.Field.doSecurityCheck(Field.java:960)
at java.lang.reflect.Field.getFieldAccessor(Field.java:896)
at java.lang.reflect.Field.set(Field.java:657)
at org.iteye.bbjava.runtimeinformation.Test.main(Test.java:25)

相关标签: java reflect