KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > transmogrify > ClassDef


1
2 // Transmogrify License
3
//
4
// Copyright (c) 2001, ThoughtWorks, Inc.
5
// All rights reserved.
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions
8
// are met:
9
// - Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// - Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
14
// Neither the name of the ThoughtWorks, Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from this
16
// software without specific prior written permission.
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.SortedSet JavaDoc;
39 import java.util.TreeSet JavaDoc;
40 import java.util.Vector JavaDoc;
41
42
43
44 /**
45  * <code>ClassDef</code> contains all the information needed to
46  * represent a java class or interface. This includes the superclass,
47  * whether it's a class or an interface, the interfaces it implements,
48  * a list of its (direct?) subclasses, and the classes that implement
49  * it if it is an interface
50  *
51  * @see Scope
52  */

53 public class ClassDef extends DefaultScope implements IClass {
54     private long id = 0;
55
56     private IClass superclass = null;
57     private List JavaDoc interfaces = new Vector JavaDoc();
58
59     private List JavaDoc subclasses = new Vector JavaDoc();
60     private List JavaDoc implementors = new Vector JavaDoc();
61
62     private Set JavaDoc importedPackages = new HashSet JavaDoc();
63
64     // variable definitions will use elements from parent
65
private Set JavaDoc methods = new HashSet JavaDoc();
66
67     private Hashtable JavaDoc imports = new Hashtable JavaDoc();
68     private Vector JavaDoc unprocessedImports = null;
69
70     protected MethodDef _defaultConstructor;
71
72     public ClassDef(String JavaDoc 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 JavaDoc aImports) {
91         unprocessedImports = (Vector JavaDoc) (aImports.clone());
92     }
93
94     public Vector JavaDoc 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     // begin definitions interface
107

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 JavaDoc getDefinitions() {
116         Vector JavaDoc allElements = new Vector JavaDoc();
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 JavaDoc 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 JavaDoc 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         //TODO: check for a class in the same package?
148
if (result == null) {
149             final String JavaDoc packageName = getParentScope().getQualifiedName();
150             final String JavaDoc fullName = packageName + "." + name;
151             Class JavaDoc theClass = null;
152             try {
153                 theClass = ClassManager.getClassLoader().loadClass(fullName);
154                 result = new ExternalClass(theClass);
155             }
156             catch (ClassNotFoundException JavaDoc e) {
157                 // no-op
158
}
159         }
160          
161         return result;
162     }
163
164     public IMethod getMethodDefinition(String JavaDoc 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         // not sure why this is here -- inner classes, maybe?
189
// regardless, write better
190
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 JavaDoc name, ISignature signature) {
200         IMethod result = null;
201
202         SortedSet JavaDoc compatibleMethods =
203             new TreeSet JavaDoc(new MethodSpecificityComparator());
204
205         Iterator JavaDoc 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 JavaDoc name, ISignature signature) {
223         // finds methods declared by this class with the given signature
224

225         IMethod result = null;
226
227         Iterator JavaDoc 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 JavaDoc name) {
242         IVariable result = null;
243
244         // in keeping with getField in java.lang.Class
245
// 1) current class
246
// 2) direct superinterfaces
247
// 3) superclass
248
// then we do the parent scope in case its an inner class
249

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     // end definitions interface
277

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 JavaDoc getSubclasses() {
296         return subclasses;
297     }
298
299     public void addImplementor(ClassDef implementor) {
300         implementors.add(implementor);
301     }
302
303     public List JavaDoc getImplementors() {
304         return implementors;
305     }
306
307     public IClass[] getInnerClasses() {
308         Iterator JavaDoc it = getClasses();
309         List JavaDoc result = new ArrayList JavaDoc();
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         // justify my existence
320
boolean result = subclasses.contains(possibleChild);
321
322         /*
323         Iterator it = subclasses.iterator();
324         while (it.hasNext() && !result) {
325           IClass child = (IClass)it.next();
326           result = child.isSuperclassOf(possibleChild);
327         }
328         */

329         return result;
330     }
331
332     public boolean isCompatibleWith(IClass type) {
333         boolean result = false;
334
335         // check myself
336
if (type.equals(this)) {
337             result = true;
338         }
339         // check my superclass
340
else if (superclass != null && superclass.isCompatibleWith(type)) {
341             result = true;
342         }
343         // check my interfaces
344
else if (!interfaces.isEmpty()) {
345             Iterator JavaDoc 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