1 /* Signature.java -- utility class to compute class and method signatures
2 Copyright (C) 2005 Free Software Foundation
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
40 package gnu.classpath.jdwp.util;
42 import gnu.java.lang.CPStringBuilder;
44 import java.lang.reflect.Field;
45 import java.lang.reflect.Method;
48 * A class to compute class and method signatures.
50 * @author Tom Tromey (tromey@redhat.com)
51 * @author Keith Seitz (keiths@redhat.com)
53 public class Signature
56 * Computes the class signature, i.e., java.lang.String.class
57 * returns "Ljava/lang/String;".
59 * @param theClass the class for which to compute the signature
60 * @return the class's type signature
62 public static String computeClassSignature (Class theClass)
64 CPStringBuilder sb = new CPStringBuilder ();
65 _addToSignature (sb, theClass);
66 return sb.toString ();
70 * Computes the field signature which is just the class signature of the
71 * field's type, ie a Field of type java.lang.String this will return
72 * "Ljava/lang/String;".
74 * @param field the field for which to compute the signature
75 * @return the field's type signature
77 public static String computeFieldSignature (Field field)
79 return computeClassSignature (field.getType());
83 * Computes the method signature, i.e., java.lang.String.split (String, int)
84 * returns "(Ljava/lang/String;I)[Ljava/lang/String;"
86 * @param method the method for which to compute the signature
87 * @return the method's type signature
89 public static String computeMethodSignature (Method method)
91 return _computeSignature (method.getReturnType (),
92 method.getParameterTypes ());
95 private static String _computeSignature (Class returnType,
98 CPStringBuilder sb = new CPStringBuilder ("(");
99 if (paramTypes != null)
101 for (int i = 0; i < paramTypes.length; ++i)
102 _addToSignature (sb, paramTypes[i]);
105 _addToSignature (sb, returnType);
106 return sb.toString();
109 private static void _addToSignature (CPStringBuilder sb, Class k)
111 // For some reason there's no easy way to get the signature of a
113 if (k.isPrimitive ())
117 else if (k == boolean.class)
119 else if (k == byte.class)
121 else if (k == char.class)
123 else if (k == short.class)
125 else if (k == int.class)
127 else if (k == float.class)
129 else if (k == double.class)
131 else if (k == long.class)
136 String name = k.getName ();
137 int len = name.length ();
138 sb.ensureCapacity (len);
141 for (int i = 0; i < len; ++i)
143 char c = name.charAt (i);