View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.rule;
5   
6   import java.lang.reflect.Field;
7   import java.lang.reflect.Method;
8   import java.util.HashSet;
9   import java.util.Set;
10  
11  import net.sourceforge.pmd.lang.ast.Node;
12  
13  public class ImportWrapper {
14      private Node node;
15      private String name;
16      private String fullname;
17      private boolean isStaticDemand;
18      private Set<String> allDemands = new HashSet<>();
19  
20      public ImportWrapper(String fullname, String name) {
21          this(fullname, name, null);
22      }
23  
24      public ImportWrapper(String fullname, String name, Node node) {
25          this(fullname, name, node, false);
26      }
27  
28      public ImportWrapper(String fullname, String name, Node node, Class<?> type, boolean isStaticDemand) {
29          this(fullname, name, node, isStaticDemand);
30          if (type != null) {
31              for (Method m : type.getMethods()) {
32                  allDemands.add(m.getName());
33              }
34              for (Field f : type.getFields()) {
35                  allDemands.add(f.getName());
36              }
37          }
38      }
39  
40      public ImportWrapper(String fullname, String name, Node node, boolean isStaticDemand) {
41          this.fullname = fullname;
42          this.name = name;
43          this.node = node;
44          this.isStaticDemand = isStaticDemand;
45      }
46  
47      public boolean equals(Object other) {
48      	if (other == null) {
49      	    return false;
50      	}
51      	if (other == this) {
52      	    return true;
53      	}
54      	if (other instanceof ImportWrapper) {
55  	        ImportWrapper i = (ImportWrapper) other;
56  	        if (name == null && i.getName() == null) {
57  	            return i.getFullName().equals(fullname);
58  	        }
59  	        return i.getName().equals(name);
60      	}
61      	return false;
62      }
63  
64      public boolean matches(ImportWrapper i) {
65          if (isStaticDemand) {
66              if (allDemands.contains(i.fullname)) {
67                  return true;
68              }
69          }
70          if (name == null && i.getName() == null) {
71              return i.getFullName().equals(fullname);
72          }
73          return i.getName().equals(name);
74      }
75  
76      public int hashCode() {
77          if(name == null){
78              return fullname.hashCode();
79          }
80          return name.hashCode();
81      }
82  
83      public String getName() {
84          return name;
85      }
86  
87      public String getFullName() {
88          return fullname;
89      }
90  
91      public Node getNode() {
92          return node;
93      }
94  
95      public boolean isStaticOnDemand() {
96          return isStaticDemand;
97      }
98  
99      @Override
100     public String toString() {
101         return "Import[name=" + name + ",fullname=" + fullname + ",static*=" + isStaticDemand + "]";
102     }
103 }
104