langtools/src/share/classes/sun/tools/javap/TypeSignature.java
changeset 2987 2e0ca9f4893d
parent 2986 5370c4ae4f6f
child 2988 094272984b53
equal deleted inserted replaced
2986:5370c4ae4f6f 2987:2e0ca9f4893d
     1 /*
       
     2  * Copyright 2002-2008 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  */
       
    25 
       
    26 
       
    27 package sun.tools.javap;
       
    28 
       
    29 import java.util.*;
       
    30 import java.io.*;
       
    31 
       
    32 /**
       
    33  * Returns java type signature.
       
    34  *
       
    35  * @author  Sucheta Dambalkar
       
    36  */
       
    37 public class TypeSignature {
       
    38 
       
    39     String parameters = null;
       
    40     String returntype = null;
       
    41     String fieldtype = null;
       
    42     int argumentlength = 0;
       
    43 
       
    44     public TypeSignature(String JVMSignature){
       
    45 
       
    46         if(JVMSignature != null){
       
    47             if(JVMSignature.indexOf("(") == -1){
       
    48                 //This is a field type.
       
    49                 this.fieldtype = getFieldTypeSignature(JVMSignature);
       
    50             }else {
       
    51                 String parameterdes = null;
       
    52                 if((JVMSignature.indexOf(")")-1) > (JVMSignature.indexOf("("))){
       
    53                     //Get parameter signature.
       
    54                     parameterdes =
       
    55                         JVMSignature.substring(JVMSignature.indexOf("(")+1,
       
    56                                                JVMSignature.indexOf(")"));
       
    57                     this.parameters = getParametersHelper(parameterdes);
       
    58                 }else this.parameters = "()";
       
    59                 //Get return type signature.
       
    60                 String returndes = JVMSignature.substring(JVMSignature.lastIndexOf(")")+1);
       
    61                 this.returntype = getReturnTypeHelper(returndes);
       
    62             }
       
    63         }
       
    64     }
       
    65 
       
    66     /**
       
    67      * Returns java type signature of a field.
       
    68      */
       
    69     public String getFieldTypeSignature(String fielddes){
       
    70         if(fielddes.startsWith("L")){
       
    71             return(getObjectType(fielddes));
       
    72         }else if(fielddes.startsWith("[")){
       
    73             return(getArrayType(fielddes));
       
    74         }else
       
    75             return(getBaseType(fielddes));
       
    76     }
       
    77 
       
    78     /**
       
    79      * Returns java type signature of a parameter.
       
    80      */
       
    81     public String getParametersHelper(String parameterdes){
       
    82         Vector<String> parameters = new Vector<String>();
       
    83         int startindex = -1;
       
    84         int endindex = -1;
       
    85         String param = "";
       
    86 
       
    87         while(parameterdes != null){
       
    88 
       
    89             if(parameterdes.startsWith("L")){
       
    90                 //parameter is a object.
       
    91                 startindex = parameterdes.indexOf("L");
       
    92                 endindex = parameterdes.indexOf(";");
       
    93                 if(startindex < parameterdes.length()) {
       
    94                     if(endindex == parameterdes.length()-1) {
       
    95                         //last parameter
       
    96                         param = parameterdes.substring(startindex);
       
    97                         parameterdes = null;
       
    98                     }else if(endindex+1 < parameterdes.length()){
       
    99                         //rest parameters
       
   100                         param = parameterdes.substring(startindex, endindex+1);
       
   101                         parameterdes = parameterdes.substring(endindex+1);
       
   102 
       
   103                     }
       
   104                     parameters.add(getObjectType(param));
       
   105                 }
       
   106             }else if(parameterdes.startsWith("[")){
       
   107                 //parameter is an array.
       
   108                 String componentType = "";
       
   109                 int enddim = -1;
       
   110                 int st = 0;
       
   111                 while(true){
       
   112                     if(st < parameterdes.length()){
       
   113                         if(parameterdes.charAt(st) == '['){
       
   114 
       
   115                             enddim = st;
       
   116                             st++;
       
   117                         }
       
   118                         else break;
       
   119                     }
       
   120                     else break;
       
   121                 }
       
   122 
       
   123                 if(enddim+1 < parameterdes.length()){
       
   124                     /* Array dimension.*/
       
   125                     param = parameterdes.substring(0,enddim+1);
       
   126 
       
   127                 }
       
   128 
       
   129                 int stotherparam = param.lastIndexOf("[")+1;
       
   130 
       
   131                 if(stotherparam < parameterdes.length()){
       
   132                     componentType =  parameterdes.substring(stotherparam);
       
   133                 }
       
   134 
       
   135                 if(componentType.startsWith("L")){
       
   136                     //parameter is array of objects.
       
   137                     startindex = parameterdes.indexOf("L");
       
   138                     endindex = parameterdes.indexOf(";");
       
   139 
       
   140                     if(endindex ==  parameterdes.length()-1){
       
   141                         //last parameter
       
   142                         param += parameterdes.substring(startindex);
       
   143                         parameterdes = null;
       
   144                     }else if(endindex+1 <  parameterdes.length()){
       
   145                         //rest parameters
       
   146                         param += parameterdes.substring(startindex, endindex+1);
       
   147                         parameterdes = parameterdes.substring(endindex+1);
       
   148                     }
       
   149                 }else{
       
   150                     //parameter is array of base type.
       
   151                     if(componentType.length() == 1){
       
   152                         //last parameter.
       
   153                         param += componentType;
       
   154                         parameterdes = null;
       
   155                     }
       
   156                     else if (componentType.length() > 1) {
       
   157                         //rest parameters.
       
   158                         param += componentType.substring(0,1);
       
   159                         parameterdes = componentType.substring(1);
       
   160                     }
       
   161                 }
       
   162                 parameters.add(getArrayType(param));
       
   163 
       
   164 
       
   165             }else {
       
   166 
       
   167                 //parameter is of base type.
       
   168                 if(parameterdes.length() == 1){
       
   169                     //last parameter
       
   170                     param = parameterdes;
       
   171                     parameterdes = null;
       
   172                 }
       
   173                 else if (parameterdes.length() > 1) {
       
   174                     //rest parameters.
       
   175                     param = parameterdes.substring(0,1);
       
   176                     parameterdes = parameterdes.substring(1);
       
   177                 }
       
   178                 parameters.add(getBaseType(param));
       
   179             }
       
   180         }
       
   181 
       
   182         /* number of arguments of a method.*/
       
   183         argumentlength =  parameters.size();
       
   184 
       
   185         /* java type signature.*/
       
   186         String parametersignature = "(";
       
   187         int i;
       
   188 
       
   189         for(i = 0; i < parameters.size(); i++){
       
   190             parametersignature += parameters.elementAt(i);
       
   191             if(i != parameters.size()-1){
       
   192                 parametersignature += ", ";
       
   193             }
       
   194         }
       
   195         parametersignature += ")";
       
   196         return parametersignature;
       
   197     }
       
   198 
       
   199     /**
       
   200      * Returns java type signature for a return type.
       
   201      */
       
   202     public String getReturnTypeHelper(String returndes){
       
   203         return getFieldTypeSignature(returndes);
       
   204     }
       
   205 
       
   206     /**
       
   207      * Returns java type signature for a base type.
       
   208      */
       
   209     public String getBaseType(String baseType){
       
   210         if(baseType != null){
       
   211             if(baseType.equals("B")) return "byte";
       
   212             else if(baseType.equals("C")) return "char";
       
   213             else if(baseType.equals("D")) return "double";
       
   214             else if(baseType.equals("F")) return "float";
       
   215             else if(baseType.equals("I")) return "int";
       
   216             else if(baseType.equals("J")) return "long";
       
   217             else if(baseType.equals("S")) return "short";
       
   218             else if(baseType.equals("Z")) return "boolean";
       
   219             else if(baseType.equals("V")) return "void";
       
   220         }
       
   221         return null;
       
   222     }
       
   223 
       
   224     /**
       
   225      * Returns java type signature for a object type.
       
   226      */
       
   227     public String getObjectType(String JVMobjectType) {
       
   228         String objectType = "";
       
   229         int startindex = JVMobjectType.indexOf("L")+1;
       
   230         int endindex =  JVMobjectType.indexOf(";");
       
   231         if((startindex != -1) && (endindex != -1)){
       
   232             if((startindex < JVMobjectType.length()) && (endindex < JVMobjectType.length())){
       
   233                 objectType = JVMobjectType.substring(startindex, endindex);
       
   234             }
       
   235             objectType = objectType.replace('/','.');
       
   236             return objectType;
       
   237         }
       
   238         return null;
       
   239     }
       
   240 
       
   241     /**
       
   242      * Returns java type signature for array type.
       
   243      */
       
   244     public String getArrayType(String arrayType) {
       
   245         if(arrayType != null){
       
   246             String dimention = "";
       
   247 
       
   248             while(arrayType.indexOf("[") != -1){
       
   249                 dimention += "[]";
       
   250 
       
   251                 int startindex = arrayType.indexOf("[")+1;
       
   252                 if(startindex <= arrayType.length()){
       
   253                 arrayType = arrayType.substring(startindex);
       
   254                 }
       
   255             }
       
   256 
       
   257             String componentType = "";
       
   258             if(arrayType.startsWith("L")){
       
   259                 componentType = getObjectType(arrayType);
       
   260             }else {
       
   261                 componentType = getBaseType(arrayType);
       
   262             }
       
   263             return componentType+dimention;
       
   264         }
       
   265         return null;
       
   266     }
       
   267 
       
   268     /**
       
   269      * Returns java type signature for parameters.
       
   270      */
       
   271      public String getParameters(){
       
   272         return parameters;
       
   273     }
       
   274 
       
   275     /**
       
   276      * Returns java type signature for return type.
       
   277      */
       
   278     public String getReturnType(){
       
   279         return returntype;
       
   280     }
       
   281 
       
   282     /**
       
   283      * Returns java type signature for field type.
       
   284      */
       
   285     public String getFieldType(){
       
   286         return fieldtype;
       
   287     }
       
   288 
       
   289     /**
       
   290      * Return number of arguments of a method.
       
   291      */
       
   292     public int getArgumentlength(){
       
   293         return argumentlength;
       
   294     }
       
   295 }