2014年11月25日 星期二

[ Java 常見問題 ] Check if a class is subclass of another class in Java

Source From Here
Question
I'm playing around with Java's reflection API and trying to handle some fields. Now I'm stuck with identifying the type of my fields. Strings are easy, just domyField.getType().equals(String.class). The same applies for other non-derived classes. But how do I check derived classes? E.g. LinkedList as subclass ofList. I can't find any isSubclassOf(...) or extends(...) method. Do I need to walk through all getSuperClass() and find my supeclass by my own?

How-To
You want this method:
  1. boolean isList = List.class.isAssignableFrom(myClass);  
From the JavaDoc:
boolean isAssignableFrom(Class cls):
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter cls.

Sample Code
Below groovy sample code will demonstrate how to check if you can assign one object to other class's object (Parent class can point to child class):
  1. class A {  
  2.     @Override  
  3.     public String toString(){return "A's object"}  
  4. }  
  5.   
  6. class B extends A{  
  7.     @Override  
  8.     public String toString(){return "B's object"}  
  9. }  
  10.   
  11. class C {  
  12.     @Override  
  13.     public String toString(){return "C's object"}  
  14. }  
  15.   
  16. objlist = []  
  17. objlist << A.class.newInstance()  
  18. objlist << B.class.newInstance()  
  19. objlist << C.class.newInstance()  
  20. objlist.each{ obj->  
  21.     if(B.class.isAssignableFrom(obj.class))  
  22.     {  
  23.         // obj can be assigned to B  
  24.         B o = (B)obj  
  25.         printf("I am %s assigned to B\n", o)  
  26.     }  
  27.     else if(A.class.isAssignableFrom(obj.class))  
  28.     {  
  29.         // obj can be assigned to A  
  30.         A o = (A)obj  
  31.         printf("I am %s assigned to A\n", o)  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("I am %s\n", obj)  
  36.     }  
  37. }  
Execution result:
I am A's object assigned to A
I am B's object assigned to B
I am C's object
If you switch the clause of A and B class to:
  1. ...  
  2.     if(A.class.isAssignableFrom(obj.class))  
  3.     {  
  4.         // obj can be assigned to A  
  5.         A o = (A)obj  
  6.         printf("I am %s assigned to A\n", o)  
  7.     }  
  8.     else if(B.class.isAssignableFrom(obj.class))  
  9.     {  
  10.         // obj can be assigned to B  
  11.         B o = (B)obj  
  12.         printf("I am %s assigned to B\n", o)  
  13.     }  
  14.     else  
  15.     {  
  16.         printf("I am %s\n", obj)  
  17.     }  
  18. ...  
The result will be different! (Because class A is parent of class B!)
I am A's object assigned to A
I am B's object assigned to
 A
I am C's object

This message was edited 7 times. Last update was at 26/11/2014 11:39:33

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...