blob: da510ceb5cb9b4b88ba7ddd7fd3933b1f5d09d31 [file] [log] [blame]
Orion Hodsonba28f9f2016-10-26 10:56:25 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Orion Hodsonba28f9f2016-10-26 10:56:25 +010017#include "method_handles-inl.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080018
19#include "android-base/stringprintf.h"
20
Orion Hodsonba28f9f2016-10-26 10:56:25 +010021#include "jvalue.h"
22#include "jvalue-inl.h"
23#include "reflection.h"
24#include "reflection-inl.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000025#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010026
27namespace art {
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029using android::base::StringPrintf;
30
Orion Hodsonba28f9f2016-10-26 10:56:25 +010031namespace {
32
Orion Hodson1a06f9f2016-11-09 08:32:42 +000033#define PRIMITIVES_LIST(V) \
34 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
35 V(Primitive::kPrimByte, Byte, Byte, B) \
36 V(Primitive::kPrimChar, Char, Character, C) \
37 V(Primitive::kPrimShort, Short, Short, S) \
38 V(Primitive::kPrimInt, Int, Integer, I) \
39 V(Primitive::kPrimLong, Long, Long, J) \
40 V(Primitive::kPrimFloat, Float, Float, F) \
41 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010042
43// Assigns |type| to the primitive type associated with |klass|. Returns
44// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
45bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
46 REQUIRES_SHARED(Locks::mutator_lock_) {
47 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000048#define LOOKUP_PRIMITIVE(primitive, _, __, ___) \
49 if (klass->DescriptorEquals(Primitive::BoxedDescriptor(primitive))) { \
50 *type = primitive; \
51 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010052 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010053
Orion Hodson1a06f9f2016-11-09 08:32:42 +000054 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
55#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010056 return false;
57}
58
Orion Hodson1a06f9f2016-11-09 08:32:42 +000059ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010060 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000061 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
62 jmethodID m = nullptr;
63 switch (type) {
64#define CASE_PRIMITIVE(primitive, _, java_name, __) \
65 case primitive: \
66 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
67 break;
68 PRIMITIVES_LIST(CASE_PRIMITIVE);
69#undef CASE_PRIMITIVE
70 case Primitive::Type::kPrimNot:
71 case Primitive::Type::kPrimVoid:
72 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010073 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000074 return jni::DecodeArtMethod(m)->GetDeclaringClass();
75}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010076
Orion Hodson1a06f9f2016-11-09 08:32:42 +000077bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
78 REQUIRES_SHARED(Locks::mutator_lock_) {
79 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010080 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010081 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000082#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
83 if (klass == GetBoxedPrimitiveClass(primitive)) { \
84 *type = primitive; \
85 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
86 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010087 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000088 PRIMITIVES_LIST(CASE_PRIMITIVE)
89#undef CASE_PRIMITIVE
90 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010091}
92
93inline bool IsReferenceType(Primitive::Type type) {
94 return type == Primitive::kPrimNot;
95}
96
97inline bool IsPrimitiveType(Primitive::Type type) {
98 return !IsReferenceType(type);
99}
100
101} // namespace
102
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000103bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
104 REQUIRES_SHARED(Locks::mutator_lock_) {
105 // This function returns true if there's any conceivable conversion
106 // between |from| and |to|. It's expected this method will be used
107 // to determine if a WrongMethodTypeException should be raised. The
108 // decision logic follows the documentation for MethodType.asType().
109 if (from == to) {
110 return true;
111 }
112
113 Primitive::Type from_primitive = from->GetPrimitiveType();
114 Primitive::Type to_primitive = to->GetPrimitiveType();
115 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
116 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
117
118 // If |to| and |from| are references.
119 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
120 // Assignability is determined during parameter conversion when
121 // invoking the associated method handle.
122 return true;
123 }
124
125 // If |to| and |from| are primitives and a widening conversion exists.
126 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
127 return true;
128 }
129
130 // If |to| is a reference and |from| is a primitive, then boxing conversion.
131 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
132 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
133 }
134
135 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
136 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
137 if (from->DescriptorEquals("Ljava/lang/Object;")) {
138 // Object might be converted into a primitive during unboxing.
139 return true;
140 } else if (Primitive::IsNumericType(to_primitive) &&
141 from->DescriptorEquals("Ljava/lang/Number;")) {
142 // Number might be unboxed into any of the number primitive types.
143 return true;
144 }
145 Primitive::Type unboxed_type;
146 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000147 if (unboxed_type == to_primitive) {
148 // Straightforward unboxing conversion such as Boolean => boolean.
149 return true;
150 } else {
151 // Check if widening operations for numeric primitives would work,
152 // such as Byte => byte => long.
153 return Primitive::IsWidenable(unboxed_type, to_primitive);
154 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000155 }
156 }
157
158 return false;
159}
160
161bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
162 REQUIRES_SHARED(Locks::mutator_lock_) {
163 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
164 // Result will be ignored.
165 return true;
166 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
167 // Returned value will be 0 / null.
168 return true;
169 } else {
170 // Otherwise apply usual parameter conversion rules.
171 return IsParameterTypeConvertible(from, to);
172 }
173}
174
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100175bool ConvertJValueCommon(
176 Handle<mirror::MethodType> callsite_type,
177 Handle<mirror::MethodType> callee_type,
178 ObjPtr<mirror::Class> from,
179 ObjPtr<mirror::Class> to,
180 JValue* value) {
181 // The reader maybe concerned about the safety of the heap object
182 // that may be in |value|. There is only one case where allocation
183 // is obviously needed and that's for boxing. However, in the case
184 // of boxing |value| contains a non-reference type.
185
186 const Primitive::Type from_type = from->GetPrimitiveType();
187 const Primitive::Type to_type = to->GetPrimitiveType();
188
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000189 // Put incoming value into |src_value| and set return value to 0.
190 // Errors and conversions from void require the return value to be 0.
191 const JValue src_value(*value);
192 value->SetJ(0);
193
194 // Conversion from void set result to zero.
195 if (from_type == Primitive::kPrimVoid) {
196 return true;
197 }
198
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100199 // This method must be called only when the types don't match.
200 DCHECK(from != to);
201
202 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
203 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000204 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100205 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100206 return false;
207 }
208 return true;
209 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
210 // They're both reference types. If "from" is null, we can pass it
211 // through unchanged. If not, we must generate a cast exception if
212 // |to| is not assignable from the dynamic type of |ref|.
213 //
214 // Playing it safe with StackHandleScope here, not expecting any allocation
215 // in mirror::Class::IsAssignable().
216 StackHandleScope<2> hs(Thread::Current());
217 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000218 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100219 if (h_obj.Get() != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) {
220 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
221 return false;
222 }
223 value->SetL(h_obj.Get());
224 return true;
225 } else if (IsReferenceType(to_type)) {
226 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100227 // The source type is a primitive and the target type is a reference, so we must box.
228 // The target type maybe a super class of the boxed source type, for example,
229 // if the source type is int, it's boxed type is java.lang.Integer, and the target
230 // type could be java.lang.Number.
231 Primitive::Type type;
232 if (!GetUnboxedPrimitiveType(to, &type)) {
233 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000234 if (boxed_from_class->IsSubClass(to)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100235 type = from_type;
236 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100237 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
238 return false;
239 }
240 }
241
242 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100243 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
244 return false;
245 }
246
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000247 if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100248 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
249 return false;
250 }
251
252 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000253 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100254 value->SetL(boxed.Ptr());
255 return true;
256 } else {
257 // The source type is a reference and the target type is a primitive, so we must unbox.
258 DCHECK(IsReferenceType(from_type));
259 DCHECK(IsPrimitiveType(to_type));
260
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000261 ObjPtr<mirror::Object> from_obj(src_value.GetL());
262 if (UNLIKELY(from_obj == nullptr)) {
263 ThrowNullPointerException(
264 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
265 from->PrettyDescriptor().c_str()).c_str());
266 return false;
267 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100268
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000269 Primitive::Type unboxed_type;
270 JValue unboxed_value;
271 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100272 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
273 return false;
274 }
275
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000276 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
277 ThrowClassCastException(from, to);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100278 return false;
279 }
280
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000281 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100282 }
283}
284
285} // namespace art