| 1 | 
         769 | 
         jeremybenn | 
         /* Value.java -- base class of JDWP values
  | 
      
      
         | 2 | 
          | 
          | 
            Copyright (C) 2007 Free Software Foundation
  | 
      
      
         | 3 | 
          | 
          | 
          
  | 
      
      
         | 4 | 
          | 
          | 
         This file is part of GNU Classpath.
  | 
      
      
         | 5 | 
          | 
          | 
          
  | 
      
      
         | 6 | 
          | 
          | 
         GNU Classpath is free software; you can redistribute it and/or modify
  | 
      
      
         | 7 | 
          | 
          | 
         it under the terms of the GNU General Public License as published by
  | 
      
      
         | 8 | 
          | 
          | 
         the Free Software Foundation; either version 2, or (at your option)
  | 
      
      
         | 9 | 
          | 
          | 
         any later version.
  | 
      
      
         | 10 | 
          | 
          | 
          
  | 
      
      
         | 11 | 
          | 
          | 
         GNU Classpath is distributed in the hope that it will be useful, but
  | 
      
      
         | 12 | 
          | 
          | 
         WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
      
      
         | 13 | 
          | 
          | 
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  | 
      
      
         | 14 | 
          | 
          | 
         General Public License for more details.
  | 
      
      
         | 15 | 
          | 
          | 
          
  | 
      
      
         | 16 | 
          | 
          | 
         You should have received a copy of the GNU General Public License
  | 
      
      
         | 17 | 
          | 
          | 
         along with GNU Classpath; see the file COPYING.  If not, write to the
  | 
      
      
         | 18 | 
          | 
          | 
         Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  | 
      
      
         | 19 | 
          | 
          | 
         02110-1301 USA.
  | 
      
      
         | 20 | 
          | 
          | 
          
  | 
      
      
         | 21 | 
          | 
          | 
         Linking this library statically or dynamically with other modules is
  | 
      
      
         | 22 | 
          | 
          | 
         making a combined work based on this library.  Thus, the terms and
  | 
      
      
         | 23 | 
          | 
          | 
         conditions of the GNU General Public License cover the whole
  | 
      
      
         | 24 | 
          | 
          | 
         combination.
  | 
      
      
         | 25 | 
          | 
          | 
          
  | 
      
      
         | 26 | 
          | 
          | 
         As a special exception, the copyright holders of this library give you
  | 
      
      
         | 27 | 
          | 
          | 
         permission to link this library with independent modules to produce an
  | 
      
      
         | 28 | 
          | 
          | 
         executable, regardless of the license terms of these independent
  | 
      
      
         | 29 | 
          | 
          | 
         modules, and to copy and distribute the resulting executable under
  | 
      
      
         | 30 | 
          | 
          | 
         terms of your choice, provided that you also meet, for each linked
  | 
      
      
         | 31 | 
          | 
          | 
         independent module, the terms and conditions of the license of that
  | 
      
      
         | 32 | 
          | 
          | 
         module.  An independent module is a module which is not derived from
  | 
      
      
         | 33 | 
          | 
          | 
         or based on this library.  If you modify this library, you may extend
  | 
      
      
         | 34 | 
          | 
          | 
         this exception to your version of the library, but you are not
  | 
      
      
         | 35 | 
          | 
          | 
         obligated to do so.  If you do not wish to do so, delete this
  | 
      
      
         | 36 | 
          | 
          | 
         exception statement from your version. */
  | 
      
      
         | 37 | 
          | 
          | 
          
  | 
      
      
         | 38 | 
          | 
          | 
          
  | 
      
      
         | 39 | 
          | 
          | 
         package gnu.classpath.jdwp.value;
  | 
      
      
         | 40 | 
          | 
          | 
          
  | 
      
      
         | 41 | 
          | 
          | 
         import gnu.classpath.jdwp.exception.InvalidClassException;
  | 
      
      
         | 42 | 
          | 
          | 
         import gnu.classpath.jdwp.exception.InvalidObjectException;
  | 
      
      
         | 43 | 
          | 
          | 
         import gnu.classpath.jdwp.exception.InvalidTagException;
  | 
      
      
         | 44 | 
          | 
          | 
         import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
  | 
      
      
         | 45 | 
          | 
          | 
          
  | 
      
      
         | 46 | 
          | 
          | 
         import java.io.DataOutputStream;
  | 
      
      
         | 47 | 
          | 
          | 
         import java.io.IOException;
  | 
      
      
         | 48 | 
          | 
          | 
         import java.nio.ByteBuffer;
  | 
      
      
         | 49 | 
          | 
          | 
          
  | 
      
      
         | 50 | 
          | 
          | 
         /**
  | 
      
      
         | 51 | 
          | 
          | 
          * Superclass for all JDWP Values.
  | 
      
      
         | 52 | 
          | 
          | 
          *
  | 
      
      
         | 53 | 
          | 
          | 
          * @author Kyle Galloway <kgallowa@redhat.com>
  | 
      
      
         | 54 | 
          | 
          | 
          */
  | 
      
      
         | 55 | 
          | 
          | 
         public abstract class Value
  | 
      
      
         | 56 | 
          | 
          | 
         {
  | 
      
      
         | 57 | 
          | 
          | 
           // A Tag representing the type of this value
  | 
      
      
         | 58 | 
          | 
          | 
           private byte _tag;
  | 
      
      
         | 59 | 
          | 
          | 
          
  | 
      
      
         | 60 | 
          | 
          | 
           /**
  | 
      
      
         | 61 | 
          | 
          | 
            * Create a new value of type tag.
  | 
      
      
         | 62 | 
          | 
          | 
            *
  | 
      
      
         | 63 | 
          | 
          | 
            * @param tag the type of the value
  | 
      
      
         | 64 | 
          | 
          | 
            */
  | 
      
      
         | 65 | 
          | 
          | 
           protected Value(byte tag)
  | 
      
      
         | 66 | 
          | 
          | 
           {
  | 
      
      
         | 67 | 
          | 
          | 
             _tag = tag;
  | 
      
      
         | 68 | 
          | 
          | 
           }
  | 
      
      
         | 69 | 
          | 
          | 
          
  | 
      
      
         | 70 | 
          | 
          | 
           /**
  | 
      
      
         | 71 | 
          | 
          | 
            * Get the tag for this Value
  | 
      
      
         | 72 | 
          | 
          | 
            *
  | 
      
      
         | 73 | 
          | 
          | 
            * @return the byte tag of this Value
  | 
      
      
         | 74 | 
          | 
          | 
            */
  | 
      
      
         | 75 | 
          | 
          | 
           public byte getTag()
  | 
      
      
         | 76 | 
          | 
          | 
           {
  | 
      
      
         | 77 | 
          | 
          | 
             return _tag;
  | 
      
      
         | 78 | 
          | 
          | 
           }
  | 
      
      
         | 79 | 
          | 
          | 
          
  | 
      
      
         | 80 | 
          | 
          | 
           /**
  | 
      
      
         | 81 | 
          | 
          | 
            * Calls the dervied classes writeValue method to write its value to the
  | 
      
      
         | 82 | 
          | 
          | 
            * DataOutputStream.
  | 
      
      
         | 83 | 
          | 
          | 
            *
  | 
      
      
         | 84 | 
          | 
          | 
            * @param os write the value here
  | 
      
      
         | 85 | 
          | 
          | 
            * @throws IOException
  | 
      
      
         | 86 | 
          | 
          | 
            */
  | 
      
      
         | 87 | 
          | 
          | 
           public void writeUntagged(DataOutputStream os)
  | 
      
      
         | 88 | 
          | 
          | 
             throws IOException
  | 
      
      
         | 89 | 
          | 
          | 
           {
  | 
      
      
         | 90 | 
          | 
          | 
             write(os);
  | 
      
      
         | 91 | 
          | 
          | 
           }
  | 
      
      
         | 92 | 
          | 
          | 
          
  | 
      
      
         | 93 | 
          | 
          | 
           /**
  | 
      
      
         | 94 | 
          | 
          | 
            * Will write the given object as a tagged value to the DataOutputStream.
  | 
      
      
         | 95 | 
          | 
          | 
            *
  | 
      
      
         | 96 | 
          | 
          | 
            * @param os write the value here
  | 
      
      
         | 97 | 
          | 
          | 
            * @param obj the Object to write
  | 
      
      
         | 98 | 
          | 
          | 
            * @throws IOException
  | 
      
      
         | 99 | 
          | 
          | 
            */
  | 
      
      
         | 100 | 
          | 
          | 
           public void writeTagged(DataOutputStream os)
  | 
      
      
         | 101 | 
          | 
          | 
             throws IOException
  | 
      
      
         | 102 | 
          | 
          | 
           {
  | 
      
      
         | 103 | 
          | 
          | 
             os.write (_tag);
  | 
      
      
         | 104 | 
          | 
          | 
             write(os);
  | 
      
      
         | 105 | 
          | 
          | 
           }
  | 
      
      
         | 106 | 
          | 
          | 
          
  | 
      
      
         | 107 | 
          | 
          | 
           /**
  | 
      
      
         | 108 | 
          | 
          | 
            * This method must write the value to the DataOutputStream in a manner
  | 
      
      
         | 109 | 
          | 
          | 
            * appropriate for the type of the value.
  | 
      
      
         | 110 | 
          | 
          | 
            *
  | 
      
      
         | 111 | 
          | 
          | 
            * @param os DataOutputStream to write to
  | 
      
      
         | 112 | 
          | 
          | 
            * @throws IOException
  | 
      
      
         | 113 | 
          | 
          | 
            */
  | 
      
      
         | 114 | 
          | 
          | 
           protected abstract void write(DataOutputStream os)
  | 
      
      
         | 115 | 
          | 
          | 
             throws IOException;
  | 
      
      
         | 116 | 
          | 
          | 
          
  | 
      
      
         | 117 | 
          | 
          | 
           /**
  | 
      
      
         | 118 | 
          | 
          | 
            * Returns an object representing this type
  | 
      
      
         | 119 | 
          | 
          | 
            *
  | 
      
      
         | 120 | 
          | 
          | 
            * @return an Object represntation of this value
  | 
      
      
         | 121 | 
          | 
          | 
            */
  | 
      
      
         | 122 | 
          | 
          | 
           protected abstract Object getObject();
  | 
      
      
         | 123 | 
          | 
          | 
          
  | 
      
      
         | 124 | 
          | 
          | 
           /**
  | 
      
      
         | 125 | 
          | 
          | 
            * Get an untagged object from the ByteBuffer
  | 
      
      
         | 126 | 
          | 
          | 
            *
  | 
      
      
         | 127 | 
          | 
          | 
            * @param bb the ByteBuffer to extract the value from
  | 
      
      
         | 128 | 
          | 
          | 
            * @param type a Class representing the type
  | 
      
      
         | 129 | 
          | 
          | 
            * @return an Object from the ByteBuffer of the type of the Class parameter
  | 
      
      
         | 130 | 
          | 
          | 
            * @throws JdwpInternalErrorException
  | 
      
      
         | 131 | 
          | 
          | 
            * @throws InvalidObjectException
  | 
      
      
         | 132 | 
          | 
          | 
            */
  | 
      
      
         | 133 | 
          | 
          | 
           public static Object getUntaggedObject(ByteBuffer bb, Class type)
  | 
      
      
         | 134 | 
          | 
          | 
             throws JdwpInternalErrorException, InvalidObjectException, InvalidClassException
  | 
      
      
         | 135 | 
          | 
          | 
           {
  | 
      
      
         | 136 | 
          | 
          | 
             Value val = ValueFactory.createFromUntagged(bb, type);
  | 
      
      
         | 137 | 
          | 
          | 
             return val.getObject();
  | 
      
      
         | 138 | 
          | 
          | 
           }
  | 
      
      
         | 139 | 
          | 
          | 
          
  | 
      
      
         | 140 | 
          | 
          | 
           /**
  | 
      
      
         | 141 | 
          | 
          | 
            * Get an untagged object from the ByteBuffer
  | 
      
      
         | 142 | 
          | 
          | 
            *
  | 
      
      
         | 143 | 
          | 
          | 
            * @param bb the ByteBuffer to extract the value from
  | 
      
      
         | 144 | 
          | 
          | 
            * @param tag a byte tag representing the type
  | 
      
      
         | 145 | 
          | 
          | 
            * @return an Object from the ByteBuffer of the type of the Class parameter
  | 
      
      
         | 146 | 
          | 
          | 
            * @throws JdwpInternalErrorException
  | 
      
      
         | 147 | 
          | 
          | 
            * @throws InvalidObjectException
  | 
      
      
         | 148 | 
          | 
          | 
            */
  | 
      
      
         | 149 | 
          | 
          | 
           public static Object getTaggedObject(ByteBuffer bb)
  | 
      
      
         | 150 | 
          | 
          | 
             throws JdwpInternalErrorException, InvalidObjectException, InvalidTagException
  | 
      
      
         | 151 | 
          | 
          | 
           {
  | 
      
      
         | 152 | 
          | 
          | 
             Value val = ValueFactory.createFromTagged(bb);
  | 
      
      
         | 153 | 
          | 
          | 
             return val.getObject();
  | 
      
      
         | 154 | 
          | 
          | 
           }
  | 
      
      
         | 155 | 
          | 
          | 
         }
  |