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