001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.summary;
053:
054: import net.sourceforge.jrefactory.ast.Node;
055: import net.sourceforge.jrefactory.ast.ASTAssignmentOperator;
056: import net.sourceforge.jrefactory.ast.ASTExpression;
057: import net.sourceforge.jrefactory.ast.ASTName;
058: import net.sourceforge.jrefactory.ast.ASTStatementExpression;
059: import net.sourceforge.jrefactory.ast.SimpleNode;
060: import org.acm.seguin.summary.query.GetTypeSummary;
061:
062: /**
063: * Summarize a field access summary.
064: *
065: *@author Chris Seguin
066: *@author Achille Petrilli, mods to distinguish read from write access
067: *@created June 23, 1999
068: */
069: public class FieldAccessSummary extends Summary {
070: // Instance Variables
071: private String objectName;
072: private String packageName;
073: private String fieldName;
074: private boolean isAssignment;
075:
076: /**
077: * Creates a field access summary from an ASTName object.
078: *
079: *@param parentSummary the parent summary
080: *@param nameNode the ASTName object
081: */
082: public FieldAccessSummary(Summary parentSummary, ASTName nameNode) {
083: // Initialize the parent class
084: super (parentSummary);
085:
086: // Initialize the variables
087: fieldName = null;
088: objectName = null;
089: packageName = null;
090: isAssignment = checkAssignment(nameNode);
091:
092: // Local Variables
093: int numChildren = nameNode.getNameSize();
094:
095: // Determine the name of the message
096: fieldName = nameNode.getNamePart(numChildren - 1).intern();
097:
098: // Determine the name of the object (or class)
099: if (numChildren > 1) {
100: objectName = nameNode.getNamePart(numChildren - 2).intern();
101:
102: // Extract the package
103: if (numChildren > 2) {
104: StringBuffer buffer = new StringBuffer(nameNode
105: .getNamePart(0));
106: for (int ndx = 1; ndx < numChildren - 2; ndx++) {
107: buffer.append(".");
108: buffer.append(nameNode.getNamePart(ndx));
109: }
110: packageName = buffer.toString().intern();
111: }
112: }
113: }
114:
115: /**
116: * Gets the Assignment attribute of the FieldAccessSummary object
117: *
118: *@return The Assignment value
119: */
120: public boolean isAssignment() {
121: return isAssignment;
122: }
123:
124: /**
125: * Get the package name
126: *
127: *@return a string containing the name of the package
128: */
129: public String getPackageName() {
130: return packageName;
131: }
132:
133: /**
134: * Get the name of the type
135: *
136: *@return a string containing the name of the type
137: */
138: public String getObjectName() {
139: return objectName;
140: }
141:
142: /**
143: * Get the name of the field
144: *
145: *@return a string containing the name of the field
146: */
147: public String getFieldName() {
148: return fieldName;
149: }
150:
151: /**
152: * Gets a type declaration if this reference is to a package and type pair
153: *
154: *@return the summary
155: */
156: public TypeDeclSummary getTypeDecl() {
157: if (packageName == null) {
158: if (objectName != null) {
159: TypeDeclSummary result = new TypeDeclSummary(this ,
160: packageName, objectName);
161: TypeSummary test = GetTypeSummary.query(result);
162: if (test != null) {
163: return result;
164: }
165: }
166: return null;
167: }
168:
169: return new TypeDeclSummary(this , packageName, objectName);
170: }
171:
172: /**
173: * Gets the FirstObject attribute of the FieldAccessSummary object
174: *
175: *@return The FirstObject value
176: */
177: public String getFirstObject() {
178: String name = getName();
179: int index = name.indexOf(".");
180: if (index == -1) {
181: return name;
182: } else {
183: return name.substring(0, index);
184: }
185: }
186:
187: /**
188: * Gets the Name attribute of the FieldAccessSummary object
189: *
190: *@return The Name value
191: */
192: public String getName() {
193: // Start with the long name
194: StringBuffer buffer = new StringBuffer();
195:
196: if (packageName != null) {
197: buffer.append(packageName);
198: buffer.append(".");
199: }
200:
201: if (objectName != null) {
202: buffer.append(objectName);
203: buffer.append(".");
204: }
205:
206: buffer.append(fieldName);
207:
208: return buffer.toString();
209: }
210:
211: /**
212: * Convert this into a string
213: *
214: *@return a string representation of the type
215: */
216: public String toString() {
217: // Start with the long name
218: StringBuffer buffer = new StringBuffer();
219:
220: if (packageName != null) {
221: buffer.append(packageName);
222: buffer.append(".");
223: }
224:
225: if (objectName != null) {
226: buffer.append(objectName);
227: buffer.append(".");
228: }
229:
230: buffer.append(fieldName);
231:
232: if (isAssignment) {
233: buffer.append("-W");
234: }
235:
236: return buffer.toString();
237: }
238:
239: /**
240: * Provide method to visit a node
241: *
242: *@param visitor the visitor
243: *@param data the data for the visit
244: *@return some new data
245: */
246: public Object accept(SummaryVisitor visitor, Object data) {
247: return visitor.visit(this , data);
248: }
249:
250: /**
251: * Check to see if it is equal
252: *
253: *@param other the other item
254: *@return true if they are equal
255: */
256: public boolean equals(Object other) {
257: if (other instanceof FieldAccessSummary) {
258: FieldAccessSummary fas = (FieldAccessSummary) other;
259:
260: boolean sameObject = ((objectName == null) && (fas.objectName == null))
261: || ((objectName != null) && objectName
262: .equals(fas.objectName));
263:
264: boolean samePackage = ((packageName == null) && (fas.packageName == null))
265: || ((packageName != null) && packageName
266: .equals(fas.packageName));
267:
268: boolean sameField = ((fieldName == null) && (fas.fieldName == null))
269: || ((fieldName != null) && fieldName
270: .equals(fas.fieldName));
271:
272: return sameObject && samePackage && sameField
273: && (isAssignment == fas.isAssignment);
274: }
275: return super .equals(other);
276: }
277:
278: /**
279: * Gets the ChildIndex attribute of the FieldAccessSummary object
280: *
281: *@param parent Description of Parameter
282: *@param child Description of Parameter
283: *@return The ChildIndex value
284: */
285: private int getChildIndex(SimpleNode parent, SimpleNode child) {
286: for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
287: if (parent.jjtGetChild(i) == child) {
288: return i;
289: }
290: }
291: return -1;
292: }
293:
294: /**
295: * Description of the Method
296: *
297: *@param node Description of Parameter
298: *@return Description of the Returned Value
299: */
300: private boolean checkAssignment(SimpleNode node) {
301: SimpleNode previous = node;
302: for (SimpleNode current = (SimpleNode) node.jjtGetParent(); current != null; current = (SimpleNode) current
303: .jjtGetParent()) {
304: int nodeIdx = getChildIndex(current, previous);
305: if (nodeIdx == -1) {
306: throw new IllegalArgumentException(
307: "Child node not found into its parent ???");
308: }
309: if (current.jjtGetNumChildren() > (nodeIdx + 1)) {
310: if (current instanceof ASTStatementExpression
311: || current instanceof ASTExpression) {
312: if (current.jjtGetChild(nodeIdx + 1) instanceof ASTAssignmentOperator) {
313: return true;
314: } else {
315: return false;
316: }
317: }
318: }
319: previous = current;
320: }
321: return false;
322: }
323: }
|