blob: 2322cacbd3510e27d69b412ea5416a3cdfd73d3b (
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
|
// Copyright 2014 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.
#include "content/browser/android/java/java_type.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
namespace content {
namespace {
// Array component type names are similar to JNI type names, except for using
// dots as namespace separators in class names.
std::unique_ptr<JavaType> CreateFromArrayComponentTypeName(
const std::string& type_name) {
std::unique_ptr<JavaType> result(new JavaType());
DCHECK(!type_name.empty());
switch (type_name[0]) {
case 'Z':
result->type = JavaType::TypeBoolean;
break;
case 'B':
result->type = JavaType::TypeByte;
break;
case 'C':
result->type = JavaType::TypeChar;
break;
case 'S':
result->type = JavaType::TypeShort;
break;
case 'I':
result->type = JavaType::TypeInt;
break;
case 'J':
result->type = JavaType::TypeLong;
break;
case 'F':
result->type = JavaType::TypeFloat;
break;
case 'D':
result->type = JavaType::TypeDouble;
break;
case '[':
result->type = JavaType::TypeArray;
result->inner_type =
CreateFromArrayComponentTypeName(type_name.substr(1));
break;
case 'L':
if (type_name == "Ljava.lang.String;") {
result->type = JavaType::TypeString;
result->class_jni_name = "java/lang/String";
} else {
result->type = JavaType::TypeObject;
result->class_jni_name = type_name.substr(1, type_name.length() - 2);
base::ReplaceSubstringsAfterOffset(
&result->class_jni_name, 0, ".", "/");
}
break;
default:
// Includes void (V).
NOTREACHED();
}
return result;
}
} // namespace
JavaType::JavaType() {
}
JavaType::JavaType(const JavaType& other) {
*this = other;
}
JavaType::~JavaType() {
}
JavaType& JavaType::operator=(const JavaType& other) {
if (this == &other)
return *this;
type = other.type;
if (other.inner_type) {
DCHECK_EQ(JavaType::TypeArray, type);
inner_type.reset(new JavaType(*other.inner_type));
} else {
inner_type.reset();
}
class_jni_name = other.class_jni_name;
return *this;
}
// static
JavaType JavaType::CreateFromBinaryName(const std::string& binary_name) {
JavaType result;
DCHECK(!binary_name.empty());
if (binary_name == "boolean") {
result.type = JavaType::TypeBoolean;
} else if (binary_name == "byte") {
result.type = JavaType::TypeByte;
} else if (binary_name == "char") {
result.type = JavaType::TypeChar;
} else if (binary_name == "short") {
result.type = JavaType::TypeShort;
} else if (binary_name == "int") {
result.type = JavaType::TypeInt;
} else if (binary_name == "long") {
result.type = JavaType::TypeLong;
} else if (binary_name == "float") {
result.type = JavaType::TypeFloat;
} else if (binary_name == "double") {
result.type = JavaType::TypeDouble;
} else if (binary_name == "void") {
result.type = JavaType::TypeVoid;
} else if (binary_name[0] == '[') {
result.type = JavaType::TypeArray;
result.inner_type = CreateFromArrayComponentTypeName(binary_name.substr(1));
} else if (binary_name == "java.lang.String") {
result.type = JavaType::TypeString;
result.class_jni_name = "java/lang/String";
} else {
result.type = JavaType::TypeObject;
result.class_jni_name = binary_name;
base::ReplaceSubstringsAfterOffset(&result.class_jni_name, 0, ".", "/");
}
return result;
}
std::string JavaType::JNIName() const {
switch (type) {
case JavaType::TypeBoolean:
return "Z";
case JavaType::TypeByte:
return "B";
case JavaType::TypeChar:
return "C";
case JavaType::TypeShort:
return "S";
case JavaType::TypeInt:
return "I";
case JavaType::TypeLong:
return "J";
case JavaType::TypeFloat:
return "F";
case JavaType::TypeDouble:
return "D";
case JavaType::TypeVoid:
return "V";
case JavaType::TypeArray:
return "[" + inner_type->JNISignature();
case JavaType::TypeString:
case JavaType::TypeObject:
return class_jni_name;
}
NOTREACHED();
return std::string();
}
std::string JavaType::JNISignature() const {
if (type == JavaType::TypeString || type == JavaType::TypeObject)
return "L" + JNIName() + ";";
else
return JNIName();
}
} // namespace content
|