blob: 45ec0ad5a251ec70899472db3ab07f36e499d9ac [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
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070017#include "class_linker.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 Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.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 Rogers00f7d0e2012-07-19 15:28:27 -070030 ScopedObjectAccess soa(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070031 DCHECK(javaElementClass != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032 mirror::Class* element_class = 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");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 mirror::IntArray* dimensions_array = down_cast<mirror::IntArray*>(dimensions_obj);
39 mirror::Array* new_array = mirror::Array::CreateMultiArray(soa.Self(), element_class, dimensions_array);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070041}
42
Elliott Hughes0512f022012-03-15 22:10:52 -070043static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 ScopedObjectAccess soa(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070045 DCHECK(javaElementClass != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 mirror::Class* element_class = soa.Decode<mirror::Class*>(javaElementClass);
Ian Rogers64b6d142012-10-29 16:34:15 -070047 if (UNLIKELY(length < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080048 ThrowNegativeArraySizeException(length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070049 return NULL;
50 }
Ian Rogers64b6d142012-10-29 16:34:15 -070051 std::string descriptor("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080052 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070053
54 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055 mirror::Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
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 }
60 DCHECK(array_class->IsArrayClass());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::Array* new_array = mirror::Array::Alloc(soa.Self(), array_class, length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070063}
64
65static JNINativeMethod gMethods[] = {
66 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
67 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
68};
69
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070070void register_java_lang_reflect_Array(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070071 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070072}
73
74} // namespace art