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 java
.lang
.reflect
.Field
;
43 import java
.lang
.reflect
.Method
;
46 * A class to compute class and method signatures.
48 * @author Tom Tromey (tromey@redhat.com)
49 * @author Keith Seitz (keiths@redhat.com)
51 public class Signature
54 * Computes the class signature, i.e., java.lang.String.class
55 * returns "Ljava/lang/String;".
57 * @param theClass the class for which to compute the signature
58 * @return the class's type signature
60 public static String
computeClassSignature (Class theClass
)
62 StringBuffer sb
= new StringBuffer ();
63 _addToSignature (sb
, theClass
);
64 return sb
.toString ();
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;".
72 * @param field the field for which to compute the signature
73 * @return the field's type signature
75 public static String
computeFieldSignature (Field field
)
77 return computeClassSignature (field
.getType());
81 * Computes the method signature, i.e., java.lang.String.split (String, int)
82 * returns "(Ljava/lang/String;I)[Ljava/lang/String;"
84 * @param method the method for which to compute the signature
85 * @return the method's type signature
87 public static String
computeMethodSignature (Method method
)
89 return _computeSignature (method
.getReturnType (),
90 method
.getParameterTypes ());
93 private static String
_computeSignature (Class returnType
,
96 StringBuffer sb
= new StringBuffer ("(");
97 if (paramTypes
!= null)
99 for (int i
= 0; i
< paramTypes
.length
; ++i
)
100 _addToSignature (sb
, paramTypes
[i
]);
103 _addToSignature (sb
, returnType
);
104 return sb
.toString();
107 private static void _addToSignature (StringBuffer sb
, Class k
)
109 // For some reason there's no easy way to get the signature of a
111 if (k
.isPrimitive ())
115 else if (k
== boolean.class)
117 else if (k
== byte.class)
119 else if (k
== char.class)
121 else if (k
== short.class)
123 else if (k
== int.class)
125 else if (k
== float.class)
127 else if (k
== double.class)
129 else if (k
== long.class)
134 String name
= k
.getName ();
135 int len
= name
.length ();
136 sb
.ensureCapacity (len
);
139 for (int i
= 0; i
< len
; ++i
)
141 char c
= name
.charAt (i
);