blob: 3c22d7f656548a93a3c100f7938b3787237c147f [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
17#include "method_handles.h"
18
19#include "method_handles-inl.h"
20#include "jvalue.h"
21#include "jvalue-inl.h"
22#include "reflection.h"
23#include "reflection-inl.h"
Orion Hodson1a06f9f2016-11-09 08:32:42 +000024#include "well_known_classes.h"
Orion Hodsonba28f9f2016-10-26 10:56:25 +010025
26namespace art {
27
28namespace {
29
Orion Hodson1a06f9f2016-11-09 08:32:42 +000030#define PRIMITIVES_LIST(V) \
31 V(Primitive::kPrimBoolean, Boolean, Boolean, Z) \
32 V(Primitive::kPrimByte, Byte, Byte, B) \
33 V(Primitive::kPrimChar, Char, Character, C) \
34 V(Primitive::kPrimShort, Short, Short, S) \
35 V(Primitive::kPrimInt, Int, Integer, I) \
36 V(Primitive::kPrimLong, Long, Long, J) \
37 V(Primitive::kPrimFloat, Float, Float, F) \
38 V(Primitive::kPrimDouble, Double, Double, D)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010039
40// Assigns |type| to the primitive type associated with |klass|. Returns
41// true iff. |klass| was a boxed type (Integer, Long etc.), false otherwise.
42bool GetUnboxedPrimitiveType(ObjPtr<mirror::Class> klass, Primitive::Type* type)
43 REQUIRES_SHARED(Locks::mutator_lock_) {
44 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000045#define LOOKUP_PRIMITIVE(primitive, _, __, ___) \
46 if (klass->DescriptorEquals(Primitive::BoxedDescriptor(primitive))) { \
47 *type = primitive; \
48 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010049 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010050
Orion Hodson1a06f9f2016-11-09 08:32:42 +000051 PRIMITIVES_LIST(LOOKUP_PRIMITIVE);
52#undef LOOKUP_PRIMITIVE
Orion Hodsonba28f9f2016-10-26 10:56:25 +010053 return false;
54}
55
Orion Hodson1a06f9f2016-11-09 08:32:42 +000056ObjPtr<mirror::Class> GetBoxedPrimitiveClass(Primitive::Type type)
Orion Hodsonba28f9f2016-10-26 10:56:25 +010057 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson1a06f9f2016-11-09 08:32:42 +000058 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
59 jmethodID m = nullptr;
60 switch (type) {
61#define CASE_PRIMITIVE(primitive, _, java_name, __) \
62 case primitive: \
63 m = WellKnownClasses::java_lang_ ## java_name ## _valueOf; \
64 break;
65 PRIMITIVES_LIST(CASE_PRIMITIVE);
66#undef CASE_PRIMITIVE
67 case Primitive::Type::kPrimNot:
68 case Primitive::Type::kPrimVoid:
69 return nullptr;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010070 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000071 return jni::DecodeArtMethod(m)->GetDeclaringClass();
72}
Orion Hodsonba28f9f2016-10-26 10:56:25 +010073
Orion Hodson1a06f9f2016-11-09 08:32:42 +000074bool GetUnboxedTypeAndValue(ObjPtr<mirror::Object> o, Primitive::Type* type, JValue* value)
75 REQUIRES_SHARED(Locks::mutator_lock_) {
76 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010077 ObjPtr<mirror::Class> klass = o->GetClass();
Orion Hodsonba28f9f2016-10-26 10:56:25 +010078 ArtField* primitive_field = &klass->GetIFieldsPtr()->At(0);
Orion Hodson1a06f9f2016-11-09 08:32:42 +000079#define CASE_PRIMITIVE(primitive, abbrev, _, shorthand) \
80 if (klass == GetBoxedPrimitiveClass(primitive)) { \
81 *type = primitive; \
82 value->Set ## shorthand(primitive_field->Get ## abbrev(o)); \
83 return true; \
Orion Hodsonba28f9f2016-10-26 10:56:25 +010084 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +000085 PRIMITIVES_LIST(CASE_PRIMITIVE)
86#undef CASE_PRIMITIVE
87 return false;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010088}
89
90inline bool IsReferenceType(Primitive::Type type) {
91 return type == Primitive::kPrimNot;
92}
93
94inline bool IsPrimitiveType(Primitive::Type type) {
95 return !IsReferenceType(type);
96}
97
98} // namespace
99
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000100bool IsParameterTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
101 REQUIRES_SHARED(Locks::mutator_lock_) {
102 // This function returns true if there's any conceivable conversion
103 // between |from| and |to|. It's expected this method will be used
104 // to determine if a WrongMethodTypeException should be raised. The
105 // decision logic follows the documentation for MethodType.asType().
106 if (from == to) {
107 return true;
108 }
109
110 Primitive::Type from_primitive = from->GetPrimitiveType();
111 Primitive::Type to_primitive = to->GetPrimitiveType();
112 DCHECK(from_primitive != Primitive::Type::kPrimVoid);
113 DCHECK(to_primitive != Primitive::Type::kPrimVoid);
114
115 // If |to| and |from| are references.
116 if (IsReferenceType(from_primitive) && IsReferenceType(to_primitive)) {
117 // Assignability is determined during parameter conversion when
118 // invoking the associated method handle.
119 return true;
120 }
121
122 // If |to| and |from| are primitives and a widening conversion exists.
123 if (Primitive::IsWidenable(from_primitive, to_primitive)) {
124 return true;
125 }
126
127 // If |to| is a reference and |from| is a primitive, then boxing conversion.
128 if (IsReferenceType(to_primitive) && IsPrimitiveType(from_primitive)) {
129 return to->IsAssignableFrom(GetBoxedPrimitiveClass(from_primitive));
130 }
131
132 // If |from| is a reference and |to| is a primitive, then unboxing conversion.
133 if (IsPrimitiveType(to_primitive) && IsReferenceType(from_primitive)) {
134 if (from->DescriptorEquals("Ljava/lang/Object;")) {
135 // Object might be converted into a primitive during unboxing.
136 return true;
137 } else if (Primitive::IsNumericType(to_primitive) &&
138 from->DescriptorEquals("Ljava/lang/Number;")) {
139 // Number might be unboxed into any of the number primitive types.
140 return true;
141 }
142 Primitive::Type unboxed_type;
143 if (GetUnboxedPrimitiveType(from, &unboxed_type)) {
Orion Hodsonf1412b42016-11-11 12:03:29 +0000144 if (unboxed_type == to_primitive) {
145 // Straightforward unboxing conversion such as Boolean => boolean.
146 return true;
147 } else {
148 // Check if widening operations for numeric primitives would work,
149 // such as Byte => byte => long.
150 return Primitive::IsWidenable(unboxed_type, to_primitive);
151 }
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000152 }
153 }
154
155 return false;
156}
157
158bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
159 REQUIRES_SHARED(Locks::mutator_lock_) {
160 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
161 // Result will be ignored.
162 return true;
163 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
164 // Returned value will be 0 / null.
165 return true;
166 } else {
167 // Otherwise apply usual parameter conversion rules.
168 return IsParameterTypeConvertible(from, to);
169 }
170}
171
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100172bool ConvertJValueCommon(
173 Handle<mirror::MethodType> callsite_type,
174 Handle<mirror::MethodType> callee_type,
175 ObjPtr<mirror::Class> from,
176 ObjPtr<mirror::Class> to,
177 JValue* value) {
178 // The reader maybe concerned about the safety of the heap object
179 // that may be in |value|. There is only one case where allocation
180 // is obviously needed and that's for boxing. However, in the case
181 // of boxing |value| contains a non-reference type.
182
183 const Primitive::Type from_type = from->GetPrimitiveType();
184 const Primitive::Type to_type = to->GetPrimitiveType();
185
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000186 // Put incoming value into |src_value| and set return value to 0.
187 // Errors and conversions from void require the return value to be 0.
188 const JValue src_value(*value);
189 value->SetJ(0);
190
191 // Conversion from void set result to zero.
192 if (from_type == Primitive::kPrimVoid) {
193 return true;
194 }
195
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100196 // This method must be called only when the types don't match.
197 DCHECK(from != to);
198
199 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
200 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000201 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100202 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100203 return false;
204 }
205 return true;
206 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
207 // They're both reference types. If "from" is null, we can pass it
208 // through unchanged. If not, we must generate a cast exception if
209 // |to| is not assignable from the dynamic type of |ref|.
210 //
211 // Playing it safe with StackHandleScope here, not expecting any allocation
212 // in mirror::Class::IsAssignable().
213 StackHandleScope<2> hs(Thread::Current());
214 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000215 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100216 if (h_obj.Get() != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) {
217 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
218 return false;
219 }
220 value->SetL(h_obj.Get());
221 return true;
222 } else if (IsReferenceType(to_type)) {
223 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100224 // The source type is a primitive and the target type is a reference, so we must box.
225 // The target type maybe a super class of the boxed source type, for example,
226 // if the source type is int, it's boxed type is java.lang.Integer, and the target
227 // type could be java.lang.Number.
228 Primitive::Type type;
229 if (!GetUnboxedPrimitiveType(to, &type)) {
230 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000231 if (boxed_from_class->IsSubClass(to)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100232 type = from_type;
233 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100234 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
235 return false;
236 }
237 }
238
239 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100240 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
241 return false;
242 }
243
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000244 if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100245 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
246 return false;
247 }
248
249 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000250 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100251 value->SetL(boxed.Ptr());
252 return true;
253 } else {
254 // The source type is a reference and the target type is a primitive, so we must unbox.
255 DCHECK(IsReferenceType(from_type));
256 DCHECK(IsPrimitiveType(to_type));
257
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000258 ObjPtr<mirror::Object> from_obj(src_value.GetL());
259 if (UNLIKELY(from_obj == nullptr)) {
260 ThrowNullPointerException(
261 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
262 from->PrettyDescriptor().c_str()).c_str());
263 return false;
264 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100265
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000266 Primitive::Type unboxed_type;
267 JValue unboxed_value;
268 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100269 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
270 return false;
271 }
272
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000273 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
274 ThrowClassCastException(from, to);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100275 return false;
276 }
277
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000278 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100279 }
280}
281
282} // namespace art