01 package net.sourceforge.pmd.lang.java.typeresolution;
02
03 import java.util.Arrays;
04 import java.util.List;
05
06 import net.sourceforge.pmd.lang.ast.Node;
07 import net.sourceforge.pmd.lang.java.ast.TypeNode;
08 import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
09
10 public class TypeHelper {
11
12 public static boolean isA(TypeNode n, Class<?> clazz) {
13 return subclasses(n, clazz);
14 }
15
16 public static boolean isA(VariableNameDeclaration vnd, Class<?> clazz) {
17 Class<?> type = vnd.getType();
18 return type != null && type.equals(clazz) || type == null
19 && (clazz.getSimpleName().equals(vnd.getTypeImage()) || clazz.getName().equals(vnd.getTypeImage()));
20 }
21
22 public static boolean subclasses(TypeNode n, Class<?> clazz) {
23 Class type = n.getType();
24 if (type == null) {
25 return clazz.getSimpleName().equals(((Node) n).getImage()) || clazz.getName().equals(((Node) n).getImage());
26 }
27
28 if (type.equals(clazz)) {
29 return Boolean.TRUE;
30 }
31
32 List<Class> implementors = Arrays.asList(type.getInterfaces());
33 if (implementors.contains(clazz)) {
34 return Boolean.TRUE;
35 }
36 Class<?> superC = type.getSuperclass();
37 while (superC != null && !superC.equals(Object.class)) {
38 if (superC.equals(clazz)) {
39 return Boolean.TRUE;
40 }
41 superC = superC.getSuperclass();
42 }
43 return Boolean.FALSE;
44 }
45 }
|