Source for gnu.classpath.jdwp.processor.ReferenceTypeCommandSet

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