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
/*
 * %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.*;

/**
 * A utility for mangling java identifiers into C names.  Should make
 * this more fine grained and distribute the functionality to the
 * generators.
 *
 * @author  Sucheta Dambalkar(Revised)
 */
class Mangle {
    
    public static class Type {
    
    public static final int CLASS            = 1;
    public static final int FIELDSTUB        = 2;
    public static final int FIELD        = 3;
    public static final int JNI              = 4;
    public static final int SIGNATURE        = 5;
    public static final int METHOD_JDK_1     = 6;
    public static final int METHOD_JNI_SHORT = 7;
    public static final int METHOD_JNI_LONG  = 8;
    };
    
    
    public static final String mangle(String name, int mtype) { 
    StringBuffer result = new StringBuffer(100);
    int length = name.length();
    
    for (int i = 0; i < length; i++) {
        char ch = name.charAt(i);
        if (isalnum(ch)) {
        result.append(ch);
        } else if ((ch == '.') &&
               mtype == Mangle.Type.CLASS) {
        result.append('_');
        } else if (( ch == '$') &&
               mtype == Mangle.Type.CLASS) {
        result.append('_');
        result.append('_');
        } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) { 
        result.append('_');
        } else if (ch == '_' && mtype == Mangle.Type.CLASS) {
        result.append('_');
        } else if (mtype == Mangle.Type.JNI) {
        String esc = null;
        if (ch == '_')
            esc = "_1";
        else if (ch == '.')
            esc = "_";
        else if (ch == ';')
            esc = "_2";
        else if (ch == '[')
            esc = "_3";
        if (esc != null) {
            result.append(esc);
        } else {
            result.append(mangleChar(ch));
        }
        } else if (mtype == Mangle.Type.SIGNATURE) {
        if (isprint(ch)) {
            result.append(ch);
        } else {
            result.append(mangleChar(ch));
        }
        } else {
        result.append(mangleChar(ch));
        }
    }
    
    return result.toString();
    }
    
    public static String mangleMethod(MethodDoc method, RootDoc root, ClassDoc clazz, 
                      int mtype) {
    StringBuffer result = new StringBuffer(100);
    result.append("Java_");
    
    if (mtype == Mangle.Type.METHOD_JDK_1) {
        result.append(mangle(clazz.qualifiedName(), Mangle.Type.CLASS));
        result.append('_');
        result.append(mangle(method.name(), 
                 Mangle.Type.FIELD));
        result.append("_stub");
        return result.toString();
    }
    
    /* JNI */
    result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI));
    result.append('_');
    result.append(mangle(method.name(),
                 Mangle.Type.JNI));
    if (mtype == Mangle.Type.METHOD_JNI_LONG) {
        result.append("__");
        String typesig = method.signature();
        TypeSignature newTypeSig = new TypeSignature(root);
        String sig = newTypeSig.getTypeSignature(typesig,  method.returnType());
        sig = sig.substring(1);
        sig = sig.substring(0, sig.lastIndexOf(')'));
        sig = sig.replace('/', '.');
        result.append(mangle(sig, Mangle.Type.JNI));
    }
    
    return result.toString();
    }
    //where
        private static String getInnerQualifiedName(ClassDoc clazz) {
        ClassDoc encl = clazz.containingClass();
        if (encl == null)
        return clazz.qualifiedName();
        else
        return getInnerQualifiedName(encl) + '$' + clazz.simpleTypeName();
    }
    
    public static final String mangleChar(char ch) {
    String s = Integer.toHexString(ch);
    int nzeros = 5 - s.length();
    char[] result = new char[6];
    result[0] = '_';
    for (int i = 1; i <= nzeros; i++)
        result[i] = '0';
    for (int i = nzeros+1, j = 0; i < 6; i++, j++)
        result[i] = s.charAt(j);
    return new String(result);
    }
    
    /* Warning: Intentional ASCII operation. */
    private static final boolean isalnum(char ch) {
    return ch <= 0x7f && /* quick test */
        ((ch >= 'A' && ch <= 'Z') ||
         (ch >= 'a' && ch <= 'z') ||
         (ch >= '0' && ch <= '9'));
    }
    
    /* Warning: Intentional ASCII operation. */
    private static final boolean isprint(char ch) {
    return ch >= 32 && ch <= 126;
    }
}
			
			

Browsed Source: [clear]