langtools/src/share/classes/com/sun/tools/javah/TypeSignature.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 10 06bc494ca11e
child 3996 dc676a9093b3
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2003 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
package com.sun.tools.javah;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * Returns internal type signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * @author Sucheta Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
public class TypeSignature{
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    RootDoc root  = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    /* Signature Characters */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private static final String SIG_VOID                   = "V";
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private static final String SIG_BOOLEAN                = "Z";
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private static final String SIG_BYTE                   = "B";
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private static final String SIG_CHAR                   = "C";
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private static final String SIG_SHORT                  = "S";
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private static final String SIG_INT                    = "I";
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private static final String SIG_LONG                   = "J";
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private static final String SIG_FLOAT                  = "F";
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private static final String SIG_DOUBLE                 = "D";
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private static final String SIG_ARRAY                  = "[";
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private static final String SIG_CLASS                  = "L";
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    public TypeSignature(RootDoc root){
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        this.root = root;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * Returns the type signature of a field according to JVM specs
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public String getTypeSignature(String javasignature){
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        return getParamJVMSignature(javasignature);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Returns the type signature of a method according to JVM specs
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public String getTypeSignature(String javasignature, Type returnType){
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        String signature = null; //Java type signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        String typeSignature = null; //Internal type signature.
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    77
        Vector<Object> params = new Vector<Object>(); //List of parameters.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        String paramsig = null; //Java parameter signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        String paramJVMSig = null; //Internal parameter signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        String returnSig = null; //Java return type signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        String returnJVMType = null; //Internal return type signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        String dimension = null; //Array dimension.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        int startIndex = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        int endIndex = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        StringTokenizer st = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        // Gets the actual java signature without parentheses.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        if(javasignature != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            startIndex = javasignature.indexOf("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            endIndex = javasignature.indexOf(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        if(((startIndex != -1) && (endIndex != -1))
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
           &&(startIndex+1 < javasignature.length())
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
           &&(endIndex < javasignature.length())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            signature = javasignature.substring(startIndex+1, endIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        // Separates parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        if(signature != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            if(signature.indexOf(",") != -1){
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                st = new StringTokenizer(signature, ",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                if(st != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                    while (st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                        params.add(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            }else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                params.add(signature);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        /* JVM type signature. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        typeSignature = "(";
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        // Gets indivisual internal parameter signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        while(params.isEmpty() != true){
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            paramsig =((String)params.remove(i)).trim();
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            paramJVMSig  = getParamJVMSignature(paramsig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            if(paramJVMSig != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                typeSignature += paramJVMSig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        typeSignature += ")";
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        // Get internal return type signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        returnJVMType = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        if(returnType != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            dimension = returnType.dimension();
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        if(dimension != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            //Gets array dimension of return type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            while(dimension.indexOf("[]") != -1){
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                returnJVMType += "[";
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                int stindex = dimension.indexOf("]") + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                if(stindex <= dimension.length()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                    dimension = dimension.substring(stindex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                }else dimension = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        if(returnType != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            returnSig = returnType.qualifiedTypeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            returnJVMType += getComponentType(returnSig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            System.out.println("Invalid return type.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        typeSignature += returnJVMType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        return typeSignature;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * Returns internal signature of a parameter.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    private String getParamJVMSignature(String paramsig){
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        String paramJVMSig = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        String componentType ="";
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        if(paramsig != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            if(paramsig.indexOf("[]") != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                // Gets array dimension.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                int endindex = paramsig.indexOf("[]");
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                componentType = paramsig.substring(0, endindex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                String dimensionString =  paramsig.substring(endindex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                if(dimensionString != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                    while(dimensionString.indexOf("[]") != -1){
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                        paramJVMSig += "[";
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                        int beginindex = dimensionString.indexOf("]") + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                        if(beginindex < dimensionString.length()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                            dimensionString = dimensionString.substring(beginindex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                        }else
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                            dimensionString = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            } else componentType = paramsig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            paramJVMSig += getComponentType(componentType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        return paramJVMSig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     * Returns internal signature of a component.
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    private String getComponentType(String componentType){
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        String JVMSig = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        if(componentType != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            if(componentType.equals("void")) JVMSig += SIG_VOID ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            else if(componentType.equals("boolean"))  JVMSig += SIG_BOOLEAN ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            else if(componentType.equals("byte")) JVMSig += SIG_BYTE ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            else if(componentType.equals("char"))  JVMSig += SIG_CHAR ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            else if(componentType.equals("short"))  JVMSig += SIG_SHORT ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            else if(componentType.equals("int"))  JVMSig += SIG_INT ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            else if(componentType.equals("long"))  JVMSig += SIG_LONG ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            else if(componentType.equals("float")) JVMSig += SIG_FLOAT ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            else if(componentType.equals("double"))  JVMSig += SIG_DOUBLE ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                if(!componentType.equals("")){
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                    ClassDoc classNameDoc = root.classNamed(componentType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                    if(classNameDoc == null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                        System.out.println("Invalid class type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                    }else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                        String classname = classNameDoc.qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                        String newclassname = classname.replace('.', '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                        JVMSig += "L";
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                        JVMSig += newclassname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                        JVMSig += ";";
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        return JVMSig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
}