| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_reflect_Array.h" |
| 18 | |
| Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 19 | #include "nativehelper/jni_macros.h" |
| 20 | |
| Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 22 | #include "common_throws.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "dex/dex_file-inl.h" |
| Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 24 | #include "handle_scope-inl.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 25 | #include "jni/jni_internal.h" |
| Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
| Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 28 | #include "native_util.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 29 | #include "scoped_fast_native_object_access-inl.h" |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 30 | |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 33 | static jobject Array_createMultiArray( |
| Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 34 | JNIEnv* env, jclass, jclass javaElementClass, jintArray javaDimArray) { |
| Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 35 | ScopedFastNativeObjectAccess soa(env); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 36 | DCHECK(javaElementClass != nullptr); |
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 | StackHandleScope<2> hs(soa.Self()); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | Handle<mirror::Class> element_class(hs.NewHandle(soa.Decode<mirror::Class>(javaElementClass))); |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 39 | DCHECK(element_class->IsClass()); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 40 | DCHECK(javaDimArray != nullptr); |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 41 | ObjPtr<mirror::Object> dimensions_obj = soa.Decode<mirror::Object>(javaDimArray); |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 42 | DCHECK(dimensions_obj->IsArrayInstance()); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 43 | DCHECK_EQ(dimensions_obj->GetClass()->GetComponentType()->GetPrimitiveType(), |
| 44 | Primitive::kPrimInt); |
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 45 | Handle<mirror::IntArray> dimensions_array( |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 46 | hs.NewHandle(ObjPtr<mirror::IntArray>::DownCast(dimensions_obj))); |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 47 | ObjPtr<mirror::Array> new_array = |
| 48 | mirror::Array::CreateMultiArray(soa.Self(), element_class, dimensions_array); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 49 | return soa.AddLocalReference<jobject>(new_array); |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 52 | static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) { |
| Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 53 | ScopedFastNativeObjectAccess soa(env); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 54 | DCHECK(javaElementClass != nullptr); |
| Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 55 | if (UNLIKELY(length < 0)) { |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 56 | ThrowNegativeArraySizeException(length); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 57 | return nullptr; |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 58 | } |
| Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 59 | Runtime* runtime = Runtime::Current(); |
| 60 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 61 | ObjPtr<mirror::Class> array_class = |
| 62 | class_linker->FindArrayClass(soa.Self(), soa.Decode<mirror::Class>(javaElementClass)); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 63 | if (UNLIKELY(array_class == nullptr)) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | CHECK(soa.Self()->IsExceptionPending()); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 65 | return nullptr; |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 66 | } |
| Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 67 | DCHECK(array_class->IsObjectArrayClass()); |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 68 | ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object>::Alloc( |
| Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 69 | soa.Self(), |
| 70 | array_class, |
| 71 | length, |
| 72 | runtime->GetHeap()->GetCurrentAllocator()); |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 73 | return soa.AddLocalReference<jobject>(new_array); |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static JNINativeMethod gMethods[] = { |
| Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 77 | FAST_NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"), |
| 78 | FAST_NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"), |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 81 | void register_java_lang_reflect_Array(JNIEnv* env) { |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 82 | REGISTER_NATIVE_METHODS("java/lang/reflect/Array"); |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace art |