Source for gnu.classpath.jdwp.util.Signature

   1: /* Signature.java -- utility class to compute class and method signatures
   2:    Copyright (C) 2005 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: 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.util;
  41: 
  42: import java.lang.reflect.Field;
  43: import java.lang.reflect.Method;
  44: 
  45: /**
  46:  * A class to compute class and method signatures.
  47:  *
  48:  * @author Tom Tromey  (tromey@redhat.com)
  49:  * @author Keith Seitz  (keiths@redhat.com)
  50:  */
  51: public class Signature
  52: {
  53:   /**
  54:    * Computes the class signature, i.e., java.lang.String.class
  55:    * returns "Ljava/lang/String;".
  56:    *
  57:    * @param theClass  the class for which to compute the signature
  58:    * @return          the class's type signature
  59:    */
  60:   public static String computeClassSignature (Class theClass)
  61:   {
  62:     StringBuffer sb = new StringBuffer ();
  63:     _addToSignature (sb, theClass);
  64:     return sb.toString ();
  65:   }
  66: 
  67:   /**
  68:    * Computes the field signature which is just the class signature of the
  69:    * field's type, ie a Field of type java.lang.String this will return
  70:    * "Ljava/lang/String;".
  71:    *
  72:    * @param field  the field for which to compute the signature
  73:    * @return       the field's type signature
  74:    */
  75:   public static String computeFieldSignature (Field field)
  76:   {
  77:     return computeClassSignature (field.getType());
  78:   }
  79: 
  80:   /**
  81:    * Computes the method signature, i.e., java.lang.String.split (String, int)
  82:    * returns "(Ljava/lang/String;I)[Ljava/lang/String;"
  83:    *
  84:    * @param method  the method for which to compute the signature
  85:    * @return        the method's type signature
  86:    */
  87:   public static String computeMethodSignature (Method method)
  88:   {
  89:     return _computeSignature (method.getReturnType (),
  90:                   method.getParameterTypes ());
  91:   }
  92: 
  93:   private static String _computeSignature (Class returnType,
  94:                        Class[] paramTypes)
  95:   {
  96:     StringBuffer sb = new StringBuffer ("(");
  97:     if (paramTypes != null)
  98:       {
  99:     for (int i = 0; i < paramTypes.length; ++i)
 100:       _addToSignature (sb, paramTypes[i]);
 101:       }
 102:     sb.append (")");
 103:     _addToSignature (sb, returnType);
 104:     return sb.toString();
 105:   }
 106: 
 107:   private static void _addToSignature (StringBuffer sb, Class k)
 108:   {
 109:     // For some reason there's no easy way to get the signature of a
 110:     // class.
 111:     if (k.isPrimitive ())
 112:       {
 113:         if (k == void.class)
 114:           sb.append('V');
 115:         else if (k == boolean.class)
 116:           sb.append('Z');
 117:         else if (k == byte.class)
 118:           sb.append('B');
 119:         else if (k == char.class)
 120:           sb.append('C');
 121:         else if (k == short.class)
 122:           sb.append('S');
 123:         else if (k == int.class)
 124:           sb.append('I');
 125:         else if (k == float.class)
 126:           sb.append('F');
 127:         else if (k == double.class)
 128:           sb.append('D');
 129:         else if (k == long.class)
 130:           sb.append('J');
 131:         return;
 132:       }
 133:     
 134:     String name = k.getName ();
 135:     int len = name.length ();
 136:     sb.ensureCapacity (len);
 137:     if (! k.isArray ())
 138:       sb.append('L');
 139:     for (int i = 0; i < len; ++i)
 140:       {
 141:     char c = name.charAt (i);
 142:     if (c == '.')
 143:       c = '/';
 144:     sb.append (c);
 145:       }
 146:     if (! k.isArray ())
 147:       sb.append(';');
 148:   }
 149: }