2016-12-20 90 views

回答

1

基本上,你必须迭代所有字段,获取每个字段类型的所有超类和接口,并检查你需要的类型。

package hello; 

import javassist.ClassPool; 
import javassist.CtClass; 
import javassist.CtField; 
import javassist.NotFoundException; 

import java.util.*; 
import java.util.stream.Collectors; 

class Test { 
    public ArrayList arrayList; 
    public List list; 
    public HashSet hashSet; 
    public Set set; 
    public HashMap hashMap; 
    public Map map; 
    public Object object; 
} 

class Main { 
    public static void main(String[] args) throws Exception { 
     CtClass testClass = ClassPool.getDefault().get("hello.Test"); 

     for (CtField ctField : testClass.getFields()) { 
      CtClass type = ctField.getType(); 

      Set<String> allSupper = getAllSuperclasses(type) 
        .stream() 
        .map(CtClass::getName) 
        .collect(Collectors.toSet()); 

      if (allSupper.contains(Map.class.getCanonicalName())){ 
       System.out.format("field %s is a Map\n", ctField.getName()); 
      } 

      if (allSupper.contains(Collection.class.getCanonicalName())){ 
       System.out.format("field %s is a Collection\n", ctField.getName()); 
      } 
     } 
    } 

    private static Set<CtClass> getAllSuperclasses(CtClass ctClass) throws NotFoundException { 
     HashSet<CtClass> ctClasses = new HashSet<>(); 

     while (ctClass != null){ 
      ctClasses.add(ctClass); 
      CtClass[] interfaces = ctClass.getInterfaces(); 
      Collections.addAll(ctClasses, interfaces); 
      ctClass = ctClass.getSuperclass(); 
     } 

     return ctClasses; 
    } 
} 

将打印

field arrayList is a Collection 
field list is a Collection 
field hashSet is a Collection 
field set is a Collection 
field hashMap is a Map 
field map is a Map 
+0

它的工作原理!,谢谢! – Gradle

相关问题