blob: ff94593cdf5a483df8859fef50522414c7bcc93d [file] [log] [blame]
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001/*
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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_reflect_Array.h"
18
Andreas Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Ian Rogers98379392014-02-24 16:53:16 -080021#include "class_linker-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080022#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070024#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010025#include "jni/jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "mirror/class-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070027#include "mirror/object_array-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070029#include "native_util.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_fast_native_object_access-inl.h"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070031
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070032namespace art {
33
Mathieu Chartier2cebb242015-04-21 16:50:40 -070034static jobject Array_createMultiArray(
Igor Murashkin06537f72018-02-22 15:03:05 -080035 JNIEnv* env, jclass, jclass javaElementClass, jintArray javaDimArray) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070036 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 DCHECK(javaElementClass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070039 Handle<mirror::Class> element_class(hs.NewHandle(soa.Decode<mirror::Class>(javaElementClass)));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070040 DCHECK(element_class->IsClass());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 DCHECK(javaDimArray != nullptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070042 ObjPtr<mirror::Object> dimensions_obj = soa.Decode<mirror::Object>(javaDimArray);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070043 DCHECK(dimensions_obj->IsArrayInstance());
Ian Rogers1ff3c982014-08-12 02:30:58 -070044 DCHECK_EQ(dimensions_obj->GetClass()->GetComponentType()->GetPrimitiveType(),
45 Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046 Handle<mirror::IntArray> dimensions_array(
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070047 hs.NewHandle(ObjPtr<mirror::IntArray>::DownCast(dimensions_obj)));
Vladimir Markobcf17522018-06-01 13:14:32 +010048 ObjPtr<mirror::Array> new_array =
49 mirror::Array::CreateMultiArray(soa.Self(), element_class, dimensions_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070051}
52
Elliott Hughes0512f022012-03-15 22:10:52 -070053static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070054 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070055 DCHECK(javaElementClass != nullptr);
Ian Rogers64b6d142012-10-29 16:34:15 -070056 if (UNLIKELY(length < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080057 ThrowNegativeArraySizeException(length);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070058 return nullptr;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070059 }
Ian Rogers6fac4472014-02-25 17:01:10 -080060 Runtime* runtime = Runtime::Current();
61 ClassLinker* class_linker = runtime->GetClassLinker();
Vladimir Markobcf17522018-06-01 13:14:32 +010062 ObjPtr<mirror::Class> array_class =
63 class_linker->FindArrayClass(soa.Self(), soa.Decode<mirror::Class>(javaElementClass));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070064 if (UNLIKELY(array_class == nullptr)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 CHECK(soa.Self()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 return nullptr;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070067 }
Ian Rogers6fac4472014-02-25 17:01:10 -080068 DCHECK(array_class->IsObjectArrayClass());
Vladimir Markobcf17522018-06-01 13:14:32 +010069 ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object>::Alloc(
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070070 soa.Self(),
71 array_class,
72 length,
73 runtime->GetHeap()->GetCurrentAllocator());
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070075}
76
77static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080078 FAST_NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
79 FAST_NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070080};
81
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070082void register_java_lang_reflect_Array(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070083 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070084}
85
86} // namespace art