1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/classfile/Descriptor.java Tue Jun 03 13:26:47 2008 -0700 1.3 @@ -0,0 +1,198 @@ 1.4 +/* 1.5 + * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 + 1.30 +package com.sun.tools.classfile; 1.31 + 1.32 +import java.io.IOException; 1.33 + 1.34 +/** 1.35 + * See JVMS3, section 4.4. 1.36 + * 1.37 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.38 + * you write code that depends on this, you do so at your own risk. 1.39 + * This code and its internal interfaces are subject to change or 1.40 + * deletion without notice.</b> 1.41 + */ 1.42 +public class Descriptor { 1.43 + public class InvalidDescriptor extends DescriptorException { 1.44 + InvalidDescriptor(String desc) { 1.45 + this.desc = desc; 1.46 + this.index = -1; 1.47 + } 1.48 + 1.49 + InvalidDescriptor(String desc, int index) { 1.50 + this.desc = desc; 1.51 + this.index = index; 1.52 + } 1.53 + 1.54 + @Override 1.55 + public String getMessage() { 1.56 + // i18n 1.57 + if (index == -1) 1.58 + return "invalid descriptor \"" + desc + "\""; 1.59 + else 1.60 + return "descriptor is invalid at offset " + index + " in \"" + desc + "\""; 1.61 + } 1.62 + 1.63 + public final String desc; 1.64 + public final int index; 1.65 + 1.66 + } 1.67 + 1.68 + public Descriptor(ClassReader cr) throws IOException { 1.69 + this(cr.readUnsignedShort()); 1.70 + } 1.71 + 1.72 + public Descriptor(int index) { 1.73 + this.index = index; 1.74 + 1.75 + } 1.76 + 1.77 + public String getValue(ConstantPool constant_pool) throws ConstantPoolException { 1.78 + return constant_pool.getUTF8Value(index); 1.79 + } 1.80 + 1.81 + public int getParameterCount(ConstantPool constant_pool) 1.82 + throws ConstantPoolException, InvalidDescriptor { 1.83 + String desc = getValue(constant_pool); 1.84 + int end = desc.indexOf(")"); 1.85 + if (end == -1) 1.86 + throw new InvalidDescriptor(desc); 1.87 + parse(desc, 0, end + 1); 1.88 + return count; 1.89 + 1.90 + } 1.91 + 1.92 + public String getParameterTypes(ConstantPool constant_pool) 1.93 + throws ConstantPoolException, InvalidDescriptor { 1.94 + String desc = getValue(constant_pool); 1.95 + int end = desc.indexOf(")"); 1.96 + if (end == -1) 1.97 + throw new InvalidDescriptor(desc); 1.98 + return parse(desc, 0, end + 1); 1.99 + } 1.100 + 1.101 + public String getReturnType(ConstantPool constant_pool) 1.102 + throws ConstantPoolException, InvalidDescriptor { 1.103 + String desc = getValue(constant_pool); 1.104 + int end = desc.indexOf(")"); 1.105 + if (end == -1) 1.106 + throw new InvalidDescriptor(desc); 1.107 + return parse(desc, end + 1, desc.length()); 1.108 + } 1.109 + 1.110 + public String getFieldType(ConstantPool constant_pool) 1.111 + throws ConstantPoolException, InvalidDescriptor { 1.112 + String desc = getValue(constant_pool); 1.113 + return parse(desc, 0, desc.length()); 1.114 + } 1.115 + 1.116 + private String parse(String desc, int start, int end) 1.117 + throws InvalidDescriptor { 1.118 + int p = start; 1.119 + StringBuffer sb = new StringBuffer(); 1.120 + int dims = 0; 1.121 + count = 0; 1.122 + 1.123 + while (p < end) { 1.124 + String type; 1.125 + char ch; 1.126 + switch (ch = desc.charAt(p++)) { 1.127 + case '(': 1.128 + sb.append('('); 1.129 + continue; 1.130 + 1.131 + case ')': 1.132 + sb.append(')'); 1.133 + continue; 1.134 + 1.135 + case '[': 1.136 + dims++; 1.137 + continue; 1.138 + 1.139 + case 'B': 1.140 + type = "byte"; 1.141 + break; 1.142 + 1.143 + case 'C': 1.144 + type = "char"; 1.145 + break; 1.146 + 1.147 + case 'D': 1.148 + type = "double"; 1.149 + break; 1.150 + 1.151 + case 'F': 1.152 + type = "float"; 1.153 + break; 1.154 + 1.155 + case 'I': 1.156 + type = "int"; 1.157 + break; 1.158 + 1.159 + case 'J': 1.160 + type = "long"; 1.161 + break; 1.162 + 1.163 + case 'L': 1.164 + int sep = desc.indexOf(';', p); 1.165 + if (sep == -1) 1.166 + throw new InvalidDescriptor(desc, p - 1); 1.167 + type = desc.substring(p, sep).replace('/', '.'); 1.168 + p = sep + 1; 1.169 + break; 1.170 + 1.171 + case 'S': 1.172 + type = "short"; 1.173 + break; 1.174 + 1.175 + case 'Z': 1.176 + type = "boolean"; 1.177 + break; 1.178 + 1.179 + case 'V': 1.180 + type = "void"; 1.181 + break; 1.182 + 1.183 + default: 1.184 + throw new InvalidDescriptor(desc, p - 1); 1.185 + } 1.186 + 1.187 + if (sb.length() > 1 && sb.charAt(0) == '(') 1.188 + sb.append(", "); 1.189 + sb.append(type); 1.190 + for ( ; dims > 0; dims-- ) 1.191 + sb.append("[]"); 1.192 + 1.193 + count++; 1.194 + } 1.195 + 1.196 + return sb.toString(); 1.197 + } 1.198 + 1.199 + public final int index; 1.200 + private int count; 1.201 +}