001: package org.acm.seguin.pmd.symboltable;
002:
003: import net.sourceforge.jrefactory.ast.ASTAssignmentOperator;
004: import net.sourceforge.jrefactory.ast.ASTPrimaryExpression;
005: import net.sourceforge.jrefactory.ast.ASTPrimarySuffix;
006: import net.sourceforge.jrefactory.ast.ASTExpression;
007: import net.sourceforge.jrefactory.ast.SimpleNode;
008:
009: public class NameOccurrence {
010:
011: private SimpleNode location;
012: private String image;
013: private NameOccurrence qualifiedName;
014: private boolean isMethodOrConstructorInvocation;
015:
016: public NameOccurrence(SimpleNode location, String image) {
017: this .location = location;
018: this .image = image;
019: }
020:
021: public void setIsMethodOrConstructorInvocation() {
022: isMethodOrConstructorInvocation = true;
023: }
024:
025: public boolean isMethodOrConstructorInvocation() {
026: return isMethodOrConstructorInvocation;
027: }
028:
029: public void setNameWhichThisQualifies(NameOccurrence qualifiedName) {
030: this .qualifiedName = qualifiedName;
031: }
032:
033: public NameOccurrence getNameForWhichThisIsAQualifier() {
034: return qualifiedName;
035: }
036:
037: public SimpleNode getLocation() {
038: return location;
039: }
040:
041: public boolean isOnLeftHandSide() {
042: SimpleNode statementExpression;
043: if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
044: statementExpression = (SimpleNode) location.jjtGetParent()
045: .jjtGetParent();
046: } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
047: statementExpression = (SimpleNode) location.jjtGetParent()
048: .jjtGetParent().jjtGetParent();
049: } else {
050: throw new RuntimeException(
051: "Found a NameOccurrence that didn't have an ASTPrimary Expression as parent or grandparent. Parent = "
052: + location.jjtGetParent()
053: + " and grandparent = "
054: + location.jjtGetParent().jjtGetParent());
055: }
056:
057: return statementExpression.jjtGetNumChildren() > 1
058: && statementExpression.jjtGetChild(1) instanceof ASTAssignmentOperator;
059: }
060:
061: public boolean isArrayAccess() {
062: SimpleNode primaryExpression;
063: if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
064: primaryExpression = (SimpleNode) location.jjtGetParent();
065: } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
066: primaryExpression = (SimpleNode) location.jjtGetParent()
067: .jjtGetParent();
068: } else {
069: throw new RuntimeException(
070: "Found a NameOccurrence that didn't have an ASTPrimary Expression as parent or grandparent. Parent = "
071: + location.jjtGetParent()
072: + " and grandparent = "
073: + location.jjtGetParent().jjtGetParent());
074: }
075:
076: if (primaryExpression.jjtGetNumChildren() > 1
077: && primaryExpression.jjtGetChild(1) instanceof ASTPrimarySuffix) {
078: ASTPrimarySuffix suffix = (ASTPrimarySuffix) primaryExpression
079: .jjtGetChild(1);
080: if (suffix.jjtGetNumChildren() > 0
081: && suffix.jjtGetChild(0) instanceof ASTExpression) {
082: return true;
083: }
084: }
085: return false;
086: }
087:
088: public Scope getScope() {
089: return (Scope) location.getScope();
090: }
091:
092: public int getBeginLine() {
093: return location.getBeginLine();
094: }
095:
096: public boolean isThisOrSuper() {
097: return image.equals("this") || image.equals("super");
098: }
099:
100: public boolean equals(Object o) {
101: NameOccurrence n = (NameOccurrence) o;
102: return n.getImage().equals(getImage());
103: }
104:
105: public String getImage() {
106: return image;
107: }
108:
109: public int hashCode() {
110: return getImage().hashCode();
111: }
112:
113: public String toString() {
114: return getImage() + ":" + location.getBeginLine() + ":"
115: + location.getClass();
116: }
117: }
|