xref: /aosp_15_r20/art/runtime/method_handles_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "method_handles.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
21*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
22*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "jvalue-inl.h"
24*795d594fSAndroid Build Coastguard Worker #include "mirror/method_type.h"
25*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-alloc-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "reflection.h"
28*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker namespace {
IsClassCastException(ObjPtr<mirror::Throwable> throwable)34*795d594fSAndroid Build Coastguard Worker   bool IsClassCastException(ObjPtr<mirror::Throwable> throwable)
35*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) {
36*795d594fSAndroid Build Coastguard Worker     return throwable->GetClass()->DescriptorEquals("Ljava/lang/ClassCastException;");
37*795d594fSAndroid Build Coastguard Worker   }
38*795d594fSAndroid Build Coastguard Worker 
IsNullPointerException(ObjPtr<mirror::Throwable> throwable)39*795d594fSAndroid Build Coastguard Worker   bool IsNullPointerException(ObjPtr<mirror::Throwable> throwable)
40*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) {
41*795d594fSAndroid Build Coastguard Worker     return throwable->GetClass()->DescriptorEquals("Ljava/lang/NullPointerException;");
42*795d594fSAndroid Build Coastguard Worker   }
43*795d594fSAndroid Build Coastguard Worker 
IsWrongMethodTypeException(ObjPtr<mirror::Throwable> throwable)44*795d594fSAndroid Build Coastguard Worker   bool IsWrongMethodTypeException(ObjPtr<mirror::Throwable> throwable)
45*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) {
46*795d594fSAndroid Build Coastguard Worker     return throwable->GetClass()->DescriptorEquals("Ljava/lang/invoke/WrongMethodTypeException;");
47*795d594fSAndroid Build Coastguard Worker   }
48*795d594fSAndroid Build Coastguard Worker 
TryConversion(Handle<mirror::Class> from,Handle<mirror::Class> to,JValue * value)49*795d594fSAndroid Build Coastguard Worker   static bool TryConversion(Handle<mirror::Class> from, Handle<mirror::Class> to, JValue* value)
50*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_) {
51*795d594fSAndroid Build Coastguard Worker     class ThrowWrongMethodTypeFunctionImpl final : public ThrowWrongMethodTypeFunction {
52*795d594fSAndroid Build Coastguard Worker       void operator()() const override REQUIRES_SHARED(Locks::mutator_lock_) {
53*795d594fSAndroid Build Coastguard Worker         ThrowWrongMethodTypeException("<callee-descriptor>", "<callsite-descriptor>");
54*795d594fSAndroid Build Coastguard Worker       }
55*795d594fSAndroid Build Coastguard Worker     };
56*795d594fSAndroid Build Coastguard Worker     ThrowWrongMethodTypeFunctionImpl throw_wmt;
57*795d594fSAndroid Build Coastguard Worker     return ConvertJValueCommon(throw_wmt, from.Get(), to.Get(), value);
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker }  // namespace
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker class MethodHandlesTest : public CommonRuntimeTest {
62*795d594fSAndroid Build Coastguard Worker  protected:
MethodHandlesTest()63*795d594fSAndroid Build Coastguard Worker   MethodHandlesTest() {
64*795d594fSAndroid Build Coastguard Worker     use_boot_image_ = true;  // Make the Runtime creation cheaper.
65*795d594fSAndroid Build Coastguard Worker   }
66*795d594fSAndroid Build Coastguard Worker };
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker //
69*795d594fSAndroid Build Coastguard Worker // Primitive -> Primitive Conversions
70*795d594fSAndroid Build Coastguard Worker //
71*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedPrimitiveWideningBI)72*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedPrimitiveWideningBI) {
73*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
74*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
75*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
76*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('B'));
77*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('I'));
78*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(static_cast<int8_t>(3));
79*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
80*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(3, value.GetI());
81*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedPrimitiveWideningCJ)84*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedPrimitiveWideningCJ) {
85*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
86*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
87*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
88*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('C'));
89*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('J'));
90*795d594fSAndroid Build Coastguard Worker   uint16_t raw_value = 0x8000;
91*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(raw_value);
92*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
93*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
94*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(static_cast<int64_t>(raw_value), value.GetJ());
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedPrimitiveWideningIF)97*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedPrimitiveWideningIF) {
98*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
99*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
100*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
101*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('I'));
102*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('F'));
103*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(-16);
104*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
105*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
106*795d594fSAndroid Build Coastguard Worker   ASSERT_FLOAT_EQ(-16.0f, value.GetF());
107*795d594fSAndroid Build Coastguard Worker }
108*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveWideningBC)109*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveWideningBC) {
110*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
111*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
112*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
113*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('B'));
114*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('C'));
115*795d594fSAndroid Build Coastguard Worker   JValue value;
116*795d594fSAndroid Build Coastguard Worker   value.SetB(0);
117*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
118*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
119*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
120*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveWideningSC)123*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveWideningSC) {
124*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
125*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
126*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
127*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('S'));
128*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('C'));
129*795d594fSAndroid Build Coastguard Worker   JValue value;
130*795d594fSAndroid Build Coastguard Worker   value.SetS(0x1234);
131*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
132*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
133*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
134*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveWideningDJ)137*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveWideningDJ) {
138*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
139*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
140*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
141*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('D'));
142*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('J'));
143*795d594fSAndroid Build Coastguard Worker   JValue value;
144*795d594fSAndroid Build Coastguard Worker   value.SetD(1e72);
145*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
146*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
147*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
148*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveWideningZI)151*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveWideningZI) {
152*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
153*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
154*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
155*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('Z'));
156*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('I'));
157*795d594fSAndroid Build Coastguard Worker   JValue value;
158*795d594fSAndroid Build Coastguard Worker   value.SetZ(true);
159*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
160*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
161*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
162*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker //
166*795d594fSAndroid Build Coastguard Worker // Reference -> Reference Conversions
167*795d594fSAndroid Build Coastguard Worker //
168*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedReferenceCast)169*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedReferenceCast) {
170*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
171*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
172*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
173*795d594fSAndroid Build Coastguard Worker   static const int32_t kInitialValue = 101;
174*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
175*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Object> boxed_value = hs.NewHandle(BoxPrimitive(Primitive::kPrimInt, value));
176*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(boxed_value->GetClass());
177*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Number;"));
178*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
179*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
180*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
181*795d594fSAndroid Build Coastguard Worker   JValue unboxed_value;
182*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(UnboxPrimitiveForResult(value.GetL(), cl->FindPrimitiveClass('I'), &unboxed_value));
183*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(kInitialValue, unboxed_value.GetI());
184*795d594fSAndroid Build Coastguard Worker }
185*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedReferenceCast)186*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedReferenceCast) {
187*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
188*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
189*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
190*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(3.733e2);
191*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Object> boxed_value = hs.NewHandle(BoxPrimitive(Primitive::kPrimDouble, value));
192*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(boxed_value->GetClass());
193*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Integer;"));
194*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
195*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
196*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
197*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
198*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsClassCastException(soa.Self()->GetException()));
199*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
200*795d594fSAndroid Build Coastguard Worker }
201*795d594fSAndroid Build Coastguard Worker 
202*795d594fSAndroid Build Coastguard Worker //
203*795d594fSAndroid Build Coastguard Worker // Primitive -> Reference Conversions
204*795d594fSAndroid Build Coastguard Worker //
205*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedPrimitiveConversionPrimitiveToBoxed)206*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedPrimitiveConversionPrimitiveToBoxed) {
207*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
208*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
209*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
210*795d594fSAndroid Build Coastguard Worker   const int32_t kInitialValue = 1;
211*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
212*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('I'));
213*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Integer;"));
214*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
215*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
216*795d594fSAndroid Build Coastguard Worker   JValue unboxed_to_value;
217*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(UnboxPrimitiveForResult(value.GetL(), from.Get(), &unboxed_to_value));
218*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(kInitialValue, unboxed_to_value.GetI());
219*795d594fSAndroid Build Coastguard Worker }
220*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedPrimitiveConversionPrimitiveToBoxedSuper)221*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedPrimitiveConversionPrimitiveToBoxedSuper) {
222*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
223*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
224*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
225*795d594fSAndroid Build Coastguard Worker   const int32_t kInitialValue = 1;
226*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
227*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('I'));
228*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Number;"));
229*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
230*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
231*795d594fSAndroid Build Coastguard Worker   JValue unboxed_to_value;
232*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(UnboxPrimitiveForResult(value.GetL(), from.Get(), &unboxed_to_value));
233*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(kInitialValue, unboxed_to_value.GetI());
234*795d594fSAndroid Build Coastguard Worker }
235*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveConversionNotBoxable)236*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveConversionNotBoxable) {
237*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
238*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
239*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
240*795d594fSAndroid Build Coastguard Worker   const int32_t kInitialValue = 1;
241*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
242*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('I'));
243*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Runtime;"));
244*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
245*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
246*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
247*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
248*795d594fSAndroid Build Coastguard Worker }
249*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveConversionPrimitiveToBoxedWider)250*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveConversionPrimitiveToBoxedWider) {
251*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
252*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
253*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
254*795d594fSAndroid Build Coastguard Worker   const int32_t kInitialValue = 1;
255*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
256*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('I'));
257*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Long;"));
258*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
259*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
260*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
261*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
262*795d594fSAndroid Build Coastguard Worker }
263*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedPrimitiveConversionPrimitiveToBoxedNarrower)264*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedPrimitiveConversionPrimitiveToBoxedNarrower) {
265*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
266*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
267*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
268*795d594fSAndroid Build Coastguard Worker   const int32_t kInitialValue = 1;
269*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
270*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindPrimitiveClass('I'));
271*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Byte;"));
272*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
273*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
274*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
275*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
276*795d594fSAndroid Build Coastguard Worker }
277*795d594fSAndroid Build Coastguard Worker 
278*795d594fSAndroid Build Coastguard Worker //
279*795d594fSAndroid Build Coastguard Worker // Reference -> Primitive Conversions
280*795d594fSAndroid Build Coastguard Worker //
281*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedBoxedToPrimitiveConversion)282*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedBoxedToPrimitiveConversion) {
283*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
284*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
285*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
286*795d594fSAndroid Build Coastguard Worker   const int32_t kInitialValue = 101;
287*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
288*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Object> boxed_value = hs.NewHandle(BoxPrimitive(Primitive::kPrimInt, value));
289*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Integer;"));
290*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('I'));
291*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
292*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
293*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(soa.Self()->IsExceptionPending());
294*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(kInitialValue, value.GetI());
295*795d594fSAndroid Build Coastguard Worker }
296*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,SupportedBoxedToWiderPrimitiveConversion)297*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, SupportedBoxedToWiderPrimitiveConversion) {
298*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
299*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
300*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
301*795d594fSAndroid Build Coastguard Worker   static const int32_t kInitialValue = 101;
302*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
303*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Object> boxed_value = hs.NewHandle(BoxPrimitive(Primitive::kPrimInt, value));
304*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Integer;"));
305*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('J'));
306*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
307*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(TryConversion(from, to, &value));
308*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(kInitialValue, value.GetJ());
309*795d594fSAndroid Build Coastguard Worker }
310*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedNullBoxedToPrimitiveConversion)311*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedNullBoxedToPrimitiveConversion) {
312*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
313*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
314*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
315*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(101);
316*795d594fSAndroid Build Coastguard Worker   ScopedNullHandle<mirror::Object> boxed_value;
317*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Integer;"));
318*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('I'));
319*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
320*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
321*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
322*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsNullPointerException(soa.Self()->GetException()));
323*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
324*795d594fSAndroid Build Coastguard Worker }
325*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedNotBoxReferenceToPrimitiveConversion)326*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedNotBoxReferenceToPrimitiveConversion) {
327*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
328*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
329*795d594fSAndroid Build Coastguard Worker   StackHandleScope<2> hs(soa.Self());
330*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Class;"));
331*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('I'));
332*795d594fSAndroid Build Coastguard Worker   // Set value to be converted as some non-primitive type.
333*795d594fSAndroid Build Coastguard Worker   JValue value;
334*795d594fSAndroid Build Coastguard Worker   value.SetL(cl->FindPrimitiveClass('V'));
335*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
336*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
337*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsClassCastException(soa.Self()->GetException()));
338*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
339*795d594fSAndroid Build Coastguard Worker }
340*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedBoxedToNarrowerPrimitiveConversionNoCast)341*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedBoxedToNarrowerPrimitiveConversionNoCast) {
342*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
343*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
344*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
345*795d594fSAndroid Build Coastguard Worker   static const int32_t kInitialValue = 101;
346*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
347*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Object> boxed_value = hs.NewHandle(BoxPrimitive(Primitive::kPrimInt, value));
348*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Integer;"));
349*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('S'));
350*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
351*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
352*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
353*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsWrongMethodTypeException(soa.Self()->GetException()));
354*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
355*795d594fSAndroid Build Coastguard Worker }
356*795d594fSAndroid Build Coastguard Worker 
TEST_F(MethodHandlesTest,UnsupportedBoxedToNarrowerPrimitiveConversionWithCast)357*795d594fSAndroid Build Coastguard Worker TEST_F(MethodHandlesTest, UnsupportedBoxedToNarrowerPrimitiveConversionWithCast) {
358*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
359*795d594fSAndroid Build Coastguard Worker   ClassLinker* cl = Runtime::Current()->GetClassLinker();
360*795d594fSAndroid Build Coastguard Worker   StackHandleScope<3> hs(soa.Self());
361*795d594fSAndroid Build Coastguard Worker   static const double kInitialValue = 1e77;
362*795d594fSAndroid Build Coastguard Worker   JValue value = JValue::FromPrimitive(kInitialValue);
363*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Object> boxed_value = hs.NewHandle(BoxPrimitive(Primitive::kPrimDouble, value));
364*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> from = hs.NewHandle(cl->FindSystemClass(soa.Self(), "Ljava/lang/Number;"));
365*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> to = hs.NewHandle(cl->FindPrimitiveClass('F'));
366*795d594fSAndroid Build Coastguard Worker   value.SetL(boxed_value.Get());
367*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(TryConversion(from, to, &value));
368*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(soa.Self()->IsExceptionPending());
369*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(IsClassCastException(soa.Self()->GetException()));
370*795d594fSAndroid Build Coastguard Worker   soa.Self()->ClearException();
371*795d594fSAndroid Build Coastguard Worker }
372*795d594fSAndroid Build Coastguard Worker 
373*795d594fSAndroid Build Coastguard Worker }  // namespace art
374