在项目中需要用到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