Source for gnu.classpath.jdwp.processor.ClassTypeCommandSet

   1: /* ClassTypeCommandSet.java -- class to implement the ClassType
   2:    Command Set
   3:    Copyright (C) 2005, 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: terms of your choice, provided that you also meet, for each linked
  33: independent module, the terms and conditions of the license of that
  34: module.  An independent module is a module which is not derived from
  35: or based on this library.  If you modify this library, you may extend
  36: this exception to your version of the library, but you are not
  37: obligated to do so.  If you do not wish to do so, delete this
  38: exception statement from your version. */
  39: 
  40: 
  41: package gnu.classpath.jdwp.processor;
  42: 
  43: import gnu.classpath.jdwp.JdwpConstants;
  44: import gnu.classpath.jdwp.VMMethod;
  45: import gnu.classpath.jdwp.VMVirtualMachine;
  46: import gnu.classpath.jdwp.exception.InvalidFieldException;
  47: import gnu.classpath.jdwp.exception.JdwpException;
  48: import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
  49: import gnu.classpath.jdwp.exception.NotImplementedException;
  50: import gnu.classpath.jdwp.id.ObjectId;
  51: import gnu.classpath.jdwp.id.ReferenceTypeId;
  52: import gnu.classpath.jdwp.util.MethodResult;
  53: import gnu.classpath.jdwp.value.ObjectValue;
  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 ClassType Command Set.
  64:  *
  65:  * @author Aaron Luchko <aluchko@redhat.com>
  66:  */
  67: public class ClassTypeCommandSet
  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.ClassType.SUPERCLASS:
  78:             executeSuperclass(bb, os);
  79:             break;
  80:           case JdwpConstants.CommandSet.ClassType.SET_VALUES:
  81:             executeSetValues(bb, os);
  82:             break;
  83:           case JdwpConstants.CommandSet.ClassType.INVOKE_METHOD:
  84:             executeInvokeMethod(bb, os);
  85:             break;
  86:           case JdwpConstants.CommandSet.ClassType.NEW_INSTANCE:
  87:             executeNewInstance(bb, os);
  88:             break;
  89:           default:
  90:             throw new NotImplementedException("Command " + command +
  91:               " not found in ClassType Command Set.");
  92:           }
  93:       }
  94:     catch (IOException ex)
  95:       {
  96:         // The DataOutputStream we're using isn't talking to a socket at all
  97:         // So if we throw an IOException we're in serious trouble
  98:         throw new JdwpInternalErrorException(ex);
  99:       }
 100: 
 101:     return false;
 102:   }
 103: 
 104:   private void executeSuperclass(ByteBuffer bb, DataOutputStream os)
 105:       throws JdwpException, IOException
 106:   {
 107:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 108:     Class clazz = refId.getType();
 109:     Class superClazz = clazz.getSuperclass();
 110: 
 111:     if (superClazz == null) {
 112:         os.writeLong(0L);
 113:     } else {
 114:         ReferenceTypeId clazzId = idMan.getReferenceTypeId(superClazz);
 115:         clazzId.write(os);
 116:     }
 117:   }
 118: 
 119:   private void executeSetValues(ByteBuffer bb, DataOutputStream os)
 120:       throws JdwpException, IOException
 121:   {
 122:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 123: 
 124:     // We don't actually seem to need this...
 125:     Class clazz = refId.getType();
 126: 
 127:     int numValues = bb.getInt();
 128: 
 129:     for (int i = 0; i < numValues; i++)
 130:       {
 131:         ObjectId fieldId = idMan.readObjectId(bb);
 132:         Field field = (Field) (fieldId.getObject());
 133:         Object value = Value.getUntaggedObject(bb, field.getType());
 134:         try
 135:           {
 136:             field.setAccessible(true); // Might be a private field
 137:             field.set(null, value);
 138:           }
 139:         catch (IllegalArgumentException ex)
 140:           {
 141:             throw new InvalidFieldException(ex);
 142:           }
 143:         catch (IllegalAccessException ex)
 144:           { // Since we set it as accessible this really shouldn't happen
 145:             throw new JdwpInternalErrorException(ex);
 146:           }
 147:       }
 148:   }
 149: 
 150:   private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
 151:       throws JdwpException, IOException
 152:   {
 153:     MethodResult mr = invokeMethod(bb);
 154: 
 155:     Throwable exception = mr.getThrownException();
 156:     ObjectId eId = idMan.getObjectId(exception);
 157:     mr.getReturnedValue().writeTagged(os);
 158:     eId.writeTagged(os);
 159:   }
 160: 
 161:   private void executeNewInstance(ByteBuffer bb, DataOutputStream os)
 162:       throws JdwpException, IOException
 163:   {
 164:     MethodResult mr = invokeMethod(bb);
 165:     Throwable exception = mr.getThrownException();
 166: 
 167:     if (exception == null && ! (mr.getReturnedValue() instanceof ObjectValue))
 168:       throw new JdwpInternalErrorException("new instance returned non-object");
 169: 
 170:     ObjectValue ov = (ObjectValue) mr.getReturnedValue();
 171:     ObjectId oId = idMan.getObjectId(ov.getValue());
 172: 
 173:     ObjectId eId = idMan.getObjectId(exception);
 174: 
 175:     oId.writeTagged(os);
 176:     eId.writeTagged(os);
 177:   }
 178: 
 179:   /**
 180:    * Execute the static method and return the resulting MethodResult.
 181:    */
 182:   private MethodResult invokeMethod(ByteBuffer bb)
 183:     throws JdwpException, IOException
 184:   {
 185:     ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
 186:     Class clazz = refId.getType();
 187: 
 188:     ObjectId tId = idMan.readObjectId(bb);
 189:     Thread thread = (Thread) tId.getObject();
 190: 
 191:     VMMethod method = VMMethod.readId(clazz, bb);
 192: 
 193:     int args = bb.getInt();
 194:     Value[] values = new Value[args];
 195: 
 196:     for (int i = 0; i < args; i++)
 197:       values[i] = ValueFactory.createFromTagged(bb);
 198: 
 199:     int invokeOpts = bb.getInt();
 200:     MethodResult mr = VMVirtualMachine.executeMethod(null, thread,
 201:                                                      clazz, method,
 202:                                                      values, invokeOpts);
 203:     return mr;
 204:   }
 205: }