Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "reflection.h" |
| 18 | |
Brian Carlstrom | 8edba0b | 2014-03-14 16:11:43 -0700 | [diff] [blame] | 19 | #include <float.h> |
Ian Rogers | 88e46a3 | 2014-03-14 14:37:59 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "ScopedLocalRef.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 22 | |
| 23 | #include "common_compiler_test.h" |
| 24 | #include "mirror/art_method-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | // TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used. |
| 30 | class ReflectionTest : public CommonCompilerTest { |
| 31 | protected: |
| 32 | virtual void SetUp() { |
| 33 | CommonCompilerTest::SetUp(); |
| 34 | |
| 35 | vm_ = Runtime::Current()->GetJavaVM(); |
| 36 | |
| 37 | // Turn on -verbose:jni for the JNI tests. |
| 38 | // gLogVerbosity.jni = true; |
| 39 | |
| 40 | vm_->AttachCurrentThread(&env_, NULL); |
| 41 | |
| 42 | ScopedLocalRef<jclass> aioobe(env_, |
| 43 | env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
| 44 | CHECK(aioobe.get() != NULL); |
| 45 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
| 46 | |
| 47 | ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException")); |
| 48 | CHECK(ase.get() != NULL); |
| 49 | ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get())); |
| 50 | |
| 51 | ScopedLocalRef<jclass> sioobe(env_, |
| 52 | env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
| 53 | CHECK(sioobe.get() != NULL); |
| 54 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 55 | } |
| 56 | |
| 57 | void CleanUpJniEnv() { |
| 58 | if (aioobe_ != NULL) { |
| 59 | env_->DeleteGlobalRef(aioobe_); |
| 60 | aioobe_ = NULL; |
| 61 | } |
| 62 | if (ase_ != NULL) { |
| 63 | env_->DeleteGlobalRef(ase_); |
| 64 | ase_ = NULL; |
| 65 | } |
| 66 | if (sioobe_ != NULL) { |
| 67 | env_->DeleteGlobalRef(sioobe_); |
| 68 | sioobe_ = NULL; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | virtual void TearDown() { |
| 73 | CleanUpJniEnv(); |
| 74 | CommonCompilerTest::TearDown(); |
| 75 | } |
| 76 | |
| 77 | jclass GetPrimitiveClass(char descriptor) { |
| 78 | ScopedObjectAccess soa(env_); |
| 79 | mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor); |
| 80 | CHECK(c != nullptr); |
| 81 | return soa.AddLocalReference<jclass>(c); |
| 82 | } |
| 83 | |
| 84 | void ReflectionTestMakeExecutable(mirror::ArtMethod** method, |
| 85 | mirror::Object** receiver, |
| 86 | bool is_static, const char* method_name, |
| 87 | const char* method_signature) |
| 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 89 | const char* class_name = is_static ? "StaticLeafMethods" : "NonStaticLeafMethods"; |
| 90 | jobject jclass_loader(LoadDex(class_name)); |
| 91 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 92 | StackHandleScope<2> hs(self); |
| 93 | Handle<mirror::ClassLoader> class_loader( |
| 94 | hs.NewHandle( |
| 95 | ScopedObjectAccessUnchecked(self).Decode<mirror::ClassLoader*>(jclass_loader))); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 96 | if (is_static) { |
| 97 | MakeExecutable(ScopedObjectAccessUnchecked(self).Decode<mirror::ClassLoader*>(jclass_loader), |
| 98 | class_name); |
| 99 | } else { |
| 100 | MakeExecutable(nullptr, "java.lang.Class"); |
| 101 | MakeExecutable(nullptr, "java.lang.Object"); |
| 102 | MakeExecutable(ScopedObjectAccessUnchecked(self).Decode<mirror::ClassLoader*>(jclass_loader), |
| 103 | class_name); |
| 104 | } |
| 105 | |
| 106 | mirror::Class* c = class_linker_->FindClass(self, DotToDescriptor(class_name).c_str(), |
| 107 | class_loader); |
| 108 | CHECK(c != NULL); |
| 109 | |
| 110 | *method = is_static ? c->FindDirectMethod(method_name, method_signature) |
| 111 | : c->FindVirtualMethod(method_name, method_signature); |
| 112 | CHECK(method != nullptr); |
| 113 | |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 114 | if (is_static) { |
| 115 | *receiver = nullptr; |
| 116 | } else { |
| 117 | // Ensure class is initialized before allocating object |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 118 | StackHandleScope<1> hs2(self); |
| 119 | Handle<mirror::Class> h_class(hs2.NewHandle(c)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 120 | bool initialized = class_linker_->EnsureInitialized(self, h_class, true, true); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 121 | CHECK(initialized); |
| 122 | *receiver = c->AllocObject(self); |
| 123 | } |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 124 | |
| 125 | // Start runtime. |
| 126 | bool started = runtime_->Start(); |
| 127 | CHECK(started); |
| 128 | self->TransitionFromSuspendedToRunnable(); |
| 129 | } |
| 130 | |
| 131 | void InvokeNopMethod(bool is_static) { |
| 132 | ScopedObjectAccess soa(env_); |
| 133 | mirror::ArtMethod* method; |
| 134 | mirror::Object* receiver; |
| 135 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "nop", "()V"); |
| 136 | InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), nullptr); |
| 137 | } |
| 138 | |
| 139 | void InvokeIdentityByteMethod(bool is_static) { |
| 140 | ScopedObjectAccess soa(env_); |
| 141 | mirror::ArtMethod* method; |
| 142 | mirror::Object* receiver; |
| 143 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "identity", "(B)B"); |
| 144 | jvalue args[1]; |
| 145 | |
| 146 | args[0].b = 0; |
| 147 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 148 | EXPECT_EQ(0, result.GetB()); |
| 149 | |
| 150 | args[0].b = -1; |
| 151 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 152 | EXPECT_EQ(-1, result.GetB()); |
| 153 | |
| 154 | args[0].b = SCHAR_MAX; |
| 155 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 156 | EXPECT_EQ(SCHAR_MAX, result.GetB()); |
| 157 | |
| 158 | args[0].b = (SCHAR_MIN << 24) >> 24; |
| 159 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 160 | EXPECT_EQ(SCHAR_MIN, result.GetB()); |
| 161 | } |
| 162 | |
| 163 | void InvokeIdentityIntMethod(bool is_static) { |
| 164 | ScopedObjectAccess soa(env_); |
| 165 | mirror::ArtMethod* method; |
| 166 | mirror::Object* receiver; |
| 167 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "identity", "(I)I"); |
| 168 | jvalue args[1]; |
| 169 | |
| 170 | args[0].i = 0; |
| 171 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 172 | EXPECT_EQ(0, result.GetI()); |
| 173 | |
| 174 | args[0].i = -1; |
| 175 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 176 | EXPECT_EQ(-1, result.GetI()); |
| 177 | |
| 178 | args[0].i = INT_MAX; |
| 179 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 180 | EXPECT_EQ(INT_MAX, result.GetI()); |
| 181 | |
| 182 | args[0].i = INT_MIN; |
| 183 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 184 | EXPECT_EQ(INT_MIN, result.GetI()); |
| 185 | } |
| 186 | |
| 187 | void InvokeIdentityDoubleMethod(bool is_static) { |
| 188 | ScopedObjectAccess soa(env_); |
| 189 | mirror::ArtMethod* method; |
| 190 | mirror::Object* receiver; |
| 191 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "identity", "(D)D"); |
| 192 | jvalue args[1]; |
| 193 | |
| 194 | args[0].d = 0.0; |
| 195 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 196 | EXPECT_DOUBLE_EQ(0.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 197 | |
| 198 | args[0].d = -1.0; |
| 199 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 200 | EXPECT_DOUBLE_EQ(-1.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 201 | |
| 202 | args[0].d = DBL_MAX; |
| 203 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 204 | EXPECT_DOUBLE_EQ(DBL_MAX, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 205 | |
| 206 | args[0].d = DBL_MIN; |
| 207 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 208 | EXPECT_DOUBLE_EQ(DBL_MIN, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void InvokeSumIntIntMethod(bool is_static) { |
| 212 | ScopedObjectAccess soa(env_); |
| 213 | mirror::ArtMethod* method; |
| 214 | mirror::Object* receiver; |
| 215 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(II)I"); |
| 216 | jvalue args[2]; |
| 217 | |
| 218 | args[0].i = 1; |
| 219 | args[1].i = 2; |
| 220 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 221 | EXPECT_EQ(3, result.GetI()); |
| 222 | |
| 223 | args[0].i = -2; |
| 224 | args[1].i = 5; |
| 225 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 226 | EXPECT_EQ(3, result.GetI()); |
| 227 | |
| 228 | args[0].i = INT_MAX; |
| 229 | args[1].i = INT_MIN; |
| 230 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 231 | EXPECT_EQ(-1, result.GetI()); |
| 232 | |
| 233 | args[0].i = INT_MAX; |
| 234 | args[1].i = INT_MAX; |
| 235 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 236 | EXPECT_EQ(-2, result.GetI()); |
| 237 | } |
| 238 | |
| 239 | void InvokeSumIntIntIntMethod(bool is_static) { |
| 240 | ScopedObjectAccess soa(env_); |
| 241 | mirror::ArtMethod* method; |
| 242 | mirror::Object* receiver; |
| 243 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(III)I"); |
| 244 | jvalue args[3]; |
| 245 | |
| 246 | args[0].i = 0; |
| 247 | args[1].i = 0; |
| 248 | args[2].i = 0; |
| 249 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 250 | EXPECT_EQ(0, result.GetI()); |
| 251 | |
| 252 | args[0].i = 1; |
| 253 | args[1].i = 2; |
| 254 | args[2].i = 3; |
| 255 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 256 | EXPECT_EQ(6, result.GetI()); |
| 257 | |
| 258 | args[0].i = -1; |
| 259 | args[1].i = 2; |
| 260 | args[2].i = -3; |
| 261 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 262 | EXPECT_EQ(-2, result.GetI()); |
| 263 | |
| 264 | args[0].i = INT_MAX; |
| 265 | args[1].i = INT_MIN; |
| 266 | args[2].i = INT_MAX; |
| 267 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 268 | EXPECT_EQ(2147483646, result.GetI()); |
| 269 | |
| 270 | args[0].i = INT_MAX; |
| 271 | args[1].i = INT_MAX; |
| 272 | args[2].i = INT_MAX; |
| 273 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 274 | EXPECT_EQ(2147483645, result.GetI()); |
| 275 | } |
| 276 | |
| 277 | void InvokeSumIntIntIntIntMethod(bool is_static) { |
| 278 | ScopedObjectAccess soa(env_); |
| 279 | mirror::ArtMethod* method; |
| 280 | mirror::Object* receiver; |
| 281 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(IIII)I"); |
| 282 | jvalue args[4]; |
| 283 | |
| 284 | args[0].i = 0; |
| 285 | args[1].i = 0; |
| 286 | args[2].i = 0; |
| 287 | args[3].i = 0; |
| 288 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 289 | EXPECT_EQ(0, result.GetI()); |
| 290 | |
| 291 | args[0].i = 1; |
| 292 | args[1].i = 2; |
| 293 | args[2].i = 3; |
| 294 | args[3].i = 4; |
| 295 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 296 | EXPECT_EQ(10, result.GetI()); |
| 297 | |
| 298 | args[0].i = -1; |
| 299 | args[1].i = 2; |
| 300 | args[2].i = -3; |
| 301 | args[3].i = 4; |
| 302 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 303 | EXPECT_EQ(2, result.GetI()); |
| 304 | |
| 305 | args[0].i = INT_MAX; |
| 306 | args[1].i = INT_MIN; |
| 307 | args[2].i = INT_MAX; |
| 308 | args[3].i = INT_MIN; |
| 309 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 310 | EXPECT_EQ(-2, result.GetI()); |
| 311 | |
| 312 | args[0].i = INT_MAX; |
| 313 | args[1].i = INT_MAX; |
| 314 | args[2].i = INT_MAX; |
| 315 | args[3].i = INT_MAX; |
| 316 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 317 | EXPECT_EQ(-4, result.GetI()); |
| 318 | } |
| 319 | |
| 320 | void InvokeSumIntIntIntIntIntMethod(bool is_static) { |
| 321 | ScopedObjectAccess soa(env_); |
| 322 | mirror::ArtMethod* method; |
| 323 | mirror::Object* receiver; |
| 324 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(IIIII)I"); |
| 325 | jvalue args[5]; |
| 326 | |
| 327 | args[0].i = 0; |
| 328 | args[1].i = 0; |
| 329 | args[2].i = 0; |
| 330 | args[3].i = 0; |
| 331 | args[4].i = 0; |
| 332 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 333 | EXPECT_EQ(0, result.GetI()); |
| 334 | |
| 335 | args[0].i = 1; |
| 336 | args[1].i = 2; |
| 337 | args[2].i = 3; |
| 338 | args[3].i = 4; |
| 339 | args[4].i = 5; |
| 340 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 341 | EXPECT_EQ(15, result.GetI()); |
| 342 | |
| 343 | args[0].i = -1; |
| 344 | args[1].i = 2; |
| 345 | args[2].i = -3; |
| 346 | args[3].i = 4; |
| 347 | args[4].i = -5; |
| 348 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 349 | EXPECT_EQ(-3, result.GetI()); |
| 350 | |
| 351 | args[0].i = INT_MAX; |
| 352 | args[1].i = INT_MIN; |
| 353 | args[2].i = INT_MAX; |
| 354 | args[3].i = INT_MIN; |
| 355 | args[4].i = INT_MAX; |
| 356 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 357 | EXPECT_EQ(2147483645, result.GetI()); |
| 358 | |
| 359 | args[0].i = INT_MAX; |
| 360 | args[1].i = INT_MAX; |
| 361 | args[2].i = INT_MAX; |
| 362 | args[3].i = INT_MAX; |
| 363 | args[4].i = INT_MAX; |
| 364 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
| 365 | EXPECT_EQ(2147483643, result.GetI()); |
| 366 | } |
| 367 | |
| 368 | void InvokeSumDoubleDoubleMethod(bool is_static) { |
| 369 | ScopedObjectAccess soa(env_); |
| 370 | mirror::ArtMethod* method; |
| 371 | mirror::Object* receiver; |
| 372 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(DD)D"); |
| 373 | jvalue args[2]; |
| 374 | |
| 375 | args[0].d = 0.0; |
| 376 | args[1].d = 0.0; |
| 377 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 378 | EXPECT_DOUBLE_EQ(0.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 379 | |
| 380 | args[0].d = 1.0; |
| 381 | args[1].d = 2.0; |
| 382 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 383 | EXPECT_DOUBLE_EQ(3.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 384 | |
| 385 | args[0].d = 1.0; |
| 386 | args[1].d = -2.0; |
| 387 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 388 | EXPECT_DOUBLE_EQ(-1.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 389 | |
| 390 | args[0].d = DBL_MAX; |
| 391 | args[1].d = DBL_MIN; |
| 392 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 393 | EXPECT_DOUBLE_EQ(1.7976931348623157e308, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 394 | |
| 395 | args[0].d = DBL_MAX; |
| 396 | args[1].d = DBL_MAX; |
| 397 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 398 | EXPECT_DOUBLE_EQ(INFINITY, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void InvokeSumDoubleDoubleDoubleMethod(bool is_static) { |
| 402 | ScopedObjectAccess soa(env_); |
| 403 | mirror::ArtMethod* method; |
| 404 | mirror::Object* receiver; |
| 405 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(DDD)D"); |
| 406 | jvalue args[3]; |
| 407 | |
| 408 | args[0].d = 0.0; |
| 409 | args[1].d = 0.0; |
| 410 | args[2].d = 0.0; |
| 411 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 412 | EXPECT_DOUBLE_EQ(0.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 413 | |
| 414 | args[0].d = 1.0; |
| 415 | args[1].d = 2.0; |
| 416 | args[2].d = 3.0; |
| 417 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 418 | EXPECT_DOUBLE_EQ(6.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 419 | |
| 420 | args[0].d = 1.0; |
| 421 | args[1].d = -2.0; |
| 422 | args[2].d = 3.0; |
| 423 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 424 | EXPECT_DOUBLE_EQ(2.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void InvokeSumDoubleDoubleDoubleDoubleMethod(bool is_static) { |
| 428 | ScopedObjectAccess soa(env_); |
| 429 | mirror::ArtMethod* method; |
| 430 | mirror::Object* receiver; |
| 431 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(DDDD)D"); |
| 432 | jvalue args[4]; |
| 433 | |
| 434 | args[0].d = 0.0; |
| 435 | args[1].d = 0.0; |
| 436 | args[2].d = 0.0; |
| 437 | args[3].d = 0.0; |
| 438 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 439 | EXPECT_DOUBLE_EQ(0.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 440 | |
| 441 | args[0].d = 1.0; |
| 442 | args[1].d = 2.0; |
| 443 | args[2].d = 3.0; |
| 444 | args[3].d = 4.0; |
| 445 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 446 | EXPECT_DOUBLE_EQ(10.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 447 | |
| 448 | args[0].d = 1.0; |
| 449 | args[1].d = -2.0; |
| 450 | args[2].d = 3.0; |
| 451 | args[3].d = -4.0; |
| 452 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 453 | EXPECT_DOUBLE_EQ(-2.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(bool is_static) { |
| 457 | ScopedObjectAccess soa(env_); |
| 458 | mirror::ArtMethod* method; |
| 459 | mirror::Object* receiver; |
| 460 | ReflectionTestMakeExecutable(&method, &receiver, is_static, "sum", "(DDDDD)D"); |
| 461 | jvalue args[5]; |
| 462 | |
| 463 | args[0].d = 0.0; |
| 464 | args[1].d = 0.0; |
| 465 | args[2].d = 0.0; |
| 466 | args[3].d = 0.0; |
| 467 | args[4].d = 0.0; |
| 468 | JValue result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 469 | EXPECT_DOUBLE_EQ(0.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 470 | |
| 471 | args[0].d = 1.0; |
| 472 | args[1].d = 2.0; |
| 473 | args[2].d = 3.0; |
| 474 | args[3].d = 4.0; |
| 475 | args[4].d = 5.0; |
| 476 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 477 | EXPECT_DOUBLE_EQ(15.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 478 | |
| 479 | args[0].d = 1.0; |
| 480 | args[1].d = -2.0; |
| 481 | args[2].d = 3.0; |
| 482 | args[3].d = -4.0; |
| 483 | args[4].d = 5.0; |
| 484 | result = InvokeWithJValues(soa, receiver, soa.EncodeMethod(method), args); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 485 | EXPECT_DOUBLE_EQ(3.0, result.GetD()); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | JavaVMExt* vm_; |
| 489 | JNIEnv* env_; |
| 490 | jclass aioobe_; |
| 491 | jclass ase_; |
| 492 | jclass sioobe_; |
| 493 | }; |
| 494 | |
| 495 | TEST_F(ReflectionTest, StaticMainMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 496 | ScopedObjectAccess soa(Thread::Current()); |
| 497 | jobject jclass_loader = LoadDex("Main"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 498 | StackHandleScope<1> hs(soa.Self()); |
| 499 | Handle<mirror::ClassLoader> class_loader( |
| 500 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader))); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 501 | CompileDirectMethod(class_loader, "Main", "main", "([Ljava/lang/String;)V"); |
| 502 | |
| 503 | mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LMain;", class_loader); |
| 504 | ASSERT_TRUE(klass != NULL); |
| 505 | |
| 506 | mirror::ArtMethod* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V"); |
| 507 | ASSERT_TRUE(method != NULL); |
| 508 | |
| 509 | // Start runtime. |
| 510 | bool started = runtime_->Start(); |
| 511 | CHECK(started); |
| 512 | soa.Self()->TransitionFromSuspendedToRunnable(); |
| 513 | |
| 514 | jvalue args[1]; |
| 515 | args[0].l = nullptr; |
| 516 | InvokeWithJValues(soa, nullptr, soa.EncodeMethod(method), args); |
| 517 | } |
| 518 | |
| 519 | TEST_F(ReflectionTest, StaticNopMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 520 | InvokeNopMethod(true); |
| 521 | } |
| 522 | |
| 523 | TEST_F(ReflectionTest, NonStaticNopMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 524 | InvokeNopMethod(false); |
| 525 | } |
| 526 | |
| 527 | TEST_F(ReflectionTest, StaticIdentityByteMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 528 | InvokeIdentityByteMethod(true); |
| 529 | } |
| 530 | |
| 531 | TEST_F(ReflectionTest, NonStaticIdentityByteMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 532 | InvokeIdentityByteMethod(false); |
| 533 | } |
| 534 | |
| 535 | TEST_F(ReflectionTest, StaticIdentityIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 536 | InvokeIdentityIntMethod(true); |
| 537 | } |
| 538 | |
| 539 | TEST_F(ReflectionTest, NonStaticIdentityIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 540 | InvokeIdentityIntMethod(false); |
| 541 | } |
| 542 | |
| 543 | TEST_F(ReflectionTest, StaticIdentityDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 544 | InvokeIdentityDoubleMethod(true); |
| 545 | } |
| 546 | |
| 547 | TEST_F(ReflectionTest, NonStaticIdentityDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 548 | InvokeIdentityDoubleMethod(false); |
| 549 | } |
| 550 | |
| 551 | TEST_F(ReflectionTest, StaticSumIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 552 | InvokeSumIntIntMethod(true); |
| 553 | } |
| 554 | |
| 555 | TEST_F(ReflectionTest, NonStaticSumIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 556 | InvokeSumIntIntMethod(false); |
| 557 | } |
| 558 | |
| 559 | TEST_F(ReflectionTest, StaticSumIntIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 560 | InvokeSumIntIntIntMethod(true); |
| 561 | } |
| 562 | |
| 563 | TEST_F(ReflectionTest, NonStaticSumIntIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 564 | InvokeSumIntIntIntMethod(false); |
| 565 | } |
| 566 | |
| 567 | TEST_F(ReflectionTest, StaticSumIntIntIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 568 | InvokeSumIntIntIntIntMethod(true); |
| 569 | } |
| 570 | |
| 571 | TEST_F(ReflectionTest, NonStaticSumIntIntIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 572 | InvokeSumIntIntIntIntMethod(false); |
| 573 | } |
| 574 | |
| 575 | TEST_F(ReflectionTest, StaticSumIntIntIntIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 576 | InvokeSumIntIntIntIntIntMethod(true); |
| 577 | } |
| 578 | |
| 579 | TEST_F(ReflectionTest, NonStaticSumIntIntIntIntIntMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 580 | InvokeSumIntIntIntIntIntMethod(false); |
| 581 | } |
| 582 | |
| 583 | TEST_F(ReflectionTest, StaticSumDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 584 | InvokeSumDoubleDoubleMethod(true); |
| 585 | } |
| 586 | |
| 587 | TEST_F(ReflectionTest, NonStaticSumDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 588 | InvokeSumDoubleDoubleMethod(false); |
| 589 | } |
| 590 | |
| 591 | TEST_F(ReflectionTest, StaticSumDoubleDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 592 | InvokeSumDoubleDoubleDoubleMethod(true); |
| 593 | } |
| 594 | |
| 595 | TEST_F(ReflectionTest, NonStaticSumDoubleDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 596 | InvokeSumDoubleDoubleDoubleMethod(false); |
| 597 | } |
| 598 | |
| 599 | TEST_F(ReflectionTest, StaticSumDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 600 | InvokeSumDoubleDoubleDoubleDoubleMethod(true); |
| 601 | } |
| 602 | |
| 603 | TEST_F(ReflectionTest, NonStaticSumDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 604 | InvokeSumDoubleDoubleDoubleDoubleMethod(false); |
| 605 | } |
| 606 | |
| 607 | TEST_F(ReflectionTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 608 | InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(true); |
| 609 | } |
| 610 | |
| 611 | TEST_F(ReflectionTest, NonStaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 612 | InvokeSumDoubleDoubleDoubleDoubleDoubleMethod(false); |
| 613 | } |
| 614 | |
| 615 | } // namespace art |