1: 
  38: 
  39: 
  40: package ;
  41: 
  42: import ;
  43: import ;
  44: import ;
  45: import ;
  46: import ;
  47: import ;
  48: import ;
  49: import ;
  50: import ;
  51: import ;
  52: import ;
  53: import ;
  54: import ;
  55: 
  56: import ;
  57: import ;
  58: import ;
  59: import ;
  60: 
  61: 
  66: public class ObjectReferenceCommandSet
  67:   extends CommandSet
  68: {
  69:   public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
  70:     throws JdwpException
  71:   {
  72:     try
  73:       {
  74:         switch (command)
  75:           {
  76:           case JdwpConstants.CommandSet.ObjectReference.REFERENCE_TYPE:
  77:             executeReferenceType(bb, os);
  78:             break;
  79:           case JdwpConstants.CommandSet.ObjectReference.GET_VALUES:
  80:             executeGetValues(bb, os);
  81:             break;
  82:           case JdwpConstants.CommandSet.ObjectReference.SET_VALUES:
  83:             executeSetValues(bb, os);
  84:             break;
  85:           case JdwpConstants.CommandSet.ObjectReference.MONITOR_INFO:
  86:             executeMonitorInfo(bb, os);
  87:             break;
  88:           case JdwpConstants.CommandSet.ObjectReference.INVOKE_METHOD:
  89:             executeInvokeMethod(bb, os);
  90:             break;
  91:           case JdwpConstants.CommandSet.ObjectReference.DISABLE_COLLECTION:
  92:             executeDisableCollection(bb, os);
  93:             break;
  94:           case JdwpConstants.CommandSet.ObjectReference.ENABLE_COLLECTION:
  95:             executeEnableCollection(bb, os);
  96:             break;
  97:           case JdwpConstants.CommandSet.ObjectReference.IS_COLLECTED:
  98:             executeIsCollected(bb, os);
  99:             break;
 100:           default:
 101:             throw new NotImplementedException("Command " + command +
 102:               " not found in ObjectReference Command Set.");
 103:           }
 104:       }
 105:     catch (IOException ex)
 106:       {
 107:         
 108:         
 109:         throw new JdwpInternalErrorException(ex);
 110:       }
 111: 
 112:     return false;
 113:   }
 114: 
 115:   private void executeReferenceType(ByteBuffer bb, DataOutputStream os)
 116:     throws JdwpException, IOException
 117:   {
 118:     ObjectId oid = idMan.readObjectId(bb);
 119:     Object obj = oid.getObject();
 120:     Class clazz = obj.getClass();
 121:     ReferenceTypeId refId = idMan.getReferenceTypeId(clazz);
 122:     refId.writeTagged(os);
 123:   }
 124: 
 125:   private void executeGetValues(ByteBuffer bb, DataOutputStream os)
 126:     throws JdwpException, IOException
 127:   {
 128:     ObjectId oid = idMan.readObjectId(bb);
 129:     Object obj = oid.getObject();
 130: 
 131:     int numFields = bb.getInt();
 132: 
 133:     os.writeInt(numFields); 
 134: 
 135:     for (int i = 0; i < numFields; i++)
 136:       {
 137:         Field field = (Field) idMan.readObjectId(bb).getObject();
 138:         try
 139:           {
 140:             field.setAccessible(true); 
 141:             Object value = field.get(obj);
 142:             Value val = ValueFactory.createFromObject(value,
 143:                                                       field.getType());
 144:             val.writeTagged(os);
 145:           }
 146:         catch (IllegalArgumentException ex)
 147:           {
 148:             
 149:             throw new InvalidFieldException(ex);
 150:           }
 151:         catch (IllegalAccessException ex)
 152:           {
 153:             
 154:             throw new JdwpInternalErrorException(ex);
 155:           }
 156:       }
 157:   }
 158: 
 159:   private void executeSetValues(ByteBuffer bb, DataOutputStream os)
 160:     throws JdwpException, IOException
 161:   {
 162:     ObjectId oid = idMan.readObjectId(bb);
 163:     Object obj = oid.getObject();
 164: 
 165:     int numFields = bb.getInt();
 166: 
 167:     for (int i = 0; i < numFields; i++)
 168:       {
 169:         Field field = (Field) idMan.readObjectId(bb).getObject();
 170:         Object value = Value.getUntaggedObject(bb, field.getType());
 171:         try
 172:           {
 173:             field.setAccessible(true); 
 174:             field.set(obj, value);
 175:           }
 176:         catch (IllegalArgumentException ex)
 177:           {
 178:             
 179:             throw new InvalidFieldException(ex);
 180:           }
 181:         catch (IllegalAccessException ex)
 182:           {
 183:             
 184:             throw new JdwpInternalErrorException(ex);
 185:           }
 186:       }
 187:   }
 188: 
 189:   private void executeMonitorInfo(ByteBuffer bb, DataOutputStream os)
 190:     throws JdwpException, IOException
 191:   {
 192:     if (!VMVirtualMachine.canGetMonitorInfo)
 193:       {
 194:         String msg = "getting monitor info not supported";
 195:         throw new NotImplementedException(msg);
 196:       }
 197: 
 198:     ObjectId oid = idMan.readObjectId(bb);
 199:     Object obj = oid.getObject();
 200:     MonitorInfo info = VMVirtualMachine.getMonitorInfo(obj);
 201:     info.write(os);
 202:   }
 203: 
 204:   private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
 205:     throws JdwpException, IOException
 206:   {
 207:     ObjectId oid = idMan.readObjectId(bb);
 208:     Object obj = oid.getObject();
 209: 
 210:     ObjectId tid = idMan.readObjectId(bb);
 211:     Thread thread = (Thread) tid.getObject();
 212: 
 213:     ReferenceTypeId rid = idMan.readReferenceTypeId(bb);
 214:     Class clazz = rid.getType();
 215: 
 216:     VMMethod method = VMMethod.readId(clazz, bb);
 217: 
 218:     int args = bb.getInt();
 219:     Value[] values = new Value[args];
 220: 
 221:     for (int i = 0; i < args; i++)
 222:       values[i] = ValueFactory.createFromTagged(bb);
 223: 
 224:     int invokeOptions = bb.getInt();
 225:     MethodResult mr = VMVirtualMachine.executeMethod(obj, thread,
 226:                                                      clazz, method,
 227:                                                      values, invokeOptions);
 228:     Throwable exception = mr.getThrownException();
 229:     ObjectId eId = idMan.getObjectId(exception);
 230:     mr.getReturnedValue().writeTagged(os);
 231:     eId.writeTagged(os);
 232:   }
 233: 
 234:   private void executeDisableCollection(ByteBuffer bb, DataOutputStream os)
 235:     throws JdwpException, IOException
 236:   {
 237:     ObjectId oid = idMan.readObjectId(bb);
 238:     oid.disableCollection();
 239:   }
 240: 
 241:   private void executeEnableCollection(ByteBuffer bb, DataOutputStream os)
 242:     throws JdwpException, IOException
 243:   {
 244:     ObjectId oid = idMan.readObjectId(bb);
 245:     oid.enableCollection();
 246:   }
 247: 
 248:   private void executeIsCollected(ByteBuffer bb, DataOutputStream os)
 249:     throws JdwpException, IOException
 250:   {
 251:     ObjectId oid = idMan.readObjectId(bb);
 252:     boolean collected = (oid.getReference().get () == null);
 253:     os.writeBoolean(collected);
 254:   }
 255: }