001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.jpda.expr;
043:
044: import com.sun.jdi.Field;
045: import com.sun.jdi.IncompatibleThreadStateException;
046: import com.sun.jdi.LocalVariable;
047: import com.sun.jdi.ObjectReference;
048: import com.sun.jdi.StackFrame;
049: import com.sun.jdi.ThreadReference;
050: import com.sun.source.tree.CompilationUnitTree;
051: import com.sun.source.tree.Tree;
052: import com.sun.source.util.TreePath;
053: import com.sun.source.util.Trees;
054:
055: import java.util.*;
056: import javax.lang.model.element.TypeElement;
057:
058: import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
059:
060: /**
061: * Defines the exection context in which to evaluate a given expression. The context consists of:
062: * the current stack frame and the source file in which the expression would exist. The source file
063: * is needed for the import facility to work.
064: *
065: * @author Maros Sandor
066: */
067: public class EvaluationContext {
068:
069: /**
070: * The runtime context of a JVM is represented by a stack frame.
071: */
072: private StackFrame frame;
073: private int frameDepth;
074: private ThreadReference thread;
075: private List<String> sourceImports;
076: private List<String> staticImports;
077: private boolean canInvokeMethods;
078: private Runnable methodInvokePreproc;
079: private JPDADebuggerImpl debugger;
080:
081: private Trees trees;
082: private CompilationUnitTree compilationUnitTree;
083: private TreePath treePath;
084: private Map<Tree, VariableInfo> variables = new HashMap<Tree, VariableInfo>();
085:
086: /**
087: * Creates a new context in which to evaluate expresions.
088: *
089: * @param frame the frame in which context evaluation occurrs
090: * @param imports list of imports
091: * @param staticImports list of static imports
092: */
093: public EvaluationContext(StackFrame frame, int frameDepth,
094: List<String> imports, List<String> staticImports,
095: boolean canInvokeMethods, Runnable methodInvokePreproc,
096: JPDADebuggerImpl debugger) {
097: if (frame == null)
098: throw new IllegalArgumentException(
099: "Frame argument must not be null");
100: if (imports == null)
101: throw new IllegalArgumentException(
102: "Imports argument must not be null");
103: if (staticImports == null)
104: throw new IllegalArgumentException(
105: "Static imports argument must not be null");
106: this .frame = frame;
107: this .frameDepth = frameDepth;
108: this .sourceImports = imports;
109: this .staticImports = staticImports;
110: this .canInvokeMethods = canInvokeMethods;
111: this .methodInvokePreproc = methodInvokePreproc;
112: this .debugger = debugger;
113: }
114:
115: public List<String> getStaticImports() {
116: return staticImports;
117: }
118:
119: public List<String> getImports() {
120: return sourceImports;
121: }
122:
123: public StackFrame getFrame() {
124: return frame;
125: }
126:
127: public boolean canInvokeMethods() {
128: return canInvokeMethods;
129: }
130:
131: void setCanInvokeMethods(boolean canInvokeMethods) {
132: this .canInvokeMethods = canInvokeMethods;
133: }
134:
135: void methodToBeInvoked() {
136: if (methodInvokePreproc != null) {
137: methodInvokePreproc.run();
138: }
139: thread = frame.thread();
140: }
141:
142: void methodInvokeDone() throws IncompatibleThreadStateException {
143: // Refresh the stack frame
144: frame = thread.frame(frameDepth);
145: }
146:
147: JPDADebuggerImpl getDebugger() {
148: return debugger;
149: }
150:
151: public void setTrees(Trees trees) {
152: this .trees = trees;
153: }
154:
155: Trees getTrees() {
156: return trees;
157: }
158:
159: public void setCompilationUnit(
160: CompilationUnitTree compilationUnitTree) {
161: this .compilationUnitTree = compilationUnitTree;
162: }
163:
164: CompilationUnitTree getCompilationUnit() {
165: return compilationUnitTree;
166: }
167:
168: public void setTreePath(TreePath treePath) {
169: this .treePath = treePath;
170: }
171:
172: TreePath getTreePath() {
173: return treePath;
174: }
175:
176: Map<Tree, VariableInfo> getVariables() {
177: return variables;
178: }
179:
180: static final class VariableInfo {
181: public Field field;
182: public ObjectReference fieldObject;
183: public LocalVariable var;
184:
185: public VariableInfo(Field field) {
186: this .field = field;
187: }
188:
189: public VariableInfo(Field field, ObjectReference fieldObject) {
190: this .field = field;
191: this .fieldObject = fieldObject;
192: }
193:
194: public VariableInfo(LocalVariable var) {
195: this.var = var;
196: }
197: }
198:
199: }
|