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-2006 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.ArrayReference;
045: import com.sun.jdi.Value;
046: import com.sun.jdi.InvocationException;
047:
048: import java.util.*;
049: import java.text.MessageFormat;
050:
051: import org.openide.util.NbBundle;
052:
053: /**
054: * This class is a runtime exception because it integrates better with the generated code and
055: * it also prevents unnecessary code bloat.
056: *
057: * @author Maros Sandor
058: */
059: public class EvaluationException extends RuntimeException {
060:
061: private SimpleNode node;
062: private String reason;
063: private Object[] params;
064:
065: private String message;
066:
067: public EvaluationException(SimpleNode node, String reason,
068: Object[] params) {
069: this .node = node;
070: this .reason = reason;
071: this .params = params;
072: }
073:
074: public String getMessage() {
075: try {
076: return getMessageImpl();
077: } catch (Exception e) {
078: return message = formatMessage(
079: "CTL_EvalError_unknownInternalError", null);
080: }
081: }
082:
083: public String getMessageImpl() {
084: if (message != null)
085: return message;
086:
087: String[] msgParams = null;
088:
089: if (reason.equals("unknownNonterminal"))
090: msgParams = new String[] { JavaParserTreeConstants.jjtNodeName[node
091: .jjtGetID()] };
092: else if (reason.equals("internalError"))
093: msgParams = new String[] { JavaParserTreeConstants.jjtNodeName[node
094: .jjtGetID()] };
095: else if (reason.equals("invalidArrayInitializer"))
096: msgParams = new String[] { params[0] == null ? null
097: : params[0].toString() };
098: else if (reason.equals("arraySizeBadType"))
099: msgParams = new String[] { params[0] == null ? null
100: : params[0].toString() };
101: else if (reason.equals("arrayCreateError"))
102: msgParams = new String[] { params[0] == null ? null
103: : params[0].toString() };
104: else if (reason.equals("instantiateInterface"))
105: msgParams = new String[] { params[0] == null ? null
106: : params[0].toString() };
107: else if (reason.equals("castToBooleanRequired"))
108: msgParams = new String[] { params[0].toString() };
109: else if (reason.equals("castFromBooleanRequired"))
110: msgParams = new String[] { params[0].toString() };
111: else if (reason.equals("castError"))
112: msgParams = new String[] { params[0].toString(),
113: params[1].toString() };
114: else if (reason.equals("badOperandForPostfixOperator"))
115: msgParams = new String[] { params[0].toString() };
116: else if (reason.equals("postfixOperatorEvaluationError"))
117: msgParams = new String[] { params[1].toString() };
118: else if (reason.equals("badOperandForPrefixOperator"))
119: msgParams = new String[] { params[0].toString() };
120: else if (reason.equals("prefixOperatorEvaluationError"))
121: msgParams = new String[] { params[1].toString() };
122: else if (reason.equals("badOperandForUnaryOperator"))
123: msgParams = new String[] { params[0].toString() };
124: else if (reason.equals("unaryOperatorEvaluationError"))
125: msgParams = new String[] { params[1].toString() };
126: else if (reason.equals("unknownType"))
127: msgParams = new String[] { params[0].toString() };
128: else if (reason.equals("internalErrorResolvingType"))
129: msgParams = new String[] { params[0].toString() };
130: else if (reason.equals("instanceOfLeftOperandNotAReference"))
131: msgParams = new String[] { ((Value) params[0]).type()
132: .name() };
133: else if (reason
134: .equals("conditionalOrAndBooleanOperandRequired"))
135: msgParams = new String[] { ((Value) params[0]).type()
136: .name() };
137: else if (reason
138: .equals("conditionalQuestionMarkBooleanOperandRequired"))
139: msgParams = new String[] { ((Value) params[0]).type()
140: .name() };
141: else if (reason.equals("thisObjectUnavailable"))
142: msgParams = null;
143: else if (reason.equals("objectReferenceRequiredOnDereference"))
144: msgParams = new String[] { ((Value) params[0]).type()
145: .name() };
146: else if (reason.equals("badArgument"))
147: msgParams = new String[] { params[0].toString() };
148: else if (reason.equals("argumentsBadSyntax"))
149: msgParams = new String[] { params[0].toString() };
150: else if (reason.equals("ambigousMethod"))
151: msgParams = new String[] {
152: ((Identifier) params[0]).typeContext.name(),
153: ((Identifier) params[0]).identifier };
154: else if (reason.equals("noSuchMethod"))
155: msgParams = new String[] {
156: ((Identifier) params[0]).typeContext.name(),
157: ((Identifier) params[0]).identifier };
158: else if (reason.equals("callException"))
159: msgParams = new String[] {
160: ((Identifier) params[1]).typeContext.name(),
161: ((Identifier) params[1]).identifier,
162: params[0].toString() };
163: else if (reason.equals("calleeException"))
164: msgParams = new String[] {
165: ((Identifier) params[1]).typeContext.name(),
166: ((Identifier) params[1]).identifier,
167: ((InvocationException) (params[0])).exception()
168: .toString() };
169: else if (reason.equals("identifierNotAReference"))
170: msgParams = new String[] { ((Value) params[0]).type()
171: .name() };
172: else if (reason.equals("notarray"))
173: msgParams = new String[] { params[0].toString() };
174: else if (reason.equals("arrayIndexNAN"))
175: msgParams = new String[] { params[1].toString() };
176: else if (reason.equals("arrayIndexOutOfBounds"))
177: msgParams = new String[] {
178: params[1].toString(),
179: Integer.toString(((ArrayReference) params[0])
180: .length() - 1) };
181: else if (reason.equals("unknownVariable"))
182: msgParams = new String[] { ((Identifier) params[0]).identifier };
183: else if (reason.equals("integerLiteralTooBig"))
184: msgParams = new String[] { params[0].toString() };
185: else if (reason.equals("badFormatOfIntegerLiteral"))
186: msgParams = new String[] { params[0].toString() };
187: else if (reason.equals("unknownLiteralType"))
188: msgParams = new String[] { params[0].toString() };
189: else if (reason.equals("evaluateError"))
190: // return Assert.error(node, "evaluateError", value, ((Token) operators[i-1]).image, next);
191: msgParams = new String[] { params[1].toString(),
192: params[0].toString(), params[2].toString() };
193: else if (reason.equals("notEnclosingType"))
194: msgParams = new String[] {
195: ((Identifier) params[0]).typeContext.name(),
196: ((Identifier) params[0]).super Qualifier };
197: else if (reason
198: .equals("accessInstanceVariableFromStaticContext"))
199: msgParams = new String[] { ((Identifier) params[0]).identifier };
200: else if (reason.equals("methodCallOnNull"))
201: msgParams = new String[] { params[0].toString() };
202: else {
203: reason = "unknownInternalError";
204: msgParams = null;
205: }
206:
207: message = formatMessage("CTL_EvalError_" + reason, msgParams);
208:
209: return message;
210: }
211:
212: private String formatMessage(String msg, String[] params) {
213: ResourceBundle bundle = NbBundle
214: .getBundle(EvaluationException.class);
215: msg = bundle.getString(msg);
216: return MessageFormat.format(msg, (Object[]) params);
217: }
218: }
|