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