尚学堂 213_尚学堂_高淇_java300集最全视频教程_反射机制_提高反射效率_操作泛型_操作注解_合并文件.mp4

在反射的时候如果去掉了安全性检测机制,能够大大的提高反射的执行效率,我们来看下面的代码进行比较

package com.bjsxt.test;

import java.lang.reflect.Method;

import com.bjsxt.test.bean.User;

/**
 * 通过跳过安全检查,提高反射效率
 * 三种执行方法的效率差异比较
 * 
 * @author 尚学堂高淇 www.sxt.cn
 *
 */
public class Demo06 {
    
    public static void test01(){
        User u = new User();
        
        long startTime = System.currentTimeMillis();
        
        for (int i = 0; i < 1000000000L; i++) {
            u.getUname();
        }
        
        long endTime = System.currentTimeMillis();
        System.out.println("普通方法调用,执行10亿次,耗时:"+(endTime-startTime)+"ms"); 
    }
    
    public static void test02() throws Exception{
        User u = new User();
        Class clazz = u.getClass();
        Method m = clazz.getDeclaredMethod("getUname", null);
//        m.setAccessible(true);
        
        long startTime = System.currentTimeMillis();
        
        for (int i = 0; i < 1000000000L; i++) {
            m.invoke(u, null);
        }
        
        long endTime = System.currentTimeMillis();
        System.out.println("反射动态方法调用,执行10亿次,耗时:"+(endTime-startTime)+"ms");
    }
    
    public static void test03() throws Exception{
        User u = new User();
        Class clazz = u.getClass();
        Method m = clazz.getDeclaredMethod("getUname", null);
        m.setAccessible(true);    //不需要执行访问安全检查
        
        long startTime = System.currentTimeMillis();
        
        for (int i = 0; i < 1000000000L; i++) {
            m.invoke(u, null);
        }
        
        long endTime = System.currentTimeMillis();
        System.out.println("反射动态方法调用,跳过安全检查,执行10亿次,耗时:"+(endTime-startTime)+"ms");
    }
    
    
    public static void main(String[] args) throws Exception {
        test01();
        test02();
        test03();
    }
}

程序运行的代码:

普通方法调用,执行10亿次,耗时:315ms
反射动态方法调用,执行10亿次,耗时:2026ms
反射动态方法调用,跳过安全检查,执行10亿次,耗时:1825ms

我们使用了 m.setAccessible(true);    //不需要执行访问安全检查

大大的提高了程序的运行时间

 即下来我们在反射中操作泛型

 

读取输入参数的泛型

package com.bjsxt.test;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.bjsxt.test.bean.User;

/**
 * 通过反射获取泛型信息
 * @author dell
 *
 */
public class Demo04 {
    
    public void test01(Map<String,User> map,List<User> list){
        System.out.println("Demo04.test01()");
    }
    
    public Map<Integer,User> test02(){
        System.out.println("Demo04.test02()");
        return null;
    }
    
    public static void main(String[] args) {

        try {
            
            //获得指定方法参数泛型信息
            Method m = Demo04.class.getMethod("test01", Map.class,List.class);
            Type[] t = m.getGenericParameterTypes();
            for (Type paramType : t) {
                System.out.println("#"+paramType);
                if(paramType instanceof ParameterizedType){//强制转换成泛型的参数类型
                    Type[] genericTypes = ((ParameterizedType) paramType).getActualTypeArguments();//获得真正的数据
                    for (Type genericType : genericTypes) {
                        System.out.println("泛型类型1:"+genericType);
                    }
                }
            }
            
            //获得指定方法返回值泛型信息,参数的返回值只能1
            Method m2 = Demo04.class.getMethod("test02", null);
            Type returnType = m2.getGenericReturnType();
            if(returnType instanceof ParameterizedType){
                    Type[] genericTypes = ((ParameterizedType) returnType).getActualTypeArguments();

                    for (Type genericType : genericTypes) {
                        System.out.println("返回值,泛型类型:"+genericType);
                    }
                    
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        
    
    }
}

 

程序运行的结果是:

#java.util.Map<java.lang.String, com.bjsxt.test.bean.User>
泛型类型1:class java.lang.String
泛型类型1:class com.bjsxt.test.bean.User
#java.util.List<com.bjsxt.test.bean.User>
泛型类型1:class com.bjsxt.test.bean.User
返回值,泛型类型:class java.lang.Integer
返回值,泛型类型:class com.bjsxt.test.bean.User

 

posted on 2017-08-02 18:32  luzhouxiaoshuai  阅读(405)  评论(0编辑  收藏  举报

导航