zoukankan      html  css  js  c++  java
  • java 泛型 3 反射机制

    简介

    说实话,不太理解反射机制。
    不过好像应该是对于特定对于泛型的接口。

    code

    package com.company;
    
    import java.lang.reflect.*;
    import java.util.*;
    
    public class GenericReflectionTest {
        public static void main(String[] args){
            String name;
            if(args.length > 0) name = args[0];
            else{
                try(Scanner in = new Scanner(System.in)){
                    System.out.println("Enter class name (e.g. java.util.Collections)");
                    name = in.next();
                }
            }
            try{
                // print generic info for class and public methods
                Class<?> cl = Class.forName(name);
                printClass(cl);
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }
        }
        public static void printClass(Class<?> cl){
            System.out.print(cl);
            printTypes(cl.getTypeParameters(), "<", ",",">", true);
            Type sc = cl.getGenericSuperclass();
            if(sc != null){
                System.out.print(" extends ");
                printType(sc, false);
            }
            printTypes(cl.getGenericInterfaces(), " implements ", ",", "", false);
            System.out.println();
        }
    
        public static void printMethod(Method m){
            String name = m.getName();
            System.out.print(Modifier.toString(m.getModifiers()));
            System.out.print(" ");
            printTypes(m.getTypeParameters(), "<", ",", ">", true);
    
            printType(m.getGenericReturnType(), false);
            System.out.print(" ");
            System.out.print(name);
            System.out.print("(");
            printTypes(m.getGenericParameterTypes(), "", ", ", "", false);
            System.out.println(")");
        }
    
        public static void printTypes(Type[] types, String pre, String sep, String suf, boolean isDefinition){
            if(pre.equals(" extends ") && Arrays.equals(types, new Type[] {Object.class})) return;
            if(types.length > 0) System.out.print(pre);
            for(int i=0; i<types.length; i++){
                if(i > 0) System.out.print(sep);
                printType(types[i], isDefinition);
            }
            if(types.length > 0) System.out.print(suf);
        }
    
        public static void printType(Type type, boolean isDefinition){
            if(type instanceof Class) {
                Class<?> t = (Class<?>) type;
                System.out.print(t.getName());
            }else if(type instanceof TypeVariable){
                TypeVariable<?> t = (TypeVariable<?>) type;
                System.out.print(t.getName());
                if(isDefinition){
                    printTypes(t.getBounds(), " extends ", " & ", "", false);
                }
            }else if(type instanceof WildcardType){
                WildcardType t = (WildcardType) type;
                System.out.print("?");
                printTypes(t.getUpperBounds(), " extends ", " & ", "", false);
                printTypes(t.getLowerBounds(), " super ", " & ", "", false);
            }else if(type instanceof ParameterizedType){
                ParameterizedType t = (ParameterizedType) type;
                Type owner = t.getOwnerType();
                if(owner != null){
                    printType(owner, false);
                    System.out.print(".");
                }
                printType(t.getRawType(), false);
                printTypes(t.getActualTypeArguments(), "<", ", ", ">", false);
            }else if(type instanceof GenericArrayType){
                GenericArrayType t = (GenericArrayType) type;
                System.out.print("");
                printType(t.getGenericComponentType(), isDefinition);
                System.out.print("[]");
            }
        }
    
    }
    

    result

    java.util.Collections
    class java.util.Collections extends java.lang.Object
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    JavaScript 检测浏览器更多信息【每日一段代码66】
    JavaScript throw 声明【每日一段代码64】
    JavaScript 计时器2 【每日一段代码73】
    JavaScript 按钮动画【每日一段代码70】
    JavaScript 检测浏览器【每日一段代码67】
    JavaScript 使用计时事件制作的钟表 【每日一段代码76】
    一个实现恢复删除机制(do undo)的设计
    基于邻接表的广度优先搜索遍历
    HDU1045 Fire Net
    深度遍历
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13663989.html
Copyright © 2011-2022 走看看