blob: a9918188f4705ab995e0ae687439a2f07f6361b4 [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
Ian Rogers98379392014-02-24 16:53:16 -080017#include "class_linker-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080018#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070019#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070020#include "jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/object-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object_utils.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070024#include "scoped_fast_native_object_access.h"
Ian Rogers1f539342012-10-03 21:09:42 -070025#include "sirt_ref.h"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070026
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070027namespace art {
28
Elliott Hughes0512f022012-03-15 22:10:52 -070029static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070030 ScopedFastNativeObjectAccess soa(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070031 DCHECK(javaElementClass != NULL);
Mathieu Chartier5bb99032014-02-08 16:20:58 -080032 SirtRef<mirror::Class> element_class(soa.Self(), soa.Decode<mirror::Class*>(javaElementClass));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070033 DCHECK(element_class->IsClass());
34 DCHECK(javaDimArray != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 mirror::Object* dimensions_obj = soa.Decode<mirror::Object*>(javaDimArray);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070036 DCHECK(dimensions_obj->IsArrayInstance());
Elliott Hughes91250e02011-12-13 22:30:35 -080037 DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I");
Mathieu Chartier5bb99032014-02-08 16:20:58 -080038 SirtRef<mirror::IntArray> dimensions_array(soa.Self(),
39 down_cast<mirror::IntArray*>(dimensions_obj));
40 mirror::Array* new_array = mirror::Array::CreateMultiArray(soa.Self(), element_class,
41 dimensions_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070043}
44
Elliott Hughes0512f022012-03-15 22:10:52 -070045static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070046 ScopedFastNativeObjectAccess soa(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070047 DCHECK(javaElementClass != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 mirror::Class* element_class = soa.Decode<mirror::Class*>(javaElementClass);
Ian Rogers64b6d142012-10-29 16:34:15 -070049 if (UNLIKELY(length < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080050 ThrowNegativeArraySizeException(length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070051 return NULL;
52 }
Ian Rogers6fac4472014-02-25 17:01:10 -080053 Runtime* runtime = Runtime::Current();
54 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -080055 mirror::Class* array_class = class_linker->FindArrayClass(soa.Self(), element_class);
Ian Rogers64b6d142012-10-29 16:34:15 -070056 if (UNLIKELY(array_class == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057 CHECK(soa.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070058 return NULL;
59 }
Ian Rogers6fac4472014-02-25 17:01:10 -080060 DCHECK(array_class->IsObjectArrayClass());
61 mirror::Array* new_array = mirror::Array::Alloc<true>(soa.Self(), array_class, length,
62 sizeof(mirror::HeapReference<mirror::Object>),
63 runtime->GetHeap()->GetCurrentAllocator());
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070065}
66
67static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -070068 NATIVE_METHOD(Array, createMultiArray, "!(Ljava/lang/Class;[I)Ljava/lang/Object;"),
69 NATIVE_METHOD(Array, createObjectArray, "!(Ljava/lang/Class;I)Ljava/lang/Object;"),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070070};
71
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070072void register_java_lang_reflect_Array(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070073 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070074}
75
76} // namespace art