当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

利用反射获取与修改private的属性的值

发表于: 2012-11-16   作者:BBjava   来源:转载   浏览:
摘要: 1.PrivateObject.java 可以看到PrivateObject 里有一个private 的属性String型的 privateString 没有为其实义getter,setter方法。 package org.iteye.bbjava.runtimeinformation; import java.lang.reflect.Field; public c
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)

利用反射获取与修改private的属性的值

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号