summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/libcore/reflect/InternalNames.java
blob: b9e65cb1d508fd15bb21d0cadedafee7e2459007 (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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.reflect;

import java.lang.reflect.Array;

/**
 * Work with a type's internal name like "V" or "Ljava/lang/String;".
 */
public final class InternalNames {
    private InternalNames() {
    }

    public static Class<?> getClass(ClassLoader classLoader, String internalName) {
        if (internalName.startsWith("[")) {
            Class<?> componentClass = getClass(classLoader, internalName.substring(1));
            return Array.newInstance(componentClass, 0).getClass();
        } else if (internalName.equals("Z")) {
            return boolean.class;
        } else if (internalName.equals("B")) {
            return byte.class;
        } else if (internalName.equals("S")) {
            return short.class;
        } else if (internalName.equals("I")) {
            return int.class;
        } else if (internalName.equals("J")) {
            return long.class;
        } else if (internalName.equals("F")) {
            return float.class;
        } else if (internalName.equals("D")) {
            return double.class;
        } else if (internalName.equals("C")) {
            return char.class;
        } else if (internalName.equals("V")) {
            return void.class;
        } else {
            String name = internalName.substring(1, internalName.length() - 1).replace('/', '.');
            try {
                return classLoader.loadClass(name);
            } catch (ClassNotFoundException e) {
                NoClassDefFoundError error = new NoClassDefFoundError(name);
                error.initCause(e);
                throw error;
            }
        }
    }

    public static String getInternalName(Class<?> c) {
        if (c.isArray()) {
            return '[' + getInternalName(c.getComponentType());
        } else if (c == boolean.class) {
            return "Z";
        } else if (c == byte.class) {
            return "B";
        } else if (c == short.class) {
            return "S";
        } else if (c == int.class) {
            return "I";
        } else if (c == long.class) {
            return "J";
        } else if (c == float.class) {
            return "F";
        } else if (c == double.class) {
            return "D";
        } else if (c == char.class) {
            return "C";
        } else if (c == void.class) {
            return "V";
        } else {
            return 'L' + c.getName().replace('.', '/') + ';';
        }
    }
}