Source for gnu.classpath.jdwp.processor.ObjectReferenceCommandSet

   1: /* ObjectReferenceCommandSet.java -- class to implement the ObjectReference
   2:    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: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package gnu.classpath.jdwp.processor;
  41: 
  42: import gnu.classpath.jdwp.JdwpConstants;
  43: import gnu.classpath.jdwp.VMVirtualMachine;
  44: import gnu.classpath.jdwp.exception.InvalidFieldException;
  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.id.ReferenceTypeId;
  50: import gnu.classpath.jdwp.util.Value;
  51: import gnu.classpath.jdwp.util.MethodResult;
  52: 
  53: import java.io.DataOutputStream;
  54: import java.io.IOException;
  55: import java.lang.reflect.Field;
  56: import java.lang.reflect.Method;
  57: import java.nio.ByteBuffer;
  58: 
  59: /**
  60:  * A class representing the ObjectReference Command Set.
  61:  * 
  62:  * @author Aaron Luchko <aluchko@redhat.com>
  63:  */
  64: public class ObjectReferenceCommandSet
  65:   extends CommandSet
  66: {
  67:   public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
  68:     throws JdwpException
  69:   {
  70:     try
  71:       {
  72:         switch (command)
  73:           {
  74:           case JdwpConstants.CommandSet.ObjectReference.REFERENCE_TYPE:
  75:             executeReferenceType(bb, os);
  76:             break;
  77:           case JdwpConstants.CommandSet.ObjectReference.GET_VALUES:
  78:             executeGetValues(bb, os);
  79:             break;
  80:           case JdwpConstants.CommandSet.ObjectReference.SET_VALUES:
  81:             executeSetValues(bb, os);
  82:             break;
  83:           case JdwpConstants.CommandSet.ObjectReference.MONITOR_INFO:
  84:             executeMonitorInfo(bb, os);
  85:             break;
  86:           case JdwpConstants.CommandSet.ObjectReference.INVOKE_METHOD:
  87:             executeInvokeMethod(bb, os);
  88:             break;
  89:           case JdwpConstants.CommandSet.ObjectReference.DISABLE_COLLECTION:
  90:             executeDisableCollection(bb, os);
  91:             break;
  92:           case JdwpConstants.CommandSet.ObjectReference.ENABLE_COLLECTION:
  93:             executeEnableCollection(bb, os);
  94:             break;
  95:           case JdwpConstants.CommandSet.ObjectReference.IS_COLLECTED:
  96:             executeIsCollected(bb, os);
  97:             break;
  98:           default:
  99:             throw new NotImplementedException("Command " + command +
 100:               " not found in ObjectReference Command Set.");
 101:           }
 102:       }
 103:     catch (IOException ex)
 104:       {
 105:         // The DataOutputStream we're using isn't talking to a socket at all
 106:         // So if we throw an IOException we're in serious trouble
 107:         throw new JdwpInternalErrorException(ex);
 108:       }
 109: 
 110:     return false;
 111:   }
 112: 
 113:   private void executeReferenceType(ByteBuffer bb, DataOutputStream os)
 114:     throws JdwpException, IOException
 115:   {
 116:     ObjectId oid = idMan.readObjectId(bb);
 117:     Object obj = oid.getObject();
 118:     Class clazz = obj.getClass();
 119:     ReferenceTypeId refId = idMan.getReferenceTypeId(clazz);
 120:     refId.writeTagged(os);
 121:   }
 122: 
 123:   private void executeGetValues(ByteBuffer bb, DataOutputStream os)
 124:     throws JdwpException, IOException
 125:   {
 126:     ObjectId oid = idMan.readObjectId(bb);
 127:     Object obj = oid.getObject();
 128: 
 129:     int numFields = bb.getInt();
 130: 
 131:     os.writeInt(numFields); // Looks pointless but this is the protocol
 132: 
 133:     for (int i = 0; i < numFields; i++)
 134:       {
 135:         Field field = (Field) idMan.readObjectId(bb).getObject();
 136:         try
 137:           {
 138:             field.setAccessible(true); // Might be a private field
 139:             Object value = field.get(obj);
 140:             Value.writeTaggedValue(os, value);
 141:           }
 142:         catch (IllegalArgumentException ex)
 143:           {
 144:             // I suppose this would best qualify as an invalid field then
 145:             throw new InvalidFieldException(ex);
 146:           }
 147:         catch (IllegalAccessException ex)
 148:           {
 149:             // Since we set it as accessible this really shouldn't happen
 150:             throw new JdwpInternalErrorException(ex);
 151:           }
 152:       }
 153:   }
 154: 
 155:   private void executeSetValues(ByteBuffer bb, DataOutputStream os)
 156:     throws JdwpException, IOException
 157:   {
 158:     ObjectId oid = idMan.readObjectId(bb);
 159:     Object obj = oid.getObject();
 160: 
 161:     int numFields = bb.getInt();
 162: 
 163:     for (int i = 0; i < numFields; i++)
 164:       {
 165:         Field field = (Field) idMan.readObjectId(bb).getObject();
 166:         Object value = Value.getUntaggedObj(bb, field.getType());
 167:         try
 168:           {
 169:             field.setAccessible(true); // Might be a private field
 170:             field.set(obj, value);
 171:           }
 172:         catch (IllegalArgumentException ex)
 173:           {
 174:             // I suppose this would best qualify as an invalid field then
 175:             throw new InvalidFieldException(ex);
 176:           }
 177:         catch (IllegalAccessException ex)
 178:           {
 179:             // Since we set it as accessible this really shouldn't happen
 180:             throw new JdwpInternalErrorException(ex);
 181:           }
 182:       }
 183:   }
 184: 
 185:   private void executeMonitorInfo(ByteBuffer bb, DataOutputStream os)
 186:     throws JdwpException
 187:   {
 188:     // This command is optional, determined by VirtualMachines CapabilitiesNew
 189:     // so we'll leave it till later to implement
 190:     throw new NotImplementedException(
 191:       "Command ExecuteMonitorInfo not implemented.");
 192: 
 193:   }
 194: 
 195:   private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
 196:     throws JdwpException, IOException
 197:   {
 198:     ObjectId oid = idMan.readObjectId(bb);
 199:     Object obj = oid.getObject();
 200: 
 201:     ObjectId tid = idMan.readObjectId(bb);
 202:     Thread thread = (Thread) tid.getObject();
 203: 
 204:     ReferenceTypeId rid = idMan.readReferenceTypeId(bb);
 205:     Class clazz = rid.getType();
 206: 
 207:     ObjectId mid = idMan.readObjectId(bb);
 208:     Method method = (Method) mid.getObject();
 209: 
 210:     int args = bb.getInt();
 211:     Object[] values = new Object[args];
 212: 
 213:     for (int i = 0; i < args; i++)
 214:       {
 215:         values[i] = Value.getObj(bb);
 216:       }
 217: 
 218:     int invokeOptions = bb.getInt();
 219:     boolean suspend = ((invokeOptions
 220:             & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED)
 221:                != 0);
 222:     if (suspend)
 223:       {
 224:     // We must suspend all other running threads first
 225:         VMVirtualMachine.suspendAllThreads ();
 226:       }
 227: 
 228:     boolean nonVirtual = ((invokeOptions
 229:                & JdwpConstants.InvokeOptions.INVOKE_NONVIRTUAL)
 230:               != 0);
 231: 
 232:     MethodResult mr = VMVirtualMachine.executeMethod(obj, thread,
 233:                              clazz, method,
 234:                              values, nonVirtual);
 235:     Object value = mr.getReturnedValue();
 236:     Exception exception = mr.getThrownException();
 237: 
 238:     ObjectId eId = idMan.getObjectId(exception);
 239:     Value.writeTaggedValue(os, value);
 240:     eId.writeTagged(os);
 241:   }
 242: 
 243:   private void executeDisableCollection(ByteBuffer bb, DataOutputStream os)
 244:     throws JdwpException, IOException
 245:   {
 246:     ObjectId oid = idMan.readObjectId(bb);
 247:     oid.disableCollection();
 248:   }
 249: 
 250:   private void executeEnableCollection(ByteBuffer bb, DataOutputStream os)
 251:     throws JdwpException, IOException
 252:   {
 253:     ObjectId oid = idMan.readObjectId(bb);
 254:     oid.enableCollection();
 255:   }
 256: 
 257:   private void executeIsCollected(ByteBuffer bb, DataOutputStream os)
 258:     throws JdwpException, IOException
 259:   {
 260:     ObjectId oid = idMan.readObjectId(bb);
 261:     boolean collected = (oid.getReference().get () == null);
 262:     os.writeBoolean(collected);
 263:   }
 264: }