summaryrefslogtreecommitdiff
path: root/deps/v8/build/android/bytecode/java/org/chromium/bytecode/TypeUtils.java
blob: ed2dc2dc24badd46e94c65a0df8c0282c604de25 (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 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.bytecode;

import org.objectweb.asm.Type;

import java.util.HashMap;
import java.util.Map;

/**
 * Utility methods for accessing {@link Type}s Strings.
 *
 * Useful definitions to keep in mind when using this class:
 * Internal name - The fully qualified name for a type with dots replaced by slashes. Not really
 * relevant for primitive types.
 * Type descriptor - Single letters for primitive types, "L" + internal name + ";" for class types.
 *
 * The methods in this class accept internal names or primitive type descriptors.
 */
class TypeUtils {
    static final String ASSERTION_ERROR = "java/lang/AssertionError";
    static final String ASSET_MANAGER = "android/content/res/AssetManager";
    static final String BUILD_HOOKS = "org/chromium/build/BuildHooks";
    static final String BUILD_HOOKS_ANDROID = "org/chromium/build/BuildHooksAndroid";
    static final String CONFIGURATION = "android/content/res/Configuration";
    static final String CONTEXT = "android/content/Context";
    static final String CONTEXT_WRAPPER = "android/content/ContextWrapper";
    static final String RESOURCES = "android/content/res/Resources";
    static final String STRING = "java/lang/String";
    static final String THEME = "android/content/res/Resources$Theme";

    static final String BOOLEAN = "Z";
    static final String INT = "I";
    static final String VOID = "V";
    private static final Map<String, Type> PRIMITIVE_DESCRIPTORS;
    static {
        PRIMITIVE_DESCRIPTORS = new HashMap<>();
        PRIMITIVE_DESCRIPTORS.put(Type.BOOLEAN_TYPE.toString(), Type.BOOLEAN_TYPE);
        PRIMITIVE_DESCRIPTORS.put(Type.INT_TYPE.toString(), Type.INT_TYPE);
        PRIMITIVE_DESCRIPTORS.put(Type.VOID_TYPE.toString(), Type.VOID_TYPE);
    }

    /**
     * Returns the full method signature with internal names.
     *
     * @param methodName Name of the method (ex. "getResources").
     * @param returnType Internal name for the return type.
     * @param argumentTypes List of internal names for argument types.
     * @return String representation of the method signature.
     */
    static String getMethodSignature(
            String methodName, String returnType, String... argumentTypes) {
        return methodName + getMethodDescriptor(returnType, argumentTypes);
    }

    /**
     * Builds a method descriptor suitable for use with {@link org.objectweb.asm.MethodVisitor}.
     *
     * @param returnType Internal name for the return type of the method (primitive or class).
     * @param argumentTypes Internal names for the argument types (primitive or class).
     * @return The generated method descriptor.
     */
    static String getMethodDescriptor(String returnType, String... argumentTypes) {
        Type[] typedArguments = new Type[argumentTypes.length];
        for (int i = 0; i < argumentTypes.length; ++i) {
            // Argument list should be empty in this case, not V (void).
            assert !Type.VOID_TYPE.toString().equals(argumentTypes[i]);
            typedArguments[i] = convert(argumentTypes[i]);
        }
        return Type.getMethodDescriptor(convert(returnType), typedArguments);
    }

    /**
     * Converts an internal name for a type to a {@link Type}.
     *
     * @param type Internal name for a type (primitive or class).
     * @return The resulting Type.
     */
    private static Type convert(String type) {
        if (PRIMITIVE_DESCRIPTORS.containsKey(type)) {
            return PRIMITIVE_DESCRIPTORS.get(type);
        }
        return Type.getObjectType(type);
    }
}