View Javadoc

    1   package net.sourceforge.pmd.typeresolution;
    2
    3   import java.util.Arrays;
    4   import java.util.List;
    5
    6   import net.sourceforge.pmd.ast.SimpleNode;
    7   import net.sourceforge.pmd.ast.TypeNode;
    8   import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
    9
    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 && (clazz.getSimpleName().equals(vnd.getTypeImage()) || clazz.getName().equals(vnd.getTypeImage()))));
    19      }
    20
    21      public static boolean subclasses(TypeNode n, Class clazz) {
    22          Class type = n.getType();
    23          if (type == null) {
    24              return (clazz.getSimpleName().equals(((SimpleNode) n).getImage()) || clazz.getName().equals(((SimpleNode) n).getImage()));
    25          }
    26
    27          if (type.equals(clazz)) {
    28              return Boolean.TRUE;
    29          }
    30
    31          List<Class> implementors = Arrays.asList(type.getInterfaces());
    32          if (implementors.contains(clazz)) {
    33              return Boolean.TRUE;
    34          }
    35          Class superC = type.getSuperclass();
    36          while (superC != null && !superC.equals(Object.class)) {
    37              if (superC.equals(clazz)) {
    38                  return Boolean.TRUE;
    39              }
    40              superC = superC.getSuperclass();
    41          }
    42          return Boolean.FALSE;
    43      }
    44  }