blob: 863f9fce7b78468c08de29a187c5368623f3d4e7 [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"
Elliott Hugheseac76672012-05-24 21:56:51 -070018#include "jni_internal.h"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070019#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070022#include "sirt_ref.h"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070023
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070024namespace 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.
Ian Rogers1f539342012-10-03 21:09:42 -070028static Array* CreateMultiArray(Thread* self, Class* array_class, int current_dimension,
29 IntArray* dimensions)
Ian Rogersb726dcb2012-09-05 08:57:23 -070030 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070031 int32_t array_length = dimensions->Get(current_dimension++);
Ian Rogers50b35e22012-10-04 10:09:15 -070032 SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070033 if (new_array.get() == NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -070034 CHECK(self->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070035 return NULL;
36 }
37 if (current_dimension == dimensions->GetLength()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070038 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070039 }
40
41 if (!array_class->GetComponentType()->IsArrayClass()) {
42 // TODO: throw an exception, not relying on class_linker->FindClass to throw.
43 // old code assumed this but if you recurse from "[Foo" to "Foo" to "oo",
44 // you shouldn't assume there isn't a class "oo".
45 }
Elliott Hughes91250e02011-12-13 22:30:35 -080046 std::string sub_array_descriptor(ClassHelper(array_class).GetDescriptor() + 1);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070047 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080048 Class* sub_array_class = class_linker->FindClass(sub_array_descriptor.c_str(),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070049 array_class->GetClassLoader());
50 if (sub_array_class == NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -070051 CHECK(self->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070052 return NULL;
53 }
54 DCHECK(sub_array_class->IsArrayClass());
55 // Create a new sub-array in every element of the array.
Ian Rogers1f539342012-10-03 21:09:42 -070056 SirtRef<ObjectArray<Array> > object_array(self, new_array->AsObjectArray<Array>());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070057 for (int32_t i = 0; i < array_length; i++) {
Ian Rogers1f539342012-10-03 21:09:42 -070058 SirtRef<Array> sub_array(self, CreateMultiArray(self, sub_array_class, current_dimension,
59 dimensions));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070060 if (sub_array.get() == NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -070061 CHECK(self->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070062 return NULL;
63 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070064 object_array->Set(i, sub_array.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070065 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070066 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070067}
68
69// Create a multi-dimensional array of Objects or primitive types.
70//
71// We have to generate the names for X[], X[][], X[][][], and so on. The
72// easiest way to deal with that is to create the full name once and then
73// subtract pieces off. Besides, we want to start with the outermost
74// piece and work our way in.
Elliott Hughes0512f022012-03-15 22:10:52 -070075static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 ScopedObjectAccess soa(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070077 DCHECK(javaElementClass != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 Class* element_class = soa.Decode<Class*>(javaElementClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070079 DCHECK(element_class->IsClass());
80 DCHECK(javaDimArray != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 Object* dimensions_obj = soa.Decode<Object*>(javaDimArray);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070082 DCHECK(dimensions_obj->IsArrayInstance());
Elliott Hughes91250e02011-12-13 22:30:35 -080083 DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070084 IntArray* dimensions_array = down_cast<IntArray*>(dimensions_obj);
85
86 // Verify dimensions.
87 //
88 // The caller is responsible for verifying that "dimArray" is non-null
89 // and has a length > 0 and <= 255.
90 int num_dimensions = dimensions_array->GetLength();
91 DCHECK_GT(num_dimensions, 0);
92 DCHECK_LE(num_dimensions, 255);
93
94 for (int i = 0; i < num_dimensions; i++) {
Elliott Hughes6271c422011-10-11 15:43:35 -070095 int dimension = dimensions_array->Get(i);
96 if (dimension < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
Elliott Hughes6271c422011-10-11 15:43:35 -070098 "Dimension %d: %d", i, dimension);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070099 return NULL;
100 }
101 }
102
103 // Generate the full name of the array class.
104 std::string descriptor(num_dimensions, '[');
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700106
107 // Find/generate the array class.
108 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800109 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700110 if (array_class == NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -0700111 CHECK(soa.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700112 return NULL;
113 }
114 // create the array
Ian Rogers1f539342012-10-03 21:09:42 -0700115 Array* new_array = CreateMultiArray(soa.Self(), array_class, 0, dimensions_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700116 if (new_array == NULL) {
Ian Rogers1f539342012-10-03 21:09:42 -0700117 CHECK(soa.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700118 return NULL;
119 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700121}
122
Elliott Hughes0512f022012-03-15 22:10:52 -0700123static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 ScopedObjectAccess soa(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700125 DCHECK(javaElementClass != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 Class* element_class = soa.Decode<Class*>(javaElementClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700127 if (length < 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700129 return NULL;
130 }
131 std::string descriptor;
132 descriptor += '[';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700134
135 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800136 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700137 if (array_class == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 CHECK(soa.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700139 return NULL;
140 }
141 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -0700142 Array* new_array = Array::Alloc(soa.Self(), array_class, length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700143 if (new_array == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 CHECK(soa.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700145 return NULL;
146 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 return soa.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700148}
149
150static JNINativeMethod gMethods[] = {
151 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
152 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
153};
154
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700155void register_java_lang_reflect_Array(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700156 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700157}
158
159} // namespace art