Source for gnu.classpath.jdwp.processor.ReferenceTypeCommandSet

   1: /* ReferenceTypeCommandSet.java -- class to implement the ReferenceType
   2:    Command Set
   3:    Copyright (C) 2005, 2006, 2007 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.VMMethod;
  44: import gnu.classpath.jdwp.VMVirtualMachine;
  45: import gnu.classpath.jdwp.exception.InvalidFieldException;
  46: import gnu.classpath.jdwp.exception.JdwpException;
  47: import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
  48: import gnu.classpath.jdwp.exception.NotImplementedException;
  49: import gnu.classpath.jdwp.id.ClassReferenceTypeId;
  50: import gnu.classpath.jdwp.id.ObjectId;
  51: import gnu.classpath.jdwp.id.ReferenceTypeId;
  52: import gnu.classpath.jdwp.util.JdwpString;
  53: import gnu.classpath.jdwp.util.Signature;
  54: import gnu.classpath.jdwp.value.Value;
  55: import gnu.classpath.jdwp.value.ValueFactory;
  56: 
  57: import java.io.DataOutputStream;
  58: import java.io.IOException;
  59: import java.lang.reflect.Field;
  60: import java.nio.ByteBuffer;
  61: 
  62: /**
  63:  * A class representing the ReferenceType Command Set.
  64:  * 
  65:  * @author Aaron Luchko <aluchko@redhat.com>
  66:  */
  67: public class ReferenceTypeCommandSet
  68:   extends CommandSet
  69: {
  70:   public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
  71:     throws JdwpException
  72:   {
  73:     try
  74:       {
  75:         switch (command)
  76:           {
  77:           case JdwpConstants.CommandSet.ReferenceType.SIGNATURE:
  78:             executeSignature(bb, os);
  79:             break;
  80:           case JdwpConstants.CommandSet.ReferenceType.CLASS_LOADER:
  81:             executeClassLoader(bb, os);
  82:             break;
  83:           case JdwpConstants.CommandSet.ReferenceType.MODIFIERS:
  84:             executeModifiers(bb, os);
  85:             break;
  86:           case JdwpConstants.CommandSet.ReferenceType.FIELDS:
  87:             executeFields(bb, os);
  88:             break;
  89:           case JdwpConstants.CommandSet.ReferenceType.METHODS:
  90:             executeMethods(bb, os);
  91:             break;
  92:           case JdwpConstants.CommandSet.ReferenceType.GET_VALUES:
  93:             executeGetValues(bb, os);
  94:             break;
  95:           case JdwpConstants.CommandSet.ReferenceType.SOURCE_FILE:
  96:             executeSourceFile(bb, os);
  97:             break;
  98:           case JdwpConstants.CommandSet.ReferenceType.NESTED_TYPES:
  99:             executeNestedTypes(bb, os);
 100:             break;
 101:           case JdwpConstants.CommandSet.ReferenceType.STATUS:
 102:             executeStatus(bb, os);
 103:             break;
 104:           case JdwpConstants.CommandSet.ReferenceType.INTERFACES:
 105:             executeInterfaces(bb, os);
 106:             break;
 107:           case JdwpConstants.CommandSet.ReferenceType.CLASS_OBJECT:
 108:             executeClassObject(bb, os);
 109:             break;
 110:           case JdwpConstants.CommandSet.ReferenceType.SOURCE_DEBUG_EXTENSION:
 111:             executeSourceDebugExtension(bb, os);
 112:             break;
 113:           case JdwpConstants.CommandSet.ReferenceType.SIGNATURE_WITH_GENERIC:
 114:             executeSignatureWithGeneric(bb, os);
 115:             break;
 116:           case JdwpConstants.CommandSet.ReferenceType.FIELDS_WITH_GENERIC:
 117:             executeFieldWithGeneric(bb, os);
 118:             break;
 119:           case JdwpConstants.CommandSet.ReferenceType.METHODS_WITH_GENERIC:
 120:             executeMethodsWithGeneric(bb, os);
 121:             break;
 122:           default:
 123:             throw new NotImplementedException("Command " + command +
 124:               " not found in ReferenceType Command Set.");
 125:           }
 126:       }
 127:     catch (IOException ex)
 128:       {
 129:         // The DataOutputStream we're using isn't talking to a socket at all
 130:         // So if we throw an IOException we're in serious trouble
 131:         throw new JdwpInternalErrorException(ex);
 132:       }
 133: 
 134:     return false;
 135:   }
 136: 
 137:   private void executeSignature(ByteBuffer bb, DataOutputStream os)
 138:     throws JdwpException, IOException
 139:   {
 140:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 141:     String sig = Signature.computeClassSignature(refId.getType());
 142:     JdwpString.writeString(os, sig);
 143:   }
 144: 
 145:   private void executeClassLoader(ByteBuffer bb, DataOutputStream os)
 146:     throws JdwpException, IOException
 147:   {
 148:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 149: 
 150:     Class clazz = refId.getType();
 151:     ClassLoader loader = clazz.getClassLoader();
 152:     ObjectId oid = idMan.getObjectId(loader);
 153:     oid.write(os);
 154:   }
 155: 
 156:   private void executeModifiers(ByteBuffer bb, DataOutputStream os)
 157:     throws JdwpException, IOException
 158:   {
 159:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 160: 
 161:     Class clazz = refId.getType();
 162:     os.writeInt(clazz.getModifiers());
 163:   }
 164: 
 165:   private void executeFields(ByteBuffer bb, DataOutputStream os)
 166:     throws JdwpException, IOException
 167:   {
 168:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 169:     Class clazz = refId.getType();
 170: 
 171:     Field[] fields = clazz.getFields();
 172:     os.writeInt(fields.length);
 173:     for (int i = 0; i < fields.length; i++)
 174:       {
 175:         Field field = fields[i];
 176:         idMan.getObjectId(field).write(os);
 177:         JdwpString.writeString(os, field.getName());
 178:         JdwpString.writeString(os, Signature.computeFieldSignature(field));
 179:         os.writeInt(field.getModifiers());
 180:       }
 181:   }
 182: 
 183:   private void executeMethods(ByteBuffer bb, DataOutputStream os)
 184:     throws JdwpException, IOException
 185:   {
 186:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 187:     Class clazz = refId.getType();
 188: 
 189:     VMMethod[] methods = VMVirtualMachine.getAllClassMethods(clazz);
 190:     os.writeInt (methods.length);
 191:     for (int i = 0; i < methods.length; i++)
 192:       {
 193:         VMMethod method = methods[i];
 194:         method.writeId(os);
 195:         JdwpString.writeString(os, method.getName());
 196:         JdwpString.writeString(os, method.getSignature());
 197:         os.writeInt(method.getModifiers());
 198:       }
 199:   }
 200: 
 201:   private void executeGetValues(ByteBuffer bb, DataOutputStream os)
 202:     throws JdwpException, IOException
 203:   {
 204:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 205:     Class clazz = refId.getType();
 206: 
 207:     int numFields = bb.getInt();
 208:     os.writeInt(numFields); // Looks pointless but this is the protocol
 209:     for (int i = 0; i < numFields; i++)
 210:       {
 211:         ObjectId fieldId = idMan.readObjectId(bb);
 212:         Field field = (Field) (fieldId.getObject());
 213:         Class fieldClazz = field.getDeclaringClass();
 214: 
 215:         // We don't actually need the clazz to get the field but we might as
 216:         // well check that the debugger got it right
 217:         if (fieldClazz.isAssignableFrom(clazz))
 218:           {
 219:             try
 220:               {
 221:                 field.setAccessible(true); // Might be a private field
 222:                 Object value = field.get(null);
 223:                 Value val = ValueFactory.createFromObject(value, 
 224:                                                           field.getType());
 225:                 val.writeTagged(os);
 226:               }
 227:             catch (IllegalArgumentException ex)
 228:               {
 229:                 // I suppose this would best qualify as an invalid field then
 230:                 throw new InvalidFieldException(ex);
 231:               }
 232:             catch (IllegalAccessException ex)
 233:               {
 234:                 // Since we set it as accessible this really shouldn't happen
 235:                 throw new JdwpInternalErrorException(ex);
 236:               }
 237:           }
 238:         else
 239:           throw new InvalidFieldException(fieldId.getId());
 240:       }
 241:   }
 242: 
 243:   private void executeSourceFile(ByteBuffer bb, DataOutputStream os)
 244:     throws JdwpException, IOException
 245:   {
 246:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 247:     Class clazz = refId.getType();
 248: 
 249:     // We'll need to go into the jvm for this unless there's an easier way
 250:     String sourceFileName = VMVirtualMachine.getSourceFile(clazz);
 251:     JdwpString.writeString(os, sourceFileName);
 252:     // clazz.getProtectionDomain().getCodeSource().getLocation();
 253:   }
 254: 
 255:   private void executeNestedTypes(ByteBuffer bb, DataOutputStream os)
 256:     throws JdwpException, IOException
 257:   {
 258:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 259:     Class clazz = refId.getType();
 260:     Class[] declaredClazzes = clazz.getDeclaredClasses();
 261:     os.writeInt(declaredClazzes.length);
 262:     for (int i = 0; i < declaredClazzes.length; i++)
 263:       {
 264:         Class decClazz = declaredClazzes[i];
 265:         ReferenceTypeId clazzId = idMan.getReferenceTypeId(decClazz);
 266:         clazzId.writeTagged(os);
 267:       }
 268:   }
 269: 
 270:   private void executeStatus(ByteBuffer bb, DataOutputStream os)
 271:     throws JdwpException, IOException
 272:   {
 273:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 274:     Class clazz = refId.getType();
 275: 
 276:     // I don't think there's any other way to get this
 277:     int status = VMVirtualMachine.getClassStatus(clazz);
 278:     os.writeInt(status);
 279:   }
 280: 
 281:   private void executeInterfaces(ByteBuffer bb, DataOutputStream os)
 282:     throws JdwpException, IOException
 283:   {
 284:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 285:     Class clazz = refId.getType();
 286:     Class[] interfaces = clazz.getInterfaces();
 287:     os.writeInt(interfaces.length);
 288:     for (int i = 0; i < interfaces.length; i++)
 289:       {
 290:         Class interfaceClass = interfaces[i];
 291:         ReferenceTypeId intId = idMan.getReferenceTypeId(interfaceClass);
 292:         intId.write(os);
 293:       }
 294:   }
 295: 
 296:   private void executeClassObject(ByteBuffer bb, DataOutputStream os)
 297:     throws JdwpException, IOException
 298:   {
 299:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 300:     Class clazz = refId.getType();
 301:     ObjectId clazzObjectId = idMan.getObjectId(clazz);
 302:     clazzObjectId.write(os);
 303:   }
 304: 
 305:   private void executeSourceDebugExtension(ByteBuffer bb, DataOutputStream os)
 306:     throws JdwpException, IOException
 307:   {
 308:     if (!VMVirtualMachine.canGetSourceDebugExtension)
 309:       {
 310:     String msg = "source debug extension is not supported";
 311:     throw new NotImplementedException(msg);
 312:       }
 313: 
 314:     ReferenceTypeId id = idMan.readReferenceTypeId(bb);
 315:     String ext = VMVirtualMachine.getSourceDebugExtension (id.getType());
 316:     JdwpString.writeString(os, ext);
 317:   }
 318: 
 319:   private void executeSignatureWithGeneric(ByteBuffer bb, DataOutputStream os)
 320:     throws JdwpException, IOException
 321:   {
 322:     // We don't have generics yet
 323:     throw new NotImplementedException(
 324:       "Command SignatureWithGeneric not implemented.");
 325:   }
 326: 
 327:   private void executeFieldWithGeneric(ByteBuffer bb, DataOutputStream os)
 328:     throws JdwpException, IOException
 329:   {
 330:     // We don't have generics yet
 331:     throw new NotImplementedException(
 332:       "Command executeFieldWithGeneric not implemented.");
 333:   }
 334: 
 335:   private void executeMethodsWithGeneric(ByteBuffer bb, DataOutputStream os)
 336:     throws JdwpException, IOException
 337:   {
 338:     // We don't have generics yet
 339:     throw new NotImplementedException(
 340:       "Command executeMethodsWithGeneric not implemented.");
 341:   }
 342: }