android开发笔记之java的reflect的理解(在android平台上)

曾经,兄弟我对那些使用java reflect(反射)的大神是五体投地的仰慕,觉得那些神仙一般的人物真是历害,能做用传说中的绝世神功,在java的世界,没有任何的限制,可以对一个对象的所有方法和变量进行读写。

       今天,因为要解决一个bug,就对reflect研究了一下,做了一个demo:

     reflect主要是对java对象实现访问,修改,包括所有的变量,方法,构造方法。这就给使用者,提供了一种方式,冲破了private对变量和方法的限制,可以随心所欲的对变量进行操作,特别是在android开发中,我们可以使用reflect对系统中的private,hide方法进行调用。reflect是在java.lang.reflect中。

     这个Demo,主要是对private变量进行读取,并设置,对private方法进行调用,对private构造方法进行构造新的对象,

1.MyTestClass.java

import android.util.Log;

public class MyTestClass {
	
	public static final String TAG = "MyTestClass";
	
	private int aInt = 0;
	private boolean bBoolean = false;
	private String sString = "test";
	
	public MyTestClass(){		
		Log.i(TAG,"MyTestClass()");
	}

        private MyTestClass(int aInt,boolean bBoolean,String sString){
		Log.i(TAG,"MyTestClass(int aInt,boolean bBoolean,String sString)");
		this.aInt = aInt;
		this.bBoolean = bBoolean;
		this.sString = sString;
	}
	
	private void setInt(int aInt){
		Log.i(TAG,"setInt(int aInt)");
		this.aInt = aInt;
	}
	
	private void setBoolean(boolean bBoolean){
		Log.i(TAG,"setBoolean(boolean bBoolean)");
		this.bBoolean = bBoolean;
	}
        private void setString(String sString){
		Log.i(TAG,"setString(String sString)");
		this.sString = sString;
	}
	
	private void getVariable(){
		Log.i(TAG,"getVariable()");
		Log.i(TAG,"aInt:" + aInt + "---bBoolean:" + bBoolean + "----sString:" + sString);
	}
}	


2.TestReflectActivity.java

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class TestReflectActivity extends Activity {
	
	public static final String TAG = "MyTestClass";
	
	private MyTestClass myTestClass;
	private Class myClass;
	private Method method;
	private Constructor constructor;
        protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_reflect);
		
		myTestClass = new MyTestClass();
		myClass = myTestClass.getClass();
		
		try {
                        method = myClass.getDeclaredMethod("getVariable", new Class[]{});
			method.setAccessible(true);
			method.invoke(myTestClass, new Class[]{});
                        Field aInt = myClass.getDeclaredField("aInt");
			aInt.setAccessible(true);
			Log.i(TAG,"aInt.getName:"+aInt.getName());
			Log.i(TAG,"aInt.get(myTestClass):"+ aInt.get(myTestClass));
			
			aInt.setInt(myTestClass, 1);
			Log.i(TAG,"aInt.get(myTestClass):"+ aInt.get(myTestClass));
                        Field bBoolean = myClass.getDeclaredField("bBoolean");
			bBoolean.setAccessible(true);
			Log.i(TAG,"bBoolean.getName:"+bBoolean.getName());
			Log.i(TAG,"bBoolean.get(myTestClass):"+ bBoolean.get(myTestClass));
			bBoolean.setBoolean(myTestClass, true);
			Log.i(TAG,"bBoolean.get(myTestClass):"+ bBoolean.get(myTestClass));		
                        Field sString = myClass.getDeclaredField("sString");
			sString.setAccessible(true);
			Log.i(TAG,"sString.getName:"+sString.getName());
			Log.i(TAG,"sString.get(myTestClass):"+ sString.get(myTestClass));
			sString.set(myTestClass, "set_String");
			Log.i(TAG,"sString.get(myTestClass):"+ sString.get(myTestClass));
                        method = myClass.getDeclaredMethod("getVariable", new Class[]{});
			method.setAccessible(true);
			method.invoke(myTestClass, new Class[]{});		

			method = myClass.getDeclaredMethod("setInt", new Class[]{int.class});
			method.setAccessible(true);
			method.invoke(myTestClass, 2);	
			
			method = myClass.getDeclaredMethod("setBoolean", new Class[]{boolean.class});
			method.setAccessible(true);
			method.invoke(myTestClass, false);
                        method = myClass.getDeclaredMethod("setString", new Class[]{String.class});
			method.setAccessible(true);
			method.invoke(myTestClass, "set_method");
			
			method = myClass.getDeclaredMethod("getVariable", new Class[]{});
			method.setAccessible(true);
			method.invoke(myTestClass, new Class[]{});		
                        constructor = myClass.getDeclaredConstructor(new Class[]{int.class,boolean.class,String.class});
			constructor.setAccessible(true);
			myTestClass = (MyTestClass) constructor.newInstance(100,false,"test_constructor");
			
			method = myClass.getDeclaredMethod("getVariable", new Class[]{});
			method.setAccessible(true);
			method.invoke(myTestClass, new Class[]{});	
                  } catch (Exception e) {
			// TODO: handle exception
			Log.i(TAG,e.toString());
		}
     }
}

3.输出日志:

01-01 10:16:56.259: I/MyTestClass(9174): MyTestClass()
01-01 10:16:56.260: I/MyTestClass(9174): getVariable()
01-01 10:16:56.260: I/MyTestClass(9174): aInt:0---bBoolean:false----sString:test
01-01 10:16:56.260: I/MyTestClass(9174): aInt.getName:aInt
01-01 10:16:56.260: I/MyTestClass(9174): aInt.get(myTestClass):0

01-01 10:16:56.260: I/MyTestClass(9174): aInt.get(myTestClass):1
01-01 10:16:56.261: I/MyTestClass(9174): bBoolean.getName:bBoolean
01-01 10:16:56.261: I/MyTestClass(9174): bBoolean.get(myTestClass):false
01-01 10:16:56.264: I/MyTestClass(9174): bBoolean.get(myTestClass):true
01-01 10:16:56.264: I/MyTestClass(9174): sString.getName:sString
01-01 10:16:56.264: I/MyTestClass(9174): sString.get(myTestClass):test

01-01 10:16:56.265: I/MyTestClass(9174): sString.get(myTestClass):set_String
01-01 10:16:56.265: I/MyTestClass(9174): getVariable()
01-01 10:16:56.268: I/MyTestClass(9174): aInt:1---bBoolean:true----sString:set_String
01-01 10:16:56.268: I/MyTestClass(9174): setInt(int aInt)
01-01 10:16:56.273: I/MyTestClass(9174): setBoolean(boolean bBoolean)
01-01 10:16:56.273: I/MyTestClass(9174): setString(String sString)

01-01 10:16:56.276: I/MyTestClass(9174): getVariable()
01-01 10:16:56.276: I/MyTestClass(9174): aInt:2---bBoolean:false----sString:set_method
01-01 10:16:56.276: I/MyTestClass(9174): MyTestClass(int aInt,boolean bBoolean,String sString)
01-01 10:16:56.276: I/MyTestClass(9174): getVariable()
01-01 10:16:56.276: I/MyTestClass(9174): aInt:100---bBoolean:false----sString:test_constructor



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值