java反射机制的简单示例(android中)

在项目中需要用到java的反射机制,因此自己就结合着搜索到的资料做了个简单的java反射机制的示例,供以后参考使用。

先看用来反射的类:

package com.example.reflection_test;

public class nativetest {
	public int nativeint;
	public String nativestr;
	
	public nativetest(){
		nativeint = 0;
		nativestr = "novalue";
	}
	public nativetest(String str,int value){
		nativeint = value;
		nativestr = str;
		
	}
	
	private void setnativeint(int var){
		nativeint = var;
	}
	
	public void setnativestring(String var){
		nativestr = var;
	}
	
	private int getnativeint(){
		return nativeint;
	}
	
	public String getnativestring(){
		return nativestr;
	}
}

这个类很简单,用来充当应用程序使用反射机制时要反射的类。

下面是主应用程序类:

package com.example.reflection_test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	public TextView text1;
	public TextView text2;
	public static String TAG = "Reflection_test";
	Class<?> test1;
	Object object1;
	Object object2;	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		int intvar = 22;
		String strvar = "222222";
		
		try {
			test1 = Class.forName("com.example.reflection_test.nativetest");
			/* 对于无参的构造函数,可以用这种方法 */
			object1 = test1.newInstance();   //实例化对象
			/* 对于有参的构造函数,须用此方法 */
			Constructor ct = test1.getConstructor(String.class,Integer.TYPE);
			object2 = ct.newInstance(strvar,intvar);    //实例化对象
			printmethods(test1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/* object 1 */
		setnovalue();
		getnovalue();
		
		/* object 2 */
		getvalue();
		setvalue();
		getvalue();
	}

	/*object 1 */
	public void setnovalue(){
		int intvar = 1;
		String stringvar = "hahaha";
		try {
			Method methodint = test1.getDeclaredMethod("setnativeint", Integer.TYPE);
			Method methodstr = test1.getMethod("setnativestring", String.class);
			methodint.setAccessible(true); 			//抑制对访问控制的检测,不加上此句会因为访问受限制报错
			try {
				methodint.invoke(object1, intvar);
				methodstr.invoke(object1, stringvar);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/* object 2 */
	public void setvalue(){
		int intvar = 1;
		String stringvar = "hahaha";
		try {
			Method methodint = test1.getDeclaredMethod("setnativeint", Integer.TYPE);
			Method methodstr = test1.getMethod("setnativestring", String.class);
			methodint.setAccessible(true); 			//抑制对访问控制的检测
			try {
				methodint.invoke(object2, intvar);
				methodstr.invoke(object2, stringvar);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*object 2 */
	public void getvalue(){
		int varint = 0;
		String varstr = null;
		try {
			Method methodint = test1.getDeclaredMethod("getnativeint");
			Method methodstr = test1.getMethod("getnativestring");
			methodint.setAccessible(true); 			//抑制对访问控制的检测
			try {
				varint = (Integer) methodint.invoke(object2);
				varstr = (String) methodstr.invoke(object2);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Log.v(TAG, "getvalue===============get int:"+varint);
		Log.v(TAG, "getvalue===============get string:"+varstr);
	}
	/* object1*/
	public void getnovalue(){
		int varint = 0;
		String varstr = null;
		try {
			Method methodint = test1.getDeclaredMethod("getnativeint");
			Method methodstr = test1.getMethod("getnativestring");
			methodint.setAccessible(true); 			//抑制对访问控制的检测
			try {
				varint = (Integer) methodint.invoke(object1);
				varstr = (String) methodstr.invoke(object1);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Log.v(TAG, "getnovalue===============get int:"+varint);
		Log.v(TAG, "getnovalue===============get string:"+varstr);
	}
	/* get the methods from reflection class */
	public void printmethods(Object object){
		Method[] method= ((Class<?>) object).getMethods();
		Method[] method2= ((Class<?>) object).getDeclaredMethods();
		
		for(int i = 0;i < method.length;i++){
			Log.v(TAG, "==================method_"+i+":"+method[i].getName());
		}
		for(int i = 0;i < method2.length;i++){
			Log.v(TAG, "==================declaredmethod_"+i+":"+method2[i].getName());
		}
		Log.v(TAG,"over");
	}
}

这个程序实现很简单的功能,主要对要反射的类中的两个属性进行实例化、赋值和取值,涉及到对public方法和private方法的反射。

参考:

http://www.cyqdata.com/android/article-detail-36281

http://www.cnblogs.com/mengdd/archive/2013/01/26/2878136.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值