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

changeset 46
7708bd6d800d
child 54
eaf608c64fec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/classfile/Signature.java	Tue Jun 03 13:26:47 2008 -0700
     1.3 @@ -0,0 +1,275 @@
     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 +package com.sun.tools.classfile;
    1.30 +
    1.31 +import java.util.ArrayList;
    1.32 +import java.util.List;
    1.33 +
    1.34 +/**
    1.35 + * See JVMS3 4.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 Signature extends Descriptor {
    1.43 +
    1.44 +    public Signature(int index) {
    1.45 +        super(index);
    1.46 +    }
    1.47 +
    1.48 +    public Type getType(ConstantPool constant_pool) throws ConstantPoolException {
    1.49 +        if (type == null)
    1.50 +            type = parse(getValue(constant_pool));
    1.51 +        return type;
    1.52 +    }
    1.53 +
    1.54 +    @Override
    1.55 +    public int getParameterCount(ConstantPool constant_pool) throws ConstantPoolException {
    1.56 +        Type.MethodType m = (Type.MethodType) getType(constant_pool);
    1.57 +        return m.argTypes.size();
    1.58 +    }
    1.59 +
    1.60 +    @Override
    1.61 +    public String getParameterTypes(ConstantPool constant_pool) throws ConstantPoolException {
    1.62 +        Type.MethodType m = (Type.MethodType) getType(constant_pool);
    1.63 +        StringBuilder sb = new StringBuilder();
    1.64 +        sb.append("(");
    1.65 +        String sep = "";
    1.66 +        for (Type argType: m.argTypes) {
    1.67 +            sb.append(sep);
    1.68 +            sb.append(argType);
    1.69 +            sep = ", ";
    1.70 +        }
    1.71 +        sb.append(")");
    1.72 +        return sb.toString();
    1.73 +    }
    1.74 +
    1.75 +    @Override
    1.76 +    public String getReturnType(ConstantPool constant_pool) throws ConstantPoolException {
    1.77 +        Type.MethodType m = (Type.MethodType) getType(constant_pool);
    1.78 +        return m.returnType.toString();
    1.79 +    }
    1.80 +
    1.81 +    @Override
    1.82 +    public String getFieldType(ConstantPool constant_pool) throws ConstantPoolException {
    1.83 +        return getType(constant_pool).toString();
    1.84 +    }
    1.85 +
    1.86 +    private Type parse(String sig) {
    1.87 +        this.sig = sig;
    1.88 +        sigp = 0;
    1.89 +
    1.90 +        List<Type> typeArgTypes = null;
    1.91 +        if (sig.charAt(sigp) == '<')
    1.92 +            typeArgTypes = parseTypeArgTypes();
    1.93 +
    1.94 +        if (sig.charAt(sigp) == '(') {
    1.95 +            List<Type> argTypes = parseTypeSignatures(')');
    1.96 +            Type returnType = parseTypeSignature();
    1.97 +            List<Type> throwsTypes = null;
    1.98 +            while (sigp < sig.length() && sig.charAt(sigp) == '^') {
    1.99 +                sigp++;
   1.100 +                if (throwsTypes == null)
   1.101 +                    throwsTypes = new ArrayList<Type>();
   1.102 +                throwsTypes.add(parseTypeSignature());
   1.103 +            }
   1.104 +            return new Type.MethodType(typeArgTypes, argTypes, returnType, throwsTypes);
   1.105 +        } else {
   1.106 +            Type t = parseTypeSignature();
   1.107 +            if (typeArgTypes == null && sigp == sig.length())
   1.108 +                return t;
   1.109 +            Type superclass = t;
   1.110 +            List<Type> superinterfaces = new ArrayList<Type>();
   1.111 +            while (sigp < sig.length())
   1.112 +                superinterfaces.add(parseTypeSignature());
   1.113 +            return new Type.ClassSigType(typeArgTypes, superclass, superinterfaces);
   1.114 +
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    private Type parseTypeSignature() {
   1.119 +        switch (sig.charAt(sigp)) {
   1.120 +            case 'B':
   1.121 +                sigp++;
   1.122 +                return new Type.SimpleType("byte");
   1.123 +
   1.124 +            case 'C':
   1.125 +                sigp++;
   1.126 +                return new Type.SimpleType("char");
   1.127 +
   1.128 +            case 'D':
   1.129 +                sigp++;
   1.130 +                return new Type.SimpleType("double");
   1.131 +
   1.132 +            case 'F':
   1.133 +                sigp++;
   1.134 +                return new Type.SimpleType("float");
   1.135 +
   1.136 +            case 'I':
   1.137 +                sigp++;
   1.138 +                return new Type.SimpleType("int");
   1.139 +
   1.140 +            case 'J':
   1.141 +                sigp++;
   1.142 +                return new Type.SimpleType("long");
   1.143 +
   1.144 +            case 'L':
   1.145 +                return parseClassTypeSignature();
   1.146 +
   1.147 +            case 'S':
   1.148 +                sigp++;
   1.149 +                return new Type.SimpleType("short");
   1.150 +
   1.151 +            case 'T':
   1.152 +                return parseTypeVariableSignature();
   1.153 +
   1.154 +            case 'V':
   1.155 +                sigp++;
   1.156 +                return new Type.SimpleType("void");
   1.157 +
   1.158 +            case 'Z':
   1.159 +                sigp++;
   1.160 +                return new Type.SimpleType("boolean");
   1.161 +
   1.162 +            case '[':
   1.163 +                sigp++;
   1.164 +                return new Type.ArrayType(parseTypeSignature());
   1.165 +
   1.166 +            case '*':
   1.167 +                sigp++;
   1.168 +                return new Type.WildcardType();
   1.169 +
   1.170 +            case '+':
   1.171 +                sigp++;
   1.172 +                return new Type.WildcardType("extends", parseTypeSignature());
   1.173 +
   1.174 +            case '-':
   1.175 +                sigp++;
   1.176 +                return new Type.WildcardType("super", parseTypeSignature());
   1.177 +
   1.178 +            default:
   1.179 +                throw new IllegalStateException(debugInfo());
   1.180 +        }
   1.181 +    }
   1.182 +
   1.183 +    private List<Type> parseTypeSignatures(char term) {
   1.184 +        sigp++;
   1.185 +        List<Type> types = new ArrayList<Type>();
   1.186 +        while (sig.charAt(sigp) != term)
   1.187 +            types.add(parseTypeSignature());
   1.188 +        sigp++;
   1.189 +        return types;
   1.190 +    }
   1.191 +
   1.192 +    private Type parseClassTypeSignature() {
   1.193 +        assert sig.charAt(sigp) == 'L';
   1.194 +        sigp++;
   1.195 +        return parseClassTypeSignatureRest();
   1.196 +    }
   1.197 +
   1.198 +    private Type parseClassTypeSignatureRest() {
   1.199 +        StringBuilder sb = new StringBuilder();
   1.200 +        Type t = null;
   1.201 +        char sigch;
   1.202 +        while (true) {
   1.203 +            switch  (sigch = sig.charAt(sigp)) {
   1.204 +                case '/':
   1.205 +                    sigp++;
   1.206 +                    sb.append(".");
   1.207 +                    break;
   1.208 +
   1.209 +                case '.':
   1.210 +                    sigp++;
   1.211 +                    if (t == null)
   1.212 +                        t = new Type.SimpleType(sb.toString());
   1.213 +                    return new Type.InnerClassType(t, parseClassTypeSignatureRest());
   1.214 +
   1.215 +                case ';':
   1.216 +                    sigp++;
   1.217 +                    if (t == null)
   1.218 +                        t = new Type.SimpleType(sb.toString());
   1.219 +                    return t;
   1.220 +
   1.221 +                case '<':
   1.222 +                    List<Type> argTypes = parseTypeSignatures('>');
   1.223 +                    t = new Type.ClassType(sb.toString(), argTypes);
   1.224 +                    break;
   1.225 +
   1.226 +                default:
   1.227 +                    sigp++;
   1.228 +                    sb.append(sigch);
   1.229 +                    break;
   1.230 +            }
   1.231 +        }
   1.232 +    }
   1.233 +
   1.234 +    private List<Type> parseTypeArgTypes() {
   1.235 +        assert sig.charAt(sigp) == '<';
   1.236 +        sigp++;
   1.237 +        List<Type> types = null;
   1.238 +        types = new ArrayList<Type>();
   1.239 +        while (sig.charAt(sigp) != '>')
   1.240 +            types.add(parseTypeArgType());
   1.241 +        sigp++;
   1.242 +        return types;
   1.243 +    }
   1.244 +
   1.245 +    private Type parseTypeArgType() {
   1.246 +        int sep = sig.indexOf(":", sigp);
   1.247 +        String name = sig.substring(sigp, sep);
   1.248 +        Type classBound = null;
   1.249 +        List<Type> interfaceBounds = null;
   1.250 +        sigp = sep + 1;
   1.251 +        if (sig.charAt(sigp) != ':')
   1.252 +            classBound = parseTypeSignature();
   1.253 +        while (sig.charAt(sigp) == ':') {
   1.254 +            sigp++;
   1.255 +            if (interfaceBounds == null)
   1.256 +                interfaceBounds = new ArrayList<Type>();
   1.257 +            interfaceBounds.add(parseTypeSignature());
   1.258 +        }
   1.259 +        return new Type.TypeArgType(name, classBound, interfaceBounds);
   1.260 +    }
   1.261 +
   1.262 +    private Type parseTypeVariableSignature() {
   1.263 +        sigp++;
   1.264 +        int sep = sig.indexOf(';', sigp);
   1.265 +        Type t = new Type.SimpleType(sig.substring(sigp, sep));
   1.266 +        sigp = sep + 1;
   1.267 +        return t;
   1.268 +    }
   1.269 +
   1.270 +    private String debugInfo() {
   1.271 +        return sig.substring(0, sigp) + "!" + sig.charAt(sigp) + "!" + sig.substring(sigp+1);
   1.272 +    }
   1.273 +
   1.274 +    private String sig;
   1.275 +    private int sigp;
   1.276 +
   1.277 +    private Type type;
   1.278 +}

mercurial