1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */


package com.sun.tools.javah;

import com.sun.javadoc.*;
import java.io.*;
import java.util.*;

/**
 * Returns internal type signature.
 *
 * @author Sucheta Dambalkar
 */

public class TypeSignature{
    
    RootDoc root  = null;
    
    /* Signature Characters */
    
    private static final String SIG_VOID                   = "V";
    private static final String SIG_BOOLEAN                = "Z";
    private static final String SIG_BYTE                   = "B";
    private static final String SIG_CHAR                   = "C";
    private static final String SIG_SHORT                  = "S";
    private static final String SIG_INT                    = "I";
    private static final String SIG_LONG                   = "J";
    private static final String SIG_FLOAT                  = "F";
    private static final String SIG_DOUBLE                 = "D";
    private static final String SIG_ARRAY                  = "[";
    private static final String SIG_CLASS                  = "L";
    
    
    
    public TypeSignature(RootDoc root){
    this.root = root;
    }
    
    /*
     * Returns the type signature of a field according to JVM specs
     */
    public String getTypeSignature(String javasignature){
    return getParamJVMSignature(javasignature);
    }
    
    /*
     * Returns the type signature of a method according to JVM specs
     */
    public String getTypeSignature(String javasignature, Type returnType){
    
    String signature = null; //Java type signature.
    String typeSignature = null; //Internal type signature.
    Vector params = new Vector(); //List of parameters.
    String paramsig = null; //Java parameter signature.
    String paramJVMSig = null; //Internal parameter signature.
    String returnSig = null; //Java return type signature.
    String returnJVMType = null; //Internal return type signature.
    String dimension = null; //Array dimension.
    
    int startIndex = -1;
    int endIndex = -1;
    StringTokenizer st = null;
    int i = 0;
    
    // Gets the actual java signature without parentheses.
    if(javasignature != null){
        startIndex = javasignature.indexOf("(");
        endIndex = javasignature.indexOf(")");
    }
    
    if(((startIndex != -1) && (endIndex != -1))
       &&(startIndex+1 < javasignature.length())
       &&(endIndex < javasignature.length())) {
        
        signature = javasignature.substring(startIndex+1, endIndex);
    }
    
    // Separates parameters.
    if(signature != null){
        if(signature.indexOf(",") != -1){
        st = new StringTokenizer(signature, ",");
        if(st != null){
            while (st.hasMoreTokens()) {
            params.add(st.nextToken());
            }
        }
        }else {
        params.add(signature);
        }
    }
    
    /* JVM type signature. */
    typeSignature = "(";
    
    // Gets indivisual internal parameter signature. 
    while(params.isEmpty() != true){
        paramsig =((String)params.remove(i)).trim();
        paramJVMSig  = getParamJVMSignature(paramsig);
        if(paramJVMSig != null){
        typeSignature += paramJVMSig;
        }
    }
    
    typeSignature += ")";
    
    // Get internal return type signature.
    
    returnJVMType = "";
    if(returnType != null){
        dimension = returnType.dimension();
    }
    
    if(dimension != null){
        
        //Gets array dimension of return type.
        while(dimension.indexOf("[]") != -1){
        returnJVMType += "[";
        int stindex = dimension.indexOf("]") + 1;
        if(stindex <= dimension.length()){
            dimension = dimension.substring(stindex);
        }else dimension = "";
        }
    }
    if(returnType != null){
        returnSig = returnType.qualifiedTypeName();
        returnJVMType += getComponentType(returnSig);
    }else {
        System.out.println("Invalid return type.");
    }
    
    typeSignature += returnJVMType;
    return typeSignature;
    }
    
    /*
     * Returns internal signature of a parameter.
     */
    private String getParamJVMSignature(String paramsig){
    String paramJVMSig = "";
    String componentType ="";
    
    if(paramsig != null){
        
        if(paramsig.indexOf("[]") != -1) {
        // Gets array dimension. 
        int endindex = paramsig.indexOf("[]");
        componentType = paramsig.substring(0, endindex);
        String dimensionString =  paramsig.substring(endindex);
        if(dimensionString != null){
            while(dimensionString.indexOf("[]") != -1){
            paramJVMSig += "[";
            int beginindex = dimensionString.indexOf("]") + 1;
            if(beginindex < dimensionString.length()){
                dimensionString = dimensionString.substring(beginindex);
            }else 
                dimensionString = "";
            }
        }
        } else componentType = paramsig;
        
        paramJVMSig += getComponentType(componentType);
    }
    return paramJVMSig;
    }
    
    /*
     * Returns internal signature of a component.
     */
    private String getComponentType(String componentType){
    
    String JVMSig = "";
    
    if(componentType != null){
        if(componentType.equals("void")) JVMSig += SIG_VOID ;
        else if(componentType.equals("boolean"))  JVMSig += SIG_BOOLEAN ;
        else if(componentType.equals("byte")) JVMSig += SIG_BYTE ;
        else if(componentType.equals("char"))  JVMSig += SIG_CHAR ;
        else if(componentType.equals("short"))  JVMSig += SIG_SHORT ;
        else if(componentType.equals("int"))  JVMSig += SIG_INT ;
        else if(componentType.equals("long"))  JVMSig += SIG_LONG ;
        else if(componentType.equals("float")) JVMSig += SIG_FLOAT ;
        else if(componentType.equals("double"))  JVMSig += SIG_DOUBLE ;
        else {
        if(!componentType.equals("")){
            ClassDoc classNameDoc = root.classNamed(componentType);
            
            if(classNameDoc == null){
            System.out.println("Invalid class type");
            }else {
            String classname = classNameDoc.qualifiedName();
            String newclassname = classname.replace('.', '/');
            JVMSig += "L";
            JVMSig += newclassname;
            JVMSig += ";";
            }
        }
        }
    }
    return JVMSig;
    }
}
			
			

Browsed Source: [clear]