001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010: package de.uka.ilkd.key.java.recoderext;
011:
012: import recoder.java.*;
013: import recoder.java.reference.ReferencePrefix;
014: import recoder.java.reference.TypeReference;
015: import recoder.java.reference.TypeReferenceContainer;
016:
017: public class ExecutionContext extends JavaNonTerminalProgramElement
018: implements Reference, TypeReferenceContainer,
019: ExpressionContainer {
020:
021: /**
022: * the ast parent
023: */
024: private NonTerminalProgramElement astParent;
025:
026: /**
027: * the class context
028: */
029: private TypeReference classContext;
030:
031: /**
032: * the reference to the active object
033: */
034: private ReferencePrefix runtimeInstance;
035:
036: protected ExecutionContext() {
037: }
038:
039: /**
040: * creates an execution context reference
041: * @param classContext the TypeReference refering to the next enclosing
042: * class
043: * @param runtimeInstance a ReferencePrefix to the object that
044: * is currently active/executed
045: */
046: public ExecutionContext(TypeReference classContext,
047: ReferencePrefix runtimeInstance) {
048: this .classContext = classContext;
049: this .runtimeInstance = runtimeInstance;
050: makeParentRoleValid();
051: }
052:
053: /**
054: * Returns the number of children of this node.
055: * @return an int giving the number of children of this node
056: */
057: public int getChildCount() {
058: int count = 0;
059: if (runtimeInstance != null)
060: count++;
061: if (classContext != null)
062: count++;
063: return count;
064: }
065:
066: /**
067: * Returns the child at the specified index in this node's "virtual"
068: * child array.
069: * @param index an index into this node's "virtual" child array
070: * @return the program element at the given position
071: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
072: * of bounds
073: */
074: public ProgramElement getChildAt(int index) {
075: if (classContext != null) {
076: if (index == 0)
077: return classContext;
078: index--;
079: }
080: if (runtimeInstance != null) {
081: if (index == 0)
082: return runtimeInstance;
083: index--;
084: }
085: throw new ArrayIndexOutOfBoundsException();
086: }
087:
088: /**
089: * Returns the positional code of the given child
090: * @param child the exact child to look for.
091: * @return the positional code of the given child, or <CODE>-1</CODE>.
092: */
093: public int getChildPositionCode(ProgramElement child) {
094: if (child != null) {
095: if (child == classContext)
096: return 0;
097: }
098: if (runtimeInstance != null) {
099: if (child == runtimeInstance)
100: return (1 << 4 | 1);
101: }
102: return -1;
103: }
104:
105: public void accept(SourceVisitor visitor) {
106: }
107:
108: public Object deepClone() {
109: return new ExecutionContext(classContext, runtimeInstance);
110: }
111:
112: public NonTerminalProgramElement getASTParent() {
113: return astParent;
114: }
115:
116: public void setParent(NonTerminalProgramElement parent) {
117: astParent = parent;
118: }
119:
120: public boolean replaceChild(recoder.java.ProgramElement child,
121: recoder.java.ProgramElement newChild) {
122: if (child == classContext) {
123: classContext = (TypeReference) newChild;
124: } else if (child == runtimeInstance) {
125: runtimeInstance = (ReferencePrefix) newChild;
126: } else {
127: return false;
128: }
129: makeParentRoleValid();
130: return true;
131: }
132:
133: /**
134: * Ensures that each child has "this" as syntactical parent.
135: */
136: public void makeParentRoleValid() {
137: super .makeParentRoleValid();
138: if (classContext != null) {
139: classContext.setParent(this );
140: }
141: if (runtimeInstance != null) {
142: ((Expression) runtimeInstance).setExpressionContainer(this );
143: }
144: }
145:
146: public TypeReference getTypeReferenceAt(int index) {
147: if (classContext != null && index == 0) {
148: return classContext;
149: }
150: throw new ArrayIndexOutOfBoundsException();
151: }
152:
153: public int getTypeReferenceCount() {
154: return classContext == null ? 0 : 1;
155: }
156:
157: public Expression getExpressionAt(int index) {
158: if (runtimeInstance != null && index == 0) {
159: return (Expression) runtimeInstance;
160: }
161: throw new ArrayIndexOutOfBoundsException();
162: }
163:
164: public int getExpressionCount() {
165: return runtimeInstance == null ? 0 : 1;
166: }
167:
168: /**
169: * returns the type reference to the next enclosing class
170: * @return the type reference to the next enclosing class
171: */
172: public TypeReference getTypeReference() {
173: return classContext;
174: }
175:
176: /**
177: * returns the runtime instance object
178: * @return the runtime instance object
179: */
180: public ReferencePrefix getRuntimeInstance() {
181: return runtimeInstance;
182: }
183:
184: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
185: }
186: }
|