Source for gnu.classpath.jdwp.processor.ArrayReferenceCommandSet

   1: /* ArrayReferenceCommandSet.java -- class to implement the Array
   2:    Reference Command Set
   3:    Copyright (C) 2005 Free Software Foundation
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: terms of your choice, provided that you also meet, for each linked
  33: independent module, the terms and conditions of the license of that
  34: module.  An independent module is a module which is not derived from
  35: or based on this library.  If you modify this library, you may extend
  36: this exception to your version of the library, but you are not
  37: obligated to do so.  If you do not wish to do so, delete this
  38: exception statement from your version. */
  39: 
  40: 
  41: package gnu.classpath.jdwp.processor;
  42: 
  43: import gnu.classpath.jdwp.JdwpConstants;
  44: import gnu.classpath.jdwp.exception.InvalidObjectException;
  45: import gnu.classpath.jdwp.exception.JdwpException;
  46: import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
  47: import gnu.classpath.jdwp.exception.NotImplementedException;
  48: import gnu.classpath.jdwp.id.ObjectId;
  49: import gnu.classpath.jdwp.util.Value;
  50: 
  51: import java.io.DataOutputStream;
  52: import java.io.IOException;
  53: import java.lang.reflect.Array;
  54: import java.nio.ByteBuffer;
  55: 
  56: /**
  57:  * A class representing the ArrayReference Command Set.
  58:  * 
  59:  * @author Aaron Luchko <aluchko@redhat.com>
  60:  */
  61: public class ArrayReferenceCommandSet
  62:   extends CommandSet
  63: {
  64:   public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
  65:     throws JdwpException
  66:   {
  67:     try
  68:       {
  69:         switch (command)
  70:           {
  71:           case JdwpConstants.CommandSet.ArrayReference.LENGTH:
  72:             executeLength(bb, os);
  73:             break;
  74:           case JdwpConstants.CommandSet.ArrayReference.GET_VALUES:
  75:             executeGetValues(bb, os);
  76:             break;
  77:           case JdwpConstants.CommandSet.ArrayReference.SET_VALUES:
  78:             executeSetValues(bb, os);
  79:             break;
  80:           default:
  81:             throw new NotImplementedException("Command " + command +
  82:               " not found in Array Reference Command Set.");
  83:           }
  84:       }
  85:     catch (IOException ex)
  86:       {
  87:         // The DataOutputStream we're using isn't talking to a socket at all
  88:         // So if we throw an IOException we're in serious trouble
  89:         throw new JdwpInternalErrorException(ex);
  90:       }
  91: 
  92:     return false;
  93:   }
  94: 
  95:   private void executeLength(ByteBuffer bb, DataOutputStream os)
  96:     throws InvalidObjectException, IOException
  97:   {
  98:     ObjectId oid = idMan.readObjectId(bb);
  99:     Object array = oid.getObject();
 100:     os.writeInt(Array.getLength(array));
 101:   }
 102: 
 103:   private void executeGetValues(ByteBuffer bb, DataOutputStream os)
 104:     throws JdwpException, IOException
 105:   {
 106:     ObjectId oid = idMan.readObjectId(bb);
 107:     Object array = oid.getObject();
 108:     int first = bb.getInt();
 109:     int length = bb.getInt();
 110: 
 111:     // We need to write out the byte signifying the type of array first
 112:     Class clazz = array.getClass().getComponentType();
 113: 
 114:     // Uugh, this is a little ugly but it's the only time we deal with
 115:     // arrayregions
 116:     if (clazz == byte.class)
 117:       os.writeByte(JdwpConstants.Tag.BYTE);
 118:     else if (clazz == char.class)
 119:       os.writeByte(JdwpConstants.Tag.CHAR);
 120:     else if (clazz == float.class)
 121:       os.writeByte(JdwpConstants.Tag.FLOAT);
 122:     else if (clazz == double.class)
 123:       os.writeByte(JdwpConstants.Tag.DOUBLE);
 124:     else if (clazz == int.class)
 125:       os.writeByte(JdwpConstants.Tag.BYTE);
 126:     else if (clazz == long.class)
 127:       os.writeByte(JdwpConstants.Tag.LONG);
 128:     else if (clazz == short.class)
 129:       os.writeByte(JdwpConstants.Tag.SHORT);
 130:     else if (clazz == void.class)
 131:       os.writeByte(JdwpConstants.Tag.VOID);
 132:     else if (clazz == boolean.class)
 133:       os.writeByte(JdwpConstants.Tag.BOOLEAN);
 134:     else if (clazz.isArray())
 135:       os.writeByte(JdwpConstants.Tag.ARRAY);
 136:     else if (String.class.isAssignableFrom(clazz))
 137:       os.writeByte(JdwpConstants.Tag.STRING);
 138:     else if (Thread.class.isAssignableFrom(clazz))
 139:       os.writeByte(JdwpConstants.Tag.THREAD);
 140:     else if (ThreadGroup.class.isAssignableFrom(clazz))
 141:       os.writeByte(JdwpConstants.Tag.THREAD_GROUP);
 142:     else if (ClassLoader.class.isAssignableFrom(clazz))
 143:       os.writeByte(JdwpConstants.Tag.CLASS_LOADER);
 144:     else if (Class.class.isAssignableFrom(clazz))
 145:       os.writeByte(JdwpConstants.Tag.CLASS_OBJECT);
 146:     else
 147:       os.writeByte(JdwpConstants.Tag.OBJECT);
 148: 
 149:     // Write all the values, primitives should be untagged and Objects must be
 150:     // tagged
 151:     for (int i = first; i < first + length; i++)
 152:       {
 153:         Object value = Array.get(array, i);
 154:         if (clazz.isPrimitive())
 155:           Value.writeUntaggedValue(os, value);
 156:         else
 157:           Value.writeTaggedValue(os, value);
 158:       }
 159:   }
 160: 
 161:   private void executeSetValues(ByteBuffer bb, DataOutputStream os)
 162:     throws IOException, JdwpException
 163:   {
 164:     ObjectId oid = idMan.readObjectId(bb);
 165:     Object array = oid.getObject();
 166:     int first = bb.getInt();
 167:     int length = bb.getInt();
 168:     Class type = array.getClass().getComponentType();
 169:     for (int i = first; i < first + length; i++)
 170:       {
 171:         Object value = Value.getUntaggedObj(bb, type);
 172:         Array.set(array, i, value);
 173:       }
 174:   }
 175: }