src/share/classes/com/sun/tools/javah/TypeSignature.java

Thu, 24 Jul 2008 19:06:57 +0100

author
mcimadamore
date
Thu, 24 Jul 2008 19:06:57 +0100
changeset 80
5c9cdeb740f2
parent 1
9a66ca7c79fa
child 184
905e151a185a
permissions
-rw-r--r--

6717241: some diagnostic argument is prematurely converted into a String object
Summary: removed early toString() conversions applied to diagnostic arguments
Reviewed-by: jjg

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

mercurial