java使用反射從外部更改方法的執(zhí)行順序
/**
* 使用反射從外部更改方法的執(zhí)行順序
*/
public class TestReflection2 {
? ?public void print1(){
? ? ? ?System.out.println("TestReflection2.print1");
? ?}
? ?public void print2(){
? ? ? ?System.out.println("TestReflection2.print2");
? ?}
? ?public void print3(){
? ? ? ?System.out.println("TestReflection2.print3");
? ?}
}
class Change2{
? ?public static void main(String[] args) {
? ? ? ?Method[] methods = TestReflection2.class.getDeclaredMethods();
? ? ? ?TestReflection2 tr = null;
? ? ? ?try {
? ? ? ? ? ?tr = TestReflection2.class.newInstance();
? ? ? ?} catch (InstantiationException | IllegalAccessException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?if (args != null&&args.length>0) {
? ? ? ? ? ?//利用program arguments從外部傳參,根據(jù)參數(shù)改變方法執(zhí)行順序
? ? ? ? ? ?for (String arg :
? ? ? ? ? ? ? ? ? ?args) {
? ? ? ? ? ? ? ?for (Method method :
? ? ? ? ? ? ? ? ? ? ? ?methods) {
? ? ? ? ? ? ? ? ? ?if (arg.equalsIgnoreCase(method.getName())){
? ? ? ? ? ? ? ? ? ? ? ?//如果找到了名字匹配的方法則調(diào)用
? ? ? ? ? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ? ? ? ? ?method.invoke(tr);
? ? ? ? ? ? ? ? ? ? ? ?} catch (IllegalAccessException | InvocationTargetException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?}else {
? ? ? ? ? ?//正常執(zhí)行時(shí)默認(rèn)順序,需要更改時(shí)外部傳參
? ? ? ? ? ?tr.print1();
? ? ? ? ? ?tr.print2();
? ? ? ? ? ?tr.print3();
? ? ? ?}
? ? ? ?//在idea中對(duì)Change2類點(diǎn)擊Run菜單--Edit Configurations,在Change2的Program arguments框內(nèi)輸入:print1 print3 print2 ? ? 參數(shù)之間用空格分開(kāi)
? ? ? ?//再次運(yùn)行時(shí)結(jié)果為:
? ? ? ?//TestReflection2.print1
? ? ? ?//TestReflection2.print3
? ? ? ?//TestReflection2.print2
? ? ? ?//測(cè)試反射的效率
? ? ? ?try {
? ? ? ? ? ?TestReflection1 tr1 = (TestReflection1) Class.forName("TestReflection1").newInstance();
? ? ? ? ? ?//通過(guò)Class.forName(String)無(wú)法直接設(shè)定編譯類型為運(yùn)行時(shí)類型,需要強(qiáng)轉(zhuǎn)
? ? ? ? ? ?Method method = TestReflection1.class.getDeclaredMethod("setAge",int.class);
? ? ? ? ? ?long time = System.currentTimeMillis();
? ? ? ? ? ?for (int i = 0 ;i<100000000;i++){
? ? ? ? ? ? ? ?method.invoke(tr1,i);
? ? ? ? ? ?}
? ? ? ? ? ?System.out.println("反射耗時(shí)"+(System.currentTimeMillis()-time));
? ? ? ?} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException |
? ? ? ? ? ? ? ? InvocationTargetException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ? ? ?TestReflection1 tr1 = new TestReflection1();
? ? ? ?long time = System.currentTimeMillis();
? ? ? ?for (int i = 0 ;i<100000000;i++){
? ? ? ? ? ?tr1.setAge(i);
? ? ? ?}
? ? ? ?System.out.println("正常耗時(shí)"+(System.currentTimeMillis()-time));
? ? ? ?//結(jié)果:
? ? ? ?//反射耗時(shí)401
? ? ? ?//正常耗時(shí)7
? ? ? ?try {
? ? ? ? ? ?//關(guān)閉安全檢查以提高反射效率
? ? ? ? ? ?TestReflection1 tr2 = (TestReflection1) Class.forName("TestReflection1").newInstance();
? ? ? ? ? ?Method method = TestReflection1.class.getDeclaredMethod("setAge",int.class);
? ? ? ? ? ?method.setAccessible(true);
? ? ? ? ? ?long time2 = System.currentTimeMillis();
? ? ? ? ? ?for (int i = 0 ;i<100000000;i++){
? ? ? ? ? ? ? ?method.invoke(tr2,i);
? ? ? ? ? ?}
? ? ? ? ? ?System.out.println("關(guān)閉安全檢查反射耗時(shí)"+(System.currentTimeMillis()-time2));
? ? ? ? ? ?//結(jié)果為:關(guān)閉安全檢查反射耗時(shí)312
? ? ? ?} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException |
? ? ? ? ? ? ? ? InvocationTargetException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ?}
}