Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/classfile/Signature.java
38899 views
/*1* Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.classfile;2627import java.util.ArrayList;28import java.util.List;29import com.sun.tools.classfile.Type.*;3031/**32* See JVMS 4.4.4.33*34* <p><b>This is NOT part of any supported API.35* If you write code that depends on this, you do so at your own risk.36* This code and its internal interfaces are subject to change or37* deletion without notice.</b>38*/39public class Signature extends Descriptor {4041public Signature(int index) {42super(index);43}4445public Type getType(ConstantPool constant_pool) throws ConstantPoolException {46if (type == null)47type = parse(getValue(constant_pool));48return type;49}5051@Override52public int getParameterCount(ConstantPool constant_pool) throws ConstantPoolException {53MethodType m = (MethodType) getType(constant_pool);54return m.paramTypes.size();55}5657@Override58public String getParameterTypes(ConstantPool constant_pool) throws ConstantPoolException {59MethodType m = (MethodType) getType(constant_pool);60StringBuilder sb = new StringBuilder();61sb.append("(");62String sep = "";63for (Type paramType: m.paramTypes) {64sb.append(sep);65sb.append(paramType);66sep = ", ";67}68sb.append(")");69return sb.toString();70}7172@Override73public String getReturnType(ConstantPool constant_pool) throws ConstantPoolException {74MethodType m = (MethodType) getType(constant_pool);75return m.returnType.toString();76}7778@Override79public String getFieldType(ConstantPool constant_pool) throws ConstantPoolException {80return getType(constant_pool).toString();81}8283private Type parse(String sig) {84this.sig = sig;85sigp = 0;8687List<TypeParamType> typeParamTypes = null;88if (sig.charAt(sigp) == '<')89typeParamTypes = parseTypeParamTypes();9091if (sig.charAt(sigp) == '(') {92List<Type> paramTypes = parseTypeSignatures(')');93Type returnType = parseTypeSignature();94List<Type> throwsTypes = null;95while (sigp < sig.length() && sig.charAt(sigp) == '^') {96sigp++;97if (throwsTypes == null)98throwsTypes = new ArrayList<Type>();99throwsTypes.add(parseTypeSignature());100}101return new MethodType(typeParamTypes, paramTypes, returnType, throwsTypes);102} else {103Type t = parseTypeSignature();104if (typeParamTypes == null && sigp == sig.length())105return t;106Type superclass = t;107List<Type> superinterfaces = null;108while (sigp < sig.length()) {109if (superinterfaces == null)110superinterfaces = new ArrayList<Type>();111superinterfaces.add(parseTypeSignature());112}113return new ClassSigType(typeParamTypes, superclass, superinterfaces);114115}116}117118private Type parseTypeSignature() {119switch (sig.charAt(sigp)) {120case 'B':121sigp++;122return new SimpleType("byte");123124case 'C':125sigp++;126return new SimpleType("char");127128case 'D':129sigp++;130return new SimpleType("double");131132case 'F':133sigp++;134return new SimpleType("float");135136case 'I':137sigp++;138return new SimpleType("int");139140case 'J':141sigp++;142return new SimpleType("long");143144case 'L':145return parseClassTypeSignature();146147case 'S':148sigp++;149return new SimpleType("short");150151case 'T':152return parseTypeVariableSignature();153154case 'V':155sigp++;156return new SimpleType("void");157158case 'Z':159sigp++;160return new SimpleType("boolean");161162case '[':163sigp++;164return new ArrayType(parseTypeSignature());165166case '*':167sigp++;168return new WildcardType();169170case '+':171sigp++;172return new WildcardType(WildcardType.Kind.EXTENDS, parseTypeSignature());173174case '-':175sigp++;176return new WildcardType(WildcardType.Kind.SUPER, parseTypeSignature());177178default:179throw new IllegalStateException(debugInfo());180}181}182183private List<Type> parseTypeSignatures(char term) {184sigp++;185List<Type> types = new ArrayList<Type>();186while (sig.charAt(sigp) != term)187types.add(parseTypeSignature());188sigp++;189return types;190}191192private Type parseClassTypeSignature() {193assert sig.charAt(sigp) == 'L';194sigp++;195return parseClassTypeSignatureRest();196}197198private Type parseClassTypeSignatureRest() {199StringBuilder sb = new StringBuilder();200List<Type> argTypes = null;201ClassType t = null;202char sigch ;203204do {205switch (sigch = sig.charAt(sigp)) {206case '<':207argTypes = parseTypeSignatures('>');208break;209210case '.':211case ';':212sigp++;213t = new ClassType(t, sb.toString(), argTypes);214sb.setLength(0);215argTypes = null;216break;217218default:219sigp++;220sb.append(sigch);221break;222}223} while (sigch != ';');224225return t;226}227228private List<TypeParamType> parseTypeParamTypes() {229assert sig.charAt(sigp) == '<';230sigp++;231List<TypeParamType> types = new ArrayList<TypeParamType>();232while (sig.charAt(sigp) != '>')233types.add(parseTypeParamType());234sigp++;235return types;236}237238private TypeParamType parseTypeParamType() {239int sep = sig.indexOf(":", sigp);240String name = sig.substring(sigp, sep);241Type classBound = null;242List<Type> interfaceBounds = null;243sigp = sep + 1;244if (sig.charAt(sigp) != ':')245classBound = parseTypeSignature();246while (sig.charAt(sigp) == ':') {247sigp++;248if (interfaceBounds == null)249interfaceBounds = new ArrayList<Type>();250interfaceBounds.add(parseTypeSignature());251}252return new TypeParamType(name, classBound, interfaceBounds);253}254255private Type parseTypeVariableSignature() {256sigp++;257int sep = sig.indexOf(';', sigp);258Type t = new SimpleType(sig.substring(sigp, sep));259sigp = sep + 1;260return t;261}262263private String debugInfo() {264return sig.substring(0, sigp) + "!" + sig.charAt(sigp) + "!" + sig.substring(sigp+1);265}266267private String sig;268private int sigp;269270private Type type;271}272273274