src/share/classes/com/sun/tools/classfile/Signature.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 972
694ff82ca68e
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

jjg@46 1 /*
ohair@554 2 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
jjg@46 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46 4 *
jjg@46 5 * This code is free software; you can redistribute it and/or modify it
jjg@46 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@46 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@46 10 *
jjg@46 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@46 15 * accompanied this code).
jjg@46 16 *
jjg@46 17 * You should have received a copy of the GNU General Public License version
jjg@46 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@46 24 */
jjg@46 25
jjg@46 26 package com.sun.tools.classfile;
jjg@46 27
jjg@46 28 import java.util.ArrayList;
jjg@46 29 import java.util.List;
jjg@427 30 import com.sun.tools.classfile.Type.*;
jjg@46 31
jjg@46 32 /**
jjg@46 33 * See JVMS3 4.4.4.
jjg@46 34 *
jjg@581 35 * <p><b>This is NOT part of any supported API.
jjg@581 36 * If you write code that depends on this, you do so at your own risk.
jjg@46 37 * This code and its internal interfaces are subject to change or
jjg@46 38 * deletion without notice.</b>
jjg@46 39 */
jjg@46 40 public class Signature extends Descriptor {
jjg@46 41
jjg@46 42 public Signature(int index) {
jjg@46 43 super(index);
jjg@46 44 }
jjg@46 45
jjg@46 46 public Type getType(ConstantPool constant_pool) throws ConstantPoolException {
jjg@46 47 if (type == null)
jjg@46 48 type = parse(getValue(constant_pool));
jjg@46 49 return type;
jjg@46 50 }
jjg@46 51
jjg@46 52 @Override
jjg@46 53 public int getParameterCount(ConstantPool constant_pool) throws ConstantPoolException {
jjg@427 54 MethodType m = (MethodType) getType(constant_pool);
jjg@427 55 return m.paramTypes.size();
jjg@46 56 }
jjg@46 57
jjg@46 58 @Override
jjg@46 59 public String getParameterTypes(ConstantPool constant_pool) throws ConstantPoolException {
jjg@427 60 MethodType m = (MethodType) getType(constant_pool);
jjg@46 61 StringBuilder sb = new StringBuilder();
jjg@46 62 sb.append("(");
jjg@46 63 String sep = "";
jjg@427 64 for (Type paramType: m.paramTypes) {
jjg@46 65 sb.append(sep);
jjg@427 66 sb.append(paramType);
jjg@46 67 sep = ", ";
jjg@46 68 }
jjg@46 69 sb.append(")");
jjg@46 70 return sb.toString();
jjg@46 71 }
jjg@46 72
jjg@46 73 @Override
jjg@46 74 public String getReturnType(ConstantPool constant_pool) throws ConstantPoolException {
jjg@427 75 MethodType m = (MethodType) getType(constant_pool);
jjg@46 76 return m.returnType.toString();
jjg@46 77 }
jjg@46 78
jjg@46 79 @Override
jjg@46 80 public String getFieldType(ConstantPool constant_pool) throws ConstantPoolException {
jjg@46 81 return getType(constant_pool).toString();
jjg@46 82 }
jjg@46 83
jjg@46 84 private Type parse(String sig) {
jjg@46 85 this.sig = sig;
jjg@46 86 sigp = 0;
jjg@46 87
jjg@427 88 List<TypeParamType> typeParamTypes = null;
jjg@46 89 if (sig.charAt(sigp) == '<')
jjg@427 90 typeParamTypes = parseTypeParamTypes();
jjg@46 91
jjg@46 92 if (sig.charAt(sigp) == '(') {
jjg@427 93 List<Type> paramTypes = parseTypeSignatures(')');
jjg@46 94 Type returnType = parseTypeSignature();
jjg@46 95 List<Type> throwsTypes = null;
jjg@46 96 while (sigp < sig.length() && sig.charAt(sigp) == '^') {
jjg@46 97 sigp++;
jjg@46 98 if (throwsTypes == null)
jjg@46 99 throwsTypes = new ArrayList<Type>();
jjg@46 100 throwsTypes.add(parseTypeSignature());
jjg@46 101 }
jjg@427 102 return new MethodType(typeParamTypes, paramTypes, returnType, throwsTypes);
jjg@46 103 } else {
jjg@46 104 Type t = parseTypeSignature();
jjg@427 105 if (typeParamTypes == null && sigp == sig.length())
jjg@46 106 return t;
jjg@46 107 Type superclass = t;
jjg@427 108 List<Type> superinterfaces = null;
jjg@427 109 while (sigp < sig.length()) {
jjg@427 110 if (superinterfaces == null)
jjg@427 111 superinterfaces = new ArrayList<Type>();
jjg@46 112 superinterfaces.add(parseTypeSignature());
jjg@427 113 }
jjg@427 114 return new ClassSigType(typeParamTypes, superclass, superinterfaces);
jjg@46 115
jjg@46 116 }
jjg@46 117 }
jjg@46 118
jjg@46 119 private Type parseTypeSignature() {
jjg@46 120 switch (sig.charAt(sigp)) {
jjg@46 121 case 'B':
jjg@46 122 sigp++;
jjg@427 123 return new SimpleType("byte");
jjg@46 124
jjg@46 125 case 'C':
jjg@46 126 sigp++;
jjg@427 127 return new SimpleType("char");
jjg@46 128
jjg@46 129 case 'D':
jjg@46 130 sigp++;
jjg@427 131 return new SimpleType("double");
jjg@46 132
jjg@46 133 case 'F':
jjg@46 134 sigp++;
jjg@427 135 return new SimpleType("float");
jjg@46 136
jjg@46 137 case 'I':
jjg@46 138 sigp++;
jjg@427 139 return new SimpleType("int");
jjg@46 140
jjg@46 141 case 'J':
jjg@46 142 sigp++;
jjg@427 143 return new SimpleType("long");
jjg@46 144
jjg@46 145 case 'L':
jjg@46 146 return parseClassTypeSignature();
jjg@46 147
jjg@46 148 case 'S':
jjg@46 149 sigp++;
jjg@427 150 return new SimpleType("short");
jjg@46 151
jjg@46 152 case 'T':
jjg@46 153 return parseTypeVariableSignature();
jjg@46 154
jjg@46 155 case 'V':
jjg@46 156 sigp++;
jjg@427 157 return new SimpleType("void");
jjg@46 158
jjg@46 159 case 'Z':
jjg@46 160 sigp++;
jjg@427 161 return new SimpleType("boolean");
jjg@46 162
jjg@46 163 case '[':
jjg@46 164 sigp++;
jjg@427 165 return new ArrayType(parseTypeSignature());
jjg@46 166
jjg@46 167 case '*':
jjg@46 168 sigp++;
jjg@427 169 return new WildcardType();
jjg@46 170
jjg@46 171 case '+':
jjg@46 172 sigp++;
jjg@427 173 return new WildcardType(WildcardType.Kind.EXTENDS, parseTypeSignature());
jjg@46 174
jjg@46 175 case '-':
jjg@46 176 sigp++;
jjg@427 177 return new WildcardType(WildcardType.Kind.SUPER, parseTypeSignature());
jjg@46 178
jjg@46 179 default:
jjg@46 180 throw new IllegalStateException(debugInfo());
jjg@46 181 }
jjg@46 182 }
jjg@46 183
jjg@46 184 private List<Type> parseTypeSignatures(char term) {
jjg@46 185 sigp++;
jjg@46 186 List<Type> types = new ArrayList<Type>();
jjg@46 187 while (sig.charAt(sigp) != term)
jjg@46 188 types.add(parseTypeSignature());
jjg@46 189 sigp++;
jjg@46 190 return types;
jjg@46 191 }
jjg@46 192
jjg@46 193 private Type parseClassTypeSignature() {
jjg@46 194 assert sig.charAt(sigp) == 'L';
jjg@46 195 sigp++;
jjg@46 196 return parseClassTypeSignatureRest();
jjg@46 197 }
jjg@46 198
jjg@46 199 private Type parseClassTypeSignatureRest() {
jjg@46 200 StringBuilder sb = new StringBuilder();
jjg@427 201 List<Type> argTypes = null;
jjg@427 202 ClassType t = null;
jjg@427 203 char sigch ;
jjg@427 204
jjg@427 205 do {
jjg@46 206 switch (sigch = sig.charAt(sigp)) {
jjg@427 207 case '<':
jjg@427 208 argTypes = parseTypeSignatures('>');
jjg@46 209 break;
jjg@46 210
jjg@46 211 case '.':
jjg@46 212 case ';':
jjg@46 213 sigp++;
jjg@427 214 t = new ClassType(t, sb.toString(), argTypes);
jjg@427 215 sb.setLength(0);
jjg@427 216 argTypes = null;
jjg@46 217 break;
jjg@46 218
jjg@46 219 default:
jjg@46 220 sigp++;
jjg@46 221 sb.append(sigch);
jjg@46 222 break;
jjg@46 223 }
jjg@427 224 } while (sigch != ';');
jjg@427 225
jjg@427 226 return t;
jjg@46 227 }
jjg@46 228
jjg@427 229 private List<TypeParamType> parseTypeParamTypes() {
jjg@46 230 assert sig.charAt(sigp) == '<';
jjg@46 231 sigp++;
jjg@427 232 List<TypeParamType> types = new ArrayList<TypeParamType>();
jjg@46 233 while (sig.charAt(sigp) != '>')
jjg@427 234 types.add(parseTypeParamType());
jjg@46 235 sigp++;
jjg@46 236 return types;
jjg@46 237 }
jjg@46 238
jjg@427 239 private TypeParamType parseTypeParamType() {
jjg@46 240 int sep = sig.indexOf(":", sigp);
jjg@46 241 String name = sig.substring(sigp, sep);
jjg@46 242 Type classBound = null;
jjg@46 243 List<Type> interfaceBounds = null;
jjg@46 244 sigp = sep + 1;
jjg@46 245 if (sig.charAt(sigp) != ':')
jjg@46 246 classBound = parseTypeSignature();
jjg@46 247 while (sig.charAt(sigp) == ':') {
jjg@46 248 sigp++;
jjg@46 249 if (interfaceBounds == null)
jjg@46 250 interfaceBounds = new ArrayList<Type>();
jjg@46 251 interfaceBounds.add(parseTypeSignature());
jjg@46 252 }
jjg@427 253 return new TypeParamType(name, classBound, interfaceBounds);
jjg@46 254 }
jjg@46 255
jjg@46 256 private Type parseTypeVariableSignature() {
jjg@46 257 sigp++;
jjg@46 258 int sep = sig.indexOf(';', sigp);
jjg@427 259 Type t = new SimpleType(sig.substring(sigp, sep));
jjg@46 260 sigp = sep + 1;
jjg@46 261 return t;
jjg@46 262 }
jjg@46 263
jjg@46 264 private String debugInfo() {
jjg@46 265 return sig.substring(0, sigp) + "!" + sig.charAt(sigp) + "!" + sig.substring(sigp+1);
jjg@46 266 }
jjg@46 267
jjg@46 268 private String sig;
jjg@46 269 private int sigp;
jjg@46 270
jjg@46 271 private Type type;
jjg@46 272 }

mercurial