java里的反射如下:
一。写个类
public class Simple {
private void displayMessage(String strMsg) {
System.out.println("message is:" + strMsg);
}
}
二。反射调用
import java.lang.reflect.*;
public class Test {
/**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
Class simpleClass = Class.forName("Simple");
Object simpelObject = simpleClass.newInstance();
Class[] args1 = new Class[1];
args1[0] = String.class;
Method simpleMethod = simpleClass.getDeclaredMethod("displayMessage",
args1);
simpleMethod.setAccessible(true);
String[] argments = new String[1];
argments[0] = "Hello,world";
simpleMethod.invoke(simpelObject, argments);
}
}
Class<?> smsManager = Class.forName("android.telephony.SmsManager" );
Object obj_smsManager=smsManager.getClass(). forName("android.telephony.SmsManager").getMethod("getDefault",null).invoke(null,null);
((SmsManager) obj_smsManager).sendTextMessage("***",null,"test",null,null);