blob: 4a5f797685e216316cdd3112180ef28c8db17382 [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
17#include "jni_internal.h"
18#include "class_linker.h"
19#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "object_utils.h"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070021
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070026// Recursively create an array with multiple dimensions. Elements may be
27// Objects or primitive types.
Elliott Hughes0512f022012-03-15 22:10:52 -070028static Array* CreateMultiArray(Class* array_class, int current_dimension, IntArray* dimensions) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070029 int32_t array_length = dimensions->Get(current_dimension++);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070030 SirtRef<Array> new_array(Array::Alloc(array_class, array_length));
31 if (new_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070032 CHECK(Thread::Current()->IsExceptionPending());
33 return NULL;
34 }
35 if (current_dimension == dimensions->GetLength()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070036 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070037 }
38
39 if (!array_class->GetComponentType()->IsArrayClass()) {
40 // TODO: throw an exception, not relying on class_linker->FindClass to throw.
41 // old code assumed this but if you recurse from "[Foo" to "Foo" to "oo",
42 // you shouldn't assume there isn't a class "oo".
43 }
Elliott Hughes91250e02011-12-13 22:30:35 -080044 std::string sub_array_descriptor(ClassHelper(array_class).GetDescriptor() + 1);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070045 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080046 Class* sub_array_class = class_linker->FindClass(sub_array_descriptor.c_str(),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070047 array_class->GetClassLoader());
48 if (sub_array_class == NULL) {
49 CHECK(Thread::Current()->IsExceptionPending());
50 return NULL;
51 }
52 DCHECK(sub_array_class->IsArrayClass());
53 // Create a new sub-array in every element of the array.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070054 SirtRef<ObjectArray<Array> > object_array(new_array->AsObjectArray<Array>());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070055 for (int32_t i = 0; i < array_length; i++) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070056 SirtRef<Array> sub_array(CreateMultiArray(sub_array_class, current_dimension, dimensions));
57 if (sub_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070058 CHECK(Thread::Current()->IsExceptionPending());
59 return NULL;
60 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070061 object_array->Set(i, sub_array.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070062 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070063 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070064}
65
66// Create a multi-dimensional array of Objects or primitive types.
67//
68// We have to generate the names for X[], X[][], X[][][], and so on. The
69// easiest way to deal with that is to create the full name once and then
70// subtract pieces off. Besides, we want to start with the outermost
71// piece and work our way in.
Elliott Hughes0512f022012-03-15 22:10:52 -070072static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070073 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070074 DCHECK(javaElementClass != NULL);
75 Class* element_class = Decode<Class*>(env, javaElementClass);
76 DCHECK(element_class->IsClass());
77 DCHECK(javaDimArray != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078 Object* dimensions_obj = Decode<Object*>(env, javaDimArray);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070079 DCHECK(dimensions_obj->IsArrayInstance());
Elliott Hughes91250e02011-12-13 22:30:35 -080080 DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070081 IntArray* dimensions_array = down_cast<IntArray*>(dimensions_obj);
82
83 // Verify dimensions.
84 //
85 // The caller is responsible for verifying that "dimArray" is non-null
86 // and has a length > 0 and <= 255.
87 int num_dimensions = dimensions_array->GetLength();
88 DCHECK_GT(num_dimensions, 0);
89 DCHECK_LE(num_dimensions, 255);
90
91 for (int i = 0; i < num_dimensions; i++) {
Elliott Hughes6271c422011-10-11 15:43:35 -070092 int dimension = dimensions_array->Get(i);
93 if (dimension < 0) {
94 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
95 "Dimension %d: %d", i, dimension);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070096 return NULL;
97 }
98 }
99
100 // Generate the full name of the array class.
101 std::string descriptor(num_dimensions, '[');
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800102 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700103
104 // Find/generate the array class.
105 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800106 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700107 if (array_class == NULL) {
108 CHECK(Thread::Current()->IsExceptionPending());
109 return NULL;
110 }
111 // create the array
112 Array* new_array = CreateMultiArray(array_class, 0, dimensions_array);
113 if (new_array == NULL) {
114 CHECK(Thread::Current()->IsExceptionPending());
115 return NULL;
116 }
117 return AddLocalReference<jobject>(env, new_array);
118}
119
Elliott Hughes0512f022012-03-15 22:10:52 -0700120static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700121 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700122 DCHECK(javaElementClass != NULL);
123 Class* element_class = Decode<Class*>(env, javaElementClass);
124 if (length < 0) {
Elliott Hughes6271c422011-10-11 15:43:35 -0700125 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700126 return NULL;
127 }
128 std::string descriptor;
129 descriptor += '[';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700131
132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800133 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700134 if (array_class == NULL) {
135 CHECK(Thread::Current()->IsExceptionPending());
136 return NULL;
137 }
138 DCHECK(array_class->IsArrayClass());
139 Array* new_array = Array::Alloc(array_class, length);
140 if (new_array == NULL) {
141 CHECK(Thread::Current()->IsExceptionPending());
142 return NULL;
143 }
144 return AddLocalReference<jobject>(env, new_array);
145}
146
147static JNINativeMethod gMethods[] = {
148 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
149 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
150};
151
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700152void register_java_lang_reflect_Array(JNIEnv* env) {
153 jniRegisterNativeMethods(env, "java/lang/reflect/Array", gMethods, NELEM(gMethods));
154}
155
156} // namespace art