summaryrefslogtreecommitdiffstats
path: root/build/annotationProcessors/utils/Utils.java
blob: aea88cfbf38307b6379b123fdab1d6063f9fc591 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.annotationProcessors.utils;

import org.mozilla.gecko.annotationProcessors.AnnotationInfo;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.HashMap;

/**
 * A collection of utility methods used by CodeGenerator. Largely used for translating types.
 */
public class Utils {

    // A collection of lookup tables to simplify the functions to follow...
    private static final HashMap<String, String> NATIVE_TYPES = new HashMap<String, String>();

    static {
        NATIVE_TYPES.put("void", "void");
        NATIVE_TYPES.put("boolean", "bool");
        NATIVE_TYPES.put("byte", "int8_t");
        NATIVE_TYPES.put("char", "char16_t");
        NATIVE_TYPES.put("short", "int16_t");
        NATIVE_TYPES.put("int", "int32_t");
        NATIVE_TYPES.put("long", "int64_t");
        NATIVE_TYPES.put("float", "float");
        NATIVE_TYPES.put("double", "double");
    }

    private static final HashMap<String, String> NATIVE_ARRAY_TYPES = new HashMap<String, String>();

    static {
        NATIVE_ARRAY_TYPES.put("boolean", "mozilla::jni::BooleanArray");
        NATIVE_ARRAY_TYPES.put("byte", "mozilla::jni::ByteArray");
        NATIVE_ARRAY_TYPES.put("char", "mozilla::jni::CharArray");
        NATIVE_ARRAY_TYPES.put("short", "mozilla::jni::ShortArray");
        NATIVE_ARRAY_TYPES.put("int", "mozilla::jni::IntArray");
        NATIVE_ARRAY_TYPES.put("long", "mozilla::jni::LongArray");
        NATIVE_ARRAY_TYPES.put("float", "mozilla::jni::FloatArray");
        NATIVE_ARRAY_TYPES.put("double", "mozilla::jni::DoubleArray");
    }

    private static final HashMap<String, String> CLASS_DESCRIPTORS = new HashMap<String, String>();

    static {
        CLASS_DESCRIPTORS.put("void", "V");
        CLASS_DESCRIPTORS.put("boolean", "Z");
        CLASS_DESCRIPTORS.put("byte", "B");
        CLASS_DESCRIPTORS.put("char", "C");
        CLASS_DESCRIPTORS.put("short", "S");
        CLASS_DESCRIPTORS.put("int", "I");
        CLASS_DESCRIPTORS.put("long", "J");
        CLASS_DESCRIPTORS.put("float", "F");
        CLASS_DESCRIPTORS.put("double", "D");
    }

    /**
     * Get the C++ type corresponding to the provided type parameter.
     *
     * @param type Class to determine the corresponding JNI type for.
     * @return C++ type as a String
     */
    public static String getNativeParameterType(Class<?> type, AnnotationInfo info) {
        final String name = type.getName().replace('.', '/');

        if (NATIVE_TYPES.containsKey(name)) {
            return NATIVE_TYPES.get(name);
        }

        if (type.isArray()) {
            final String compName = type.getComponentType().getName();
            if (NATIVE_ARRAY_TYPES.containsKey(compName)) {
                return NATIVE_ARRAY_TYPES.get(compName) + "::Param";
            }
            return "mozilla::jni::ObjectArray::Param";
        }

        if (type.equals(String.class) || type.equals(CharSequence.class)) {
            return "mozilla::jni::String::Param";
        }

        if (type.equals(Class.class)) {
            // You're doing reflection on Java objects from inside C, returning Class objects
            // to C, generating the corresponding code using this Java program. Really?!
            return "mozilla::jni::Class::Param";
        }

        if (type.equals(Throwable.class)) {
            return "mozilla::jni::Throwable::Param";
        }

        if (type.equals(ByteBuffer.class)) {
            return "mozilla::jni::ByteBuffer::Param";
        }

        return "mozilla::jni::Object::Param";
    }

    public static String getNativeReturnType(Class<?> type, AnnotationInfo info) {
        final String name = type.getName().replace('.', '/');

        if (NATIVE_TYPES.containsKey(name)) {
            return NATIVE_TYPES.get(name);
        }

        if (type.isArray()) {
            final String compName = type.getComponentType().getName();
            if (NATIVE_ARRAY_TYPES.containsKey(compName)) {
                return NATIVE_ARRAY_TYPES.get(compName) + "::LocalRef";
            }
            return "mozilla::jni::ObjectArray::LocalRef";
        }

        if (type.equals(String.class)) {
            return "mozilla::jni::String::LocalRef";
        }

        if (type.equals(Class.class)) {
            // You're doing reflection on Java objects from inside C, returning Class objects
            // to C, generating the corresponding code using this Java program. Really?!
            return "mozilla::jni::Class::LocalRef";
        }

        if (type.equals(Throwable.class)) {
            return "mozilla::jni::Throwable::LocalRef";
        }

        if (type.equals(ByteBuffer.class)) {
            return "mozilla::jni::ByteBuffer::LocalRef";
        }

        return "mozilla::jni::Object::LocalRef";
    }

    /**
     * Get the JNI class descriptor corresponding to the provided type parameter.
     *
     * @param type Class to determine the corresponding JNI descriptor for.
     * @return Class descripor as a String
     */
    public static String getClassDescriptor(Class<?> type) {
        final String name = type.getName().replace('.', '/');

        if (CLASS_DESCRIPTORS.containsKey(name)) {
            return CLASS_DESCRIPTORS.get(name);
        }

        if (type.isArray()) {
            // Array names are already in class descriptor form.
            return name;
        }

        return "L" + name + ';';
    }

    /**
     * Get the JNI signaure for a member.
     *
     * @param member Member to get the signature for.
     * @return JNI signature as a string
     */
    public static String getSignature(Member member) {
        return member instanceof Field ?  getSignature((Field) member) :
               member instanceof Method ? getSignature((Method) member) :
                                          getSignature((Constructor<?>) member);
    }

    /**
     * Get the JNI signaure for a field.
     *
     * @param member Field to get the signature for.
     * @return JNI signature as a string
     */
    public static String getSignature(Field member) {
        return getClassDescriptor(member.getType());
    }

    private static String getSignature(Class<?>[] args, Class<?> ret) {
        final StringBuilder sig = new StringBuilder("(");
        for (int i = 0; i < args.length; i++) {
            sig.append(getClassDescriptor(args[i]));
        }
        return sig.append(')').append(getClassDescriptor(ret)).toString();
    }

    /**
     * Get the JNI signaure for a method.
     *
     * @param member Method to get the signature for.
     * @return JNI signature as a string
     */
    public static String getSignature(Method member) {
        return getSignature(member.getParameterTypes(), member.getReturnType());
    }

    /**
     * Get the JNI signaure for a constructor.
     *
     * @param member Constructor to get the signature for.
     * @return JNI signature as a string
     */
    public static String getSignature(Constructor<?> member) {
        return getSignature(member.getParameterTypes(), void.class);
    }

    /**
     * Get the C++ name for a member.
     *
     * @param member Member to get the name for.
     * @return JNI name as a string
     */
    public static String getNativeName(Member member) {
        final String name = getMemberName(member);
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    /**
     * Get the C++ name for a member.
     *
     * @param member Member to get the name for.
     * @return JNI name as a string
     */
    public static String getNativeName(Class<?> clz) {
        final String name = clz.getName();
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    /**
     * Get the C++ name for a member.
     *
     * @param member Member to get the name for.
     * @return JNI name as a string
     */
    public static String getNativeName(AnnotatedElement element) {
        if (element instanceof Class<?>) {
            return getNativeName((Class<?>)element);
        } else if (element instanceof Member) {
            return getNativeName((Member)element);
        } else {
            return null;
        }
    }

    /**
     * Get the JNI name for a member.
     *
     * @param member Member to get the name for.
     * @return JNI name as a string
     */
    public static String getMemberName(Member member) {
        if (member instanceof Constructor) {
            return "<init>";
        }
        return member.getName();
    }

    public static String getUnqualifiedName(String name) {
        return name.substring(name.lastIndexOf(':') + 1);
    }

    /**
     * Determine if a member is declared static.
     *
     * @param member The Member to check.
     * @return true if the member is declared static, false otherwise.
     */
    public static boolean isStatic(final Member member) {
        return Modifier.isStatic(member.getModifiers());
    }

    /**
     * Determine if a member is declared final.
     *
     * @param member The Member to check.
     * @return true if the member is declared final, false otherwise.
     */
    public static boolean isFinal(final Member member) {
        return Modifier.isFinal(member.getModifiers());
    }
}