blob: f1adc32ce220ff9fc2840d4b32acd9afddb95059 [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)) {
144 return Primitive::IsWidenable(unboxed_type, to_primitive);
145 }
146 }
147
148 return false;
149}
150
151bool IsReturnTypeConvertible(ObjPtr<mirror::Class> from, ObjPtr<mirror::Class> to)
152 REQUIRES_SHARED(Locks::mutator_lock_) {
153 if (to->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
154 // Result will be ignored.
155 return true;
156 } else if (from->GetPrimitiveType() == Primitive::Type::kPrimVoid) {
157 // Returned value will be 0 / null.
158 return true;
159 } else {
160 // Otherwise apply usual parameter conversion rules.
161 return IsParameterTypeConvertible(from, to);
162 }
163}
164
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100165bool ConvertJValueCommon(
166 Handle<mirror::MethodType> callsite_type,
167 Handle<mirror::MethodType> callee_type,
168 ObjPtr<mirror::Class> from,
169 ObjPtr<mirror::Class> to,
170 JValue* value) {
171 // The reader maybe concerned about the safety of the heap object
172 // that may be in |value|. There is only one case where allocation
173 // is obviously needed and that's for boxing. However, in the case
174 // of boxing |value| contains a non-reference type.
175
176 const Primitive::Type from_type = from->GetPrimitiveType();
177 const Primitive::Type to_type = to->GetPrimitiveType();
178
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000179 // Put incoming value into |src_value| and set return value to 0.
180 // Errors and conversions from void require the return value to be 0.
181 const JValue src_value(*value);
182 value->SetJ(0);
183
184 // Conversion from void set result to zero.
185 if (from_type == Primitive::kPrimVoid) {
186 return true;
187 }
188
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100189 // This method must be called only when the types don't match.
190 DCHECK(from != to);
191
192 if (IsPrimitiveType(from_type) && IsPrimitiveType(to_type)) {
193 // The source and target types are both primitives.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000194 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(from_type, to_type, src_value, value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100195 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100196 return false;
197 }
198 return true;
199 } else if (IsReferenceType(from_type) && IsReferenceType(to_type)) {
200 // They're both reference types. If "from" is null, we can pass it
201 // through unchanged. If not, we must generate a cast exception if
202 // |to| is not assignable from the dynamic type of |ref|.
203 //
204 // Playing it safe with StackHandleScope here, not expecting any allocation
205 // in mirror::Class::IsAssignable().
206 StackHandleScope<2> hs(Thread::Current());
207 Handle<mirror::Class> h_to(hs.NewHandle(to));
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000208 Handle<mirror::Object> h_obj(hs.NewHandle(src_value.GetL()));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100209 if (h_obj.Get() != nullptr && !to->IsAssignableFrom(h_obj->GetClass())) {
210 ThrowClassCastException(h_to.Get(), h_obj->GetClass());
211 return false;
212 }
213 value->SetL(h_obj.Get());
214 return true;
215 } else if (IsReferenceType(to_type)) {
216 DCHECK(IsPrimitiveType(from_type));
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100217 // The source type is a primitive and the target type is a reference, so we must box.
218 // The target type maybe a super class of the boxed source type, for example,
219 // if the source type is int, it's boxed type is java.lang.Integer, and the target
220 // type could be java.lang.Number.
221 Primitive::Type type;
222 if (!GetUnboxedPrimitiveType(to, &type)) {
223 ObjPtr<mirror::Class> boxed_from_class = GetBoxedPrimitiveClass(from_type);
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000224 if (boxed_from_class->IsSubClass(to)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100225 type = from_type;
226 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100227 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
228 return false;
229 }
230 }
231
232 if (UNLIKELY(from_type != type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100233 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
234 return false;
235 }
236
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000237 if (!ConvertPrimitiveValueNoThrow(from_type, type, src_value, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100238 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
239 return false;
240 }
241
242 // Then perform the actual boxing, and then set the reference.
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000243 ObjPtr<mirror::Object> boxed = BoxPrimitive(type, src_value);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100244 value->SetL(boxed.Ptr());
245 return true;
246 } else {
247 // The source type is a reference and the target type is a primitive, so we must unbox.
248 DCHECK(IsReferenceType(from_type));
249 DCHECK(IsPrimitiveType(to_type));
250
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000251 ObjPtr<mirror::Object> from_obj(src_value.GetL());
252 if (UNLIKELY(from_obj == nullptr)) {
253 ThrowNullPointerException(
254 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
255 from->PrettyDescriptor().c_str()).c_str());
256 return false;
257 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100258
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000259 Primitive::Type unboxed_type;
260 JValue unboxed_value;
261 if (UNLIKELY(!GetUnboxedTypeAndValue(from_obj, &unboxed_type, &unboxed_value))) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100262 ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get());
263 return false;
264 }
265
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000266 if (UNLIKELY(!ConvertPrimitiveValueNoThrow(unboxed_type, to_type, unboxed_value, value))) {
267 ThrowClassCastException(from, to);
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100268 return false;
269 }
270
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000271 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100272 }
273}
274
275} // namespace art