001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal.jdwp;
011:
012: import java.io.IOException;
013: import java.lang.reflect.Field;
014: import java.lang.reflect.Modifier;
015: import java.util.HashMap;
016: import java.util.Map;
017:
018: /**
019: * This class implements the corresponding Java Debug Wire Protocol (JDWP) packet
020: * declared by the JDWP specification.
021: *
022: */
023: public class JdwpReplyPacket extends JdwpPacket {
024: /** Error code constants. */
025: public static final short NONE = 0;
026: public static final short INVALID_THREAD = 10;
027: public static final short INVALID_THREAD_GROUP = 11;
028: public static final short INVALID_PRIORITY = 12;
029: public static final short THREAD_NOT_SUSPENDED = 13;
030: public static final short THREAD_SUSPENDED = 14;
031: public static final short THREAD_NOT_ALIVE = 15;
032: public static final short INVALID_OBJECT = 20;
033: public static final short INVALID_CLASS = 21;
034: public static final short CLASS_NOT_PREPARED = 22;
035: public static final short INVALID_METHODID = 23;
036: public static final short INVALID_LOCATION = 24;
037: public static final short INVALID_FIELDID = 25;
038: public static final short INVALID_FRAMEID = 30;
039: public static final short NO_MORE_FRAMES = 31;
040: public static final short OPAQUE_FRAME = 32;
041: public static final short NOT_CURRENT_FRAME = 33;
042: public static final short TYPE_MISMATCH = 34;
043: public static final short INVALID_SLOT = 35;
044: public static final short DUPLICATE = 40;
045: public static final short NOT_FOUND = 41;
046: public static final short INVALID_MONITOR = 50;
047: public static final short NOT_MONITOR_OWNER = 51;
048: public static final short INTERRUPT = 52;
049: public static final short INVALID_CLASS_FORMAT = 60;
050: public static final short CIRCULAR_CLASS_DEFINITION = 61;
051: public static final short FAILS_VERIFICATION = 62;
052: public static final short ADD_METHOD_NOT_IMPLEMENTED = 63;
053: public static final short SCHEMA_CHANGE_NOT_IMPLEMENTED = 64;
054: public static final short INVALID_TYPESTATE = 65;
055: public static final short HIERARCHY_CHANGE_NOT_IMPLEMENTED = 66;
056: public static final short DELETE_METHOD_NOT_IMPLEMENTED = 67;
057: public static final short UNSUPPORTED_VERSION = 68;
058: public static final short NAMES_DONT_MATCH = 69;
059: public static final short CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED = 70;
060: public static final short METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED = 71;
061: public static final short NOT_IMPLEMENTED = 99;
062: public static final short NULL_POINTER = 100;
063: public static final short ABSENT_INFORMATION = 101;
064: public static final short INVALID_EVENT_TYPE = 102;
065: public static final short ILLEGAL_ARGUMENT = 103;
066: public static final short OUT_OF_MEMORY = 110;
067: public static final short ACCESS_DENIED = 111;
068: public static final short VM_DEAD = 112;
069: public static final short INTERNAL = 113;
070: public static final short UNATTACHED_THREAD = 115;
071: public static final short INVALID_TAG = 500;
072: public static final short ALREADY_INVOKING = 502;
073: public static final short INVALID_INDEX = 503;
074: public static final short INVALID_LENGTH = 504;
075: public static final short INVALID_STRING = 506;
076: public static final short INVALID_CLASS_LOADER = 507;
077: public static final short INVALID_ARRAY = 508;
078: public static final short TRANSPORT_LOAD = 509;
079: public static final short TRANSPORT_INIT = 510;
080: public static final short NATIVE_METHOD = 511;
081: public static final short INVALID_COUNT = 512;
082: public static final short HCR_OPERATION_REFUSED = 900; // HCR specific.
083:
084: /** Mapping of error codes to strings. */
085: private static HashMap fErrorMap = null;
086:
087: /** JDWP Error code. */
088: private short fErrorCode;
089:
090: /**
091: * Creates new JdwpReplyPacket.
092: */
093: public JdwpReplyPacket() {
094: setFlags(FLAG_REPLY_PACKET);
095: }
096:
097: /**
098: * @return Returns JDWP Error code.
099: */
100: public short errorCode() {
101: return fErrorCode;
102: }
103:
104: /**
105: * Assigns JDWP Error code.
106: */
107: public void setErrorCode(short newValue) {
108: fErrorCode = newValue;
109: }
110:
111: /**
112: * Reads header fields that are specific for this type of packet.
113: */
114: protected int readSpecificHeaderFields(byte[] bytes, int index)
115: throws IOException {
116: fErrorCode = (short) ((bytes[index] << 8) + (bytes[index + 1] << 0));
117: return 2;
118: }
119:
120: /**
121: * Writes header fields that are specific for this type of packet.
122: */
123: protected int writeSpecificHeaderFields(byte[] bytes, int index)
124: throws IOException {
125: bytes[index] = (byte) ((fErrorCode >>> 8) & 0xFF);
126: bytes[index + 1] = (byte) ((fErrorCode >>> 0) & 0xFF);
127: return 2;
128: }
129:
130: /**
131: * Retrieves constant mappings.
132: */
133: public static void getConstantMaps() {
134: if (fErrorMap != null) {
135: return;
136: }
137:
138: Field[] fields = JdwpReplyPacket.class.getDeclaredFields();
139: fErrorMap = new HashMap(fields.length);
140: for (int i = 0; i < fields.length; i++) {
141: Field field = fields[i];
142: if ((field.getModifiers() & Modifier.PUBLIC) == 0
143: || (field.getModifiers() & Modifier.STATIC) == 0
144: || (field.getModifiers() & Modifier.FINAL) == 0)
145: continue;
146:
147: try {
148: Integer intValue = new Integer(field.getInt(null));
149: fErrorMap.put(intValue, field.getName());
150: } catch (IllegalAccessException e) {
151: // Will not occur for own class.
152: } catch (IllegalArgumentException e) {
153: // Should not occur.
154: // We should take care that all public static final constants
155: // in this class are numbers that are convertible to int.
156: }
157: }
158: }
159:
160: /**
161: * @return Returns a map with string representations of error codes.
162: */
163: public static Map errorMap() {
164: getConstantMaps();
165: return fErrorMap;
166: }
167:
168: /* (non-Javadoc)
169: * @see java.lang.Object#toString()
170: */
171: public String toString() {
172: StringBuffer buffer = new StringBuffer();
173: buffer.append("["); //$NON-NLS-1$
174: buffer.append(getId());
175: buffer.append("] "); //$NON-NLS-1$
176: buffer.append("(reply)"); //$NON-NLS-1$
177: short ec = errorCode();
178: if (ec != NONE) {
179: buffer.append(" ERROR CODE: "); //$NON-NLS-1$
180: buffer.append(ec);
181: }
182: return buffer.toString();
183: }
184: }
|