KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
32 import java.util.SortedSet JavaDoc;
33 import java.util.TreeSet JavaDoc;
34
35
36
37
38 /**
39  * <code>Definition</code> contains basic information for everything
40  * that is defined in a java source file. A definition has a list
41  * of <code>Reference</code>s, an <code>Occurrence</code>, a name, and
42  * a parent <code>Scope</code>.
43  *
44  * @see Occurrence
45  * @see Scope
46  * @see Reference
47  */

48
49 public abstract class Definition implements IDefinition, Comparable JavaDoc {
50   private String JavaDoc _name = null;
51   private Scope _parentScope = null;
52   private SymTabAST _node = null;
53   private SortedSet JavaDoc _references;
54
55   private Occurrence _occurrence = null;
56
57   public Definition(String JavaDoc name, Scope parentScope, SymTabAST node) {
58     _name = name;
59     _parentScope = parentScope;
60     _node = node;
61     _references = new TreeSet JavaDoc();
62
63     if ( node != null ) {
64       _occurrence = new Occurrence( _node.getFile(),
65                                     ASTUtil.getLine( _node ),
66                                     ASTUtil.getColumn( _node ));
67     }
68   }
69
70   public boolean isSourced() {
71     return true;
72   }
73
74   /**
75    * returns the name of the definition
76    *
77    * @return String
78    */

79
80   public String JavaDoc getName() {
81     return _name;
82   }
83
84   /**
85    * returns the node in the AST that represents this definition
86    *
87    * @return the node in the AST that represents this definition
88    */

89   public SymTabAST getTreeNode() {
90     return _node;
91   }
92
93   /**
94    * Adds a <code>Reference</code> to the collection of <code>Reference</code>s
95    *
96    * @param reference the <code>Reference</code> to add
97    */

98   public void addReference( Reference reference ) {
99     _references.add( reference );
100   }
101
102   /**
103    * Returns the <code>Reference</code>s to this definition
104    *
105    * @return the <code>Reference</code>s to this definition
106    */

107   public Iterator JavaDoc getReferences() {
108     return _references.iterator();
109   }
110
111   public int getNumReferences() {
112     return _references.size();
113   }
114
115   /**
116    * Returns the scope in which this definition is defined
117    *
118    * @return the scope in which this definition is defined
119    */

120   public Scope getParentScope() {
121     return _parentScope;
122   }
123
124   /**
125    * returns the fully qualifed name of this defintion. The name of
126    * the parentScope and all of its parents are considered when constructing
127    * this name.
128    *
129    * @return the fully qualified name of this definition
130    */

131   public String JavaDoc getQualifiedName() {
132     String JavaDoc nameToUse = _name;
133     String JavaDoc result;
134
135     if (_name == null) {
136       nameToUse = "~NO NAME~";
137     }
138
139     if (getParentScope() != null &&
140          !(getParentScope() instanceof BaseScope)) {
141       result = getParentScope().getQualifiedName() + "." + nameToUse;
142     }
143     else {
144       result = nameToUse;
145     }
146     return result;
147   }
148
149   /**
150    * Returns the <code>Occurrence</code> for the location of this definition
151    *
152    * @return the <code>Occurrence</code> for the location of this definition
153    */

154   public Occurrence getOccurrence() {
155     return _occurrence;
156   }
157
158   /**
159    * Returns the <code>ClassDef</code> that this scope is contained in.
160    *
161    * @return the <code>ClassDef</code> this definition is contained in
162    */

163   public ClassDef getEnclosingClass() {
164     ClassDef result = null;
165
166     if ( getParentScope() != null ) {
167       result = getParentScope().getEnclosingClass();
168     }
169
170     return result;
171   }
172
173   public IPackage getEnclosingPackage() {
174     IPackage result = null;
175     if (getParentScope() != null) {
176       result = getParentScope().getEnclosingPackage();
177     }
178
179     return result;
180   }
181
182   /**
183    * returns a String representation of the definition. This string includes
184    * the class of the defintion and its qualified name
185    *
186    * @return String
187    */

188   public String JavaDoc toString() {
189     return getClass().getName() + "[" + getQualifiedName() + "]";
190   }
191
192   public int compareTo(Object JavaDoc o) {
193     int result = 0;
194
195     if (!(o instanceof Definition)) {
196       throw new ClassCastException JavaDoc(o.getClass().getName());
197     }
198     result = getQualifiedName().compareTo(((Definition)o).getQualifiedName());
199
200     return result;
201   }
202
203 }
204
Popular Tags