1 2 29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify; 30 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.HashSet ; 34 import java.util.Hashtable ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Set ; 38 import java.util.SortedSet ; 39 import java.util.TreeSet ; 40 import java.util.Vector ; 41 42 43 44 53 public class ClassDef extends DefaultScope implements IClass { 54 private long id = 0; 55 56 private IClass superclass = null; 57 private List interfaces = new Vector (); 58 59 private List subclasses = new Vector (); 60 private List implementors = new Vector (); 61 62 private Set importedPackages = new HashSet (); 63 64 private Set methods = new HashSet (); 66 67 private Hashtable imports = new Hashtable (); 68 private Vector unprocessedImports = null; 69 70 protected MethodDef _defaultConstructor; 71 72 public ClassDef(String name, Scope parentScope, SymTabAST node) { 73 super(name, parentScope, node); 74 _defaultConstructor = new DefaultConstructor(this); 75 addDefinition(_defaultConstructor); 76 } 77 78 public long getNextAnonymousId() { 79 return ++id; 80 } 81 82 public void setSuperclass(IClass newSuperclass) { 83 this.superclass = newSuperclass; 84 } 85 86 public IClass getSuperclass() { 87 return superclass; 88 } 89 90 public void addUnprocessedImports(Vector aImports) { 91 unprocessedImports = (Vector ) (aImports.clone()); 92 } 93 94 public Vector getUnprocessedImports() { 95 return unprocessedImports; 96 } 97 98 public void importPackage(IPackage pkg) { 99 importedPackages.add(pkg); 100 } 101 102 public void importClass(IClass imported) { 103 imports.put(imported.getName(), imported); 104 } 105 106 108 public void addDefinition(MethodDef method) { 109 if (method.getName().equals(getName())) { 110 methods.remove(_defaultConstructor); 111 } 112 methods.add(method); 113 } 114 115 protected Enumeration getDefinitions() { 116 Vector allElements = new Vector (); 117 118 allElements.addAll(elements.values()); 119 allElements.addAll(methods); 120 allElements.addAll(labels.values()); 121 allElements.addAll(classes.values()); 122 123 return allElements.elements(); 124 } 125 126 public IClass getClassDefinition(String name) { 127 IClass result = null; 128 129 result = (ClassDef) (classes.get(name)); 130 131 if (result == null) { 132 result = (IClass) (imports.get(name)); 133 } 134 135 if (result == null) { 136 Iterator it = importedPackages.iterator(); 137 while (it.hasNext() && result == null) { 138 IPackage pkg = (IPackage) it.next(); 139 result = pkg.getClass(name); 140 } 141 } 142 143 if (result == null) { 144 result = getParentScope().getClassDefinition(name); 145 } 146 147 if (result == null) { 149 final String packageName = getParentScope().getQualifiedName(); 150 final String fullName = packageName + "." + name; 151 Class theClass = null; 152 try { 153 theClass = ClassManager.getClassLoader().loadClass(fullName); 154 result = new ExternalClass(theClass); 155 } 156 catch (ClassNotFoundException e) { 157 } 159 } 160 161 return result; 162 } 163 164 public IMethod getMethodDefinition(String name, ISignature signature) { 165 IMethod result = null; 166 167 result = getDeclaredMethod(name, signature); 168 169 if (result == null) { 170 result = getMostCompatibleMethod(name, signature); 171 } 172 173 if (result == null) { 174 if (superclass != null) { 175 result = superclass.getMethodDefinition(name, signature); 176 } 177 } 178 179 if (result == null) { 180 IClass[] myInterfaces = getInterfaces(); 181 for (int index = 0; 182 index < myInterfaces.length && result == null; 183 index++) { 184 result = myInterfaces[index].getMethodDefinition(name, signature); 185 } 186 } 187 188 if (result == null) { 191 if (getParentScope() != null) { 192 result = getParentScope().getMethodDefinition(name, signature); 193 } 194 } 195 196 return result; 197 } 198 199 public IMethod getMostCompatibleMethod(String name, ISignature signature) { 200 IMethod result = null; 201 202 SortedSet compatibleMethods = 203 new TreeSet (new MethodSpecificityComparator()); 204 205 Iterator it = methods.iterator(); 206 while (it.hasNext()) { 207 MethodDef method = (MethodDef) it.next(); 208 if (name.equals(method.getName())) { 209 if (method.hasCompatibleSignature(signature)) { 210 compatibleMethods.add(method); 211 } 212 } 213 } 214 215 if (!compatibleMethods.isEmpty()) { 216 result = (IMethod) compatibleMethods.first(); 217 } 218 219 return result; 220 } 221 222 public IMethod getDeclaredMethod(String name, ISignature signature) { 223 225 IMethod result = null; 226 227 Iterator it = methods.iterator(); 228 while (it.hasNext()) { 229 MethodDef method = (MethodDef) it.next(); 230 if (name.equals(method.getName())) { 231 if (method.hasSameSignature(signature)) { 232 result = method; 233 break; 234 } 235 } 236 } 237 238 return result; 239 } 240 241 public IVariable getVariableDefinition(String name) { 242 IVariable result = null; 243 244 250 result = (VariableDef) (elements.get(name)); 251 252 if (result == null) { 253 IClass[] superinterfaces = getInterfaces(); 254 for (int i = 0; 255 i < superinterfaces.length && result == null; 256 i++) { 257 result = superinterfaces[i].getVariableDefinition(name); 258 } 259 } 260 261 if (result == null) { 262 if (superclass != null) { 263 result = superclass.getVariableDefinition(name); 264 } 265 } 266 267 if (result == null) { 268 if (getParentScope() != null) { 269 result = getParentScope().getVariableDefinition(name); 270 } 271 } 272 273 return result; 274 } 275 276 278 public void addInterface(IClass implemented) { 279 interfaces.add(implemented); 280 } 281 282 public IClass[] getInterfaces() { 283 IClass[] type = new IClass[0]; 284 return (IClass[]) interfaces.toArray(type); 285 } 286 287 public ClassDef getEnclosingClass() { 288 return this; 289 } 290 291 public void addSubclass(ClassDef subclass) { 292 subclasses.add(subclass); 293 } 294 295 public List getSubclasses() { 296 return subclasses; 297 } 298 299 public void addImplementor(ClassDef implementor) { 300 implementors.add(implementor); 301 } 302 303 public List getImplementors() { 304 return implementors; 305 } 306 307 public IClass[] getInnerClasses() { 308 Iterator it = getClasses(); 309 List result = new ArrayList (); 310 311 while (it.hasNext()) { 312 result.add(it.next()); 313 } 314 315 return (IClass[]) result.toArray(new IClass[0]); 316 } 317 318 public boolean isSuperclassOf(IClass possibleChild) { 319 boolean result = subclasses.contains(possibleChild); 321 322 329 return result; 330 } 331 332 public boolean isCompatibleWith(IClass type) { 333 boolean result = false; 334 335 if (type.equals(this)) { 337 result = true; 338 } 339 else if (superclass != null && superclass.isCompatibleWith(type)) { 341 result = true; 342 } 343 else if (!interfaces.isEmpty()) { 345 Iterator it = interfaces.iterator(); 346 347 while (it.hasNext() && !result) { 348 IClass current = (IClass) it.next(); 349 350 if (current.isCompatibleWith(type)) { 351 result = true; 352 } 353 } 354 } 355 356 return result; 357 } 358 359 public boolean isPrimitive() { 360 return false; 361 } 362 } 363 | Popular Tags |