Source for gnu.classpath.jdwp.processor.ReferenceTypeCommandSet

   1: /* ReferenceTypeCommandSet.java -- class to implement the ReferenceType
   2:    Command Set
   3:    Copyright (C) 2005, 2006 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.util.Value;
  55: 
  56: import java.io.DataOutputStream;
  57: import java.io.IOException;
  58: import java.lang.reflect.Field;
  59: import java.nio.ByteBuffer;
  60: 
  61: /**
  62:  * A class representing the ReferenceType Command Set.
  63:  * 
  64:  * @author Aaron Luchko <aluchko@redhat.com>
  65:  */
  66: public class ReferenceTypeCommandSet
  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.ReferenceType.SIGNATURE:
  77:             executeSignature(bb, os);
  78:             break;
  79:           case JdwpConstants.CommandSet.ReferenceType.CLASS_LOADER:
  80:             executeClassLoader(bb, os);
  81:             break;
  82:           case JdwpConstants.CommandSet.ReferenceType.MODIFIERS:
  83:             executeModifiers(bb, os);
  84:             break;
  85:           case JdwpConstants.CommandSet.ReferenceType.FIELDS:
  86:             executeFields(bb, os);
  87:             break;
  88:           case JdwpConstants.CommandSet.ReferenceType.METHODS:
  89:             executeMethods(bb, os);
  90:             break;
  91:           case JdwpConstants.CommandSet.ReferenceType.GET_VALUES:
  92:             executeGetValues(bb, os);
  93:             break;
  94:           case JdwpConstants.CommandSet.ReferenceType.SOURCE_FILE:
  95:             executeSourceFile(bb, os);
  96:             break;
  97:           case JdwpConstants.CommandSet.ReferenceType.NESTED_TYPES:
  98:             executeNestedTypes(bb, os);
  99:             break;
 100:           case JdwpConstants.CommandSet.ReferenceType.STATUS:
 101:             executeStatus(bb, os);
 102:             break;
 103:           case JdwpConstants.CommandSet.ReferenceType.INTERFACES:
 104:             executeInterfaces(bb, os);
 105:             break;
 106:           case JdwpConstants.CommandSet.ReferenceType.CLASS_OBJECT:
 107:             executeClassObject(bb, os);
 108:             break;
 109:           case JdwpConstants.CommandSet.ReferenceType.SOURCE_DEBUG_EXTENSION:
 110:             executeSourceDebugExtension(bb, os);
 111:             break;
 112:           case JdwpConstants.CommandSet.ReferenceType.SIGNATURE_WITH_GENERIC:
 113:             executeSignatureWithGeneric(bb, os);
 114:             break;
 115:           case JdwpConstants.CommandSet.ReferenceType.FIELDS_WITH_GENERIC:
 116:             executeFieldWithGeneric(bb, os);
 117:             break;
 118:           case JdwpConstants.CommandSet.ReferenceType.METHODS_WITH_GENERIC:
 119:             executeMethodsWithGeneric(bb, os);
 120:             break;
 121:           default:
 122:             throw new NotImplementedException("Command " + command +
 123:               " not found in ReferenceType Command Set.");
 124:           }
 125:       }
 126:     catch (IOException ex)
 127:       {
 128:         // The DataOutputStream we're using isn't talking to a socket at all
 129:         // So if we throw an IOException we're in serious trouble
 130:         throw new JdwpInternalErrorException(ex);
 131:       }
 132: 
 133:     return false;
 134:   }
 135: 
 136:   private void executeSignature(ByteBuffer bb, DataOutputStream os)
 137:     throws JdwpException, IOException
 138:   {
 139:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 140:     String sig = Signature.computeClassSignature(refId.getType());
 141:     JdwpString.writeString(os, sig);
 142:   }
 143: 
 144:   private void executeClassLoader(ByteBuffer bb, DataOutputStream os)
 145:     throws JdwpException, IOException
 146:   {
 147:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 148: 
 149:     Class clazz = refId.getType();
 150:     ClassLoader loader = clazz.getClassLoader();
 151:     ObjectId oid = idMan.getObjectId(loader);
 152:     oid.write(os);
 153:   }
 154: 
 155:   private void executeModifiers(ByteBuffer bb, DataOutputStream os)
 156:     throws JdwpException, IOException
 157:   {
 158:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 159: 
 160:     Class clazz = refId.getType();
 161:     os.writeInt(clazz.getModifiers());
 162:   }
 163: 
 164:   private void executeFields(ByteBuffer bb, DataOutputStream os)
 165:     throws JdwpException, IOException
 166:   {
 167:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 168:     Class clazz = refId.getType();
 169: 
 170:     Field[] fields = clazz.getFields();
 171:     os.writeInt(fields.length);
 172:     for (int i = 0; i < fields.length; i++)
 173:       {
 174:         Field field = fields[i];
 175:         idMan.getObjectId(field).write(os);
 176:         JdwpString.writeString(os, field.getName());
 177:         JdwpString.writeString(os, Signature.computeFieldSignature(field));
 178:         os.writeInt(field.getModifiers());
 179:       }
 180:   }
 181: 
 182:   private void executeMethods(ByteBuffer bb, DataOutputStream os)
 183:     throws JdwpException, IOException
 184:   {
 185:     ClassReferenceTypeId refId
 186:       = (ClassReferenceTypeId) 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.writeTaggedValue(os, value);
 224:               }
 225:             catch (IllegalArgumentException ex)
 226:               {
 227:                 // I suppose this would best qualify as an invalid field then
 228:                 throw new InvalidFieldException(ex);
 229:               }
 230:             catch (IllegalAccessException ex)
 231:               {
 232:                 // Since we set it as accessible this really shouldn't happen
 233:                 throw new JdwpInternalErrorException(ex);
 234:               }
 235:           }
 236:         else
 237:           throw new InvalidFieldException(fieldId.getId());
 238:       }
 239:   }
 240: 
 241:   private void executeSourceFile(ByteBuffer bb, DataOutputStream os)
 242:     throws JdwpException, IOException
 243:   {
 244:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 245:     Class clazz = refId.getType();
 246: 
 247:     // We'll need to go into the jvm for this unless there's an easier way
 248:     String sourceFileName = VMVirtualMachine.getSourceFile(clazz);
 249:     JdwpString.writeString(os, sourceFileName);
 250:     // clazz.getProtectionDomain().getCodeSource().getLocation();
 251:   }
 252: 
 253:   private void executeNestedTypes(ByteBuffer bb, DataOutputStream os)
 254:     throws JdwpException, IOException
 255:   {
 256:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 257:     Class clazz = refId.getType();
 258:     Class[] declaredClazzes = clazz.getDeclaredClasses();
 259:     os.writeInt(declaredClazzes.length);
 260:     for (int i = 0; i < declaredClazzes.length; i++)
 261:       {
 262:         Class decClazz = declaredClazzes[i];
 263:         ReferenceTypeId clazzId = idMan.getReferenceTypeId(decClazz);
 264:         clazzId.writeTagged(os);
 265:       }
 266:   }
 267: 
 268:   private void executeStatus(ByteBuffer bb, DataOutputStream os)
 269:     throws JdwpException, IOException
 270:   {
 271:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 272:     Class clazz = refId.getType();
 273: 
 274:     // I don't think there's any other way to get this
 275:     int status = VMVirtualMachine.getClassStatus(clazz);
 276:     os.writeInt(status);
 277:   }
 278: 
 279:   private void executeInterfaces(ByteBuffer bb, DataOutputStream os)
 280:     throws JdwpException, IOException
 281:   {
 282:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 283:     Class clazz = refId.getType();
 284:     Class[] interfaces = clazz.getInterfaces();
 285:     os.writeInt(interfaces.length);
 286:     for (int i = 0; i < interfaces.length; i++)
 287:       {
 288:         Class interfaceClass = interfaces[i];
 289:         ReferenceTypeId intId = idMan.getReferenceTypeId(interfaceClass);
 290:         intId.write(os);
 291:       }
 292:   }
 293: 
 294:   private void executeClassObject(ByteBuffer bb, DataOutputStream os)
 295:     throws JdwpException, IOException
 296:   {
 297:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 298:     Class clazz = refId.getType();
 299:     ObjectId clazzObjectId = idMan.getObjectId(clazz);
 300:     clazzObjectId.write(os);
 301:   }
 302: 
 303:   private void executeSourceDebugExtension(ByteBuffer bb, DataOutputStream os)
 304:     throws JdwpException, IOException
 305:   {
 306:     // This command is optional, determined by VirtualMachines CapabilitiesNew
 307:     // so we'll leave it till later to implement
 308:     throw new NotImplementedException(
 309:       "Command SourceDebugExtension not implemented.");
 310:   }
 311: 
 312:   private void executeSignatureWithGeneric(ByteBuffer bb, DataOutputStream os)
 313:     throws JdwpException, IOException
 314:   {
 315:     // We don't have generics yet
 316:     throw new NotImplementedException(
 317:       "Command SourceDebugExtension not implemented.");
 318:   }
 319: 
 320:   private void executeFieldWithGeneric(ByteBuffer bb, DataOutputStream os)
 321:     throws JdwpException, IOException
 322:   {
 323:     // We don't have generics yet
 324:     throw new NotImplementedException(
 325:       "Command SourceDebugExtension not implemented.");
 326:   }
 327: 
 328:   private void executeMethodsWithGeneric(ByteBuffer bb, DataOutputStream os)
 329:     throws JdwpException, IOException
 330:   {
 331:     // We don't have generics yet
 332:     throw new NotImplementedException(
 333:       "Command SourceDebugExtension not implemented.");
 334:   }
 335: }