blob: 729312ed94e6dc0c75173e0d3ca33b071d6e13f2 [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 Rogers365c1022012-06-22 15:05:28 -070021#include "scoped_jni_thread_state.h"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070022
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070023namespace art {
24
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070025// Recursively create an array with multiple dimensions. Elements may be
26// Objects or primitive types.
Elliott Hughes0512f022012-03-15 22:10:52 -070027static Array* CreateMultiArray(Class* array_class, int current_dimension, IntArray* dimensions) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070028 int32_t array_length = dimensions->Get(current_dimension++);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070029 SirtRef<Array> new_array(Array::Alloc(array_class, array_length));
30 if (new_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070031 CHECK(Thread::Current()->IsExceptionPending());
32 return NULL;
33 }
34 if (current_dimension == dimensions->GetLength()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070035 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070036 }
37
38 if (!array_class->GetComponentType()->IsArrayClass()) {
39 // TODO: throw an exception, not relying on class_linker->FindClass to throw.
40 // old code assumed this but if you recurse from "[Foo" to "Foo" to "oo",
41 // you shouldn't assume there isn't a class "oo".
42 }
Elliott Hughes91250e02011-12-13 22:30:35 -080043 std::string sub_array_descriptor(ClassHelper(array_class).GetDescriptor() + 1);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070044 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080045 Class* sub_array_class = class_linker->FindClass(sub_array_descriptor.c_str(),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070046 array_class->GetClassLoader());
47 if (sub_array_class == NULL) {
48 CHECK(Thread::Current()->IsExceptionPending());
49 return NULL;
50 }
51 DCHECK(sub_array_class->IsArrayClass());
52 // Create a new sub-array in every element of the array.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070053 SirtRef<ObjectArray<Array> > object_array(new_array->AsObjectArray<Array>());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070054 for (int32_t i = 0; i < array_length; i++) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070055 SirtRef<Array> sub_array(CreateMultiArray(sub_array_class, current_dimension, dimensions));
56 if (sub_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070057 CHECK(Thread::Current()->IsExceptionPending());
58 return NULL;
59 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070060 object_array->Set(i, sub_array.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070061 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070062 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070063}
64
65// Create a multi-dimensional array of Objects or primitive types.
66//
67// We have to generate the names for X[], X[][], X[][][], and so on. The
68// easiest way to deal with that is to create the full name once and then
69// subtract pieces off. Besides, we want to start with the outermost
70// piece and work our way in.
Elliott Hughes0512f022012-03-15 22:10:52 -070071static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
Ian Rogers365c1022012-06-22 15:05:28 -070072 ScopedJniThreadState ts(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070073 DCHECK(javaElementClass != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -070074 Class* element_class = ts.Decode<Class*>(javaElementClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070075 DCHECK(element_class->IsClass());
76 DCHECK(javaDimArray != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -070077 Object* dimensions_obj = ts.Decode<Object*>(javaDimArray);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070078 DCHECK(dimensions_obj->IsArrayInstance());
Elliott Hughes91250e02011-12-13 22:30:35 -080079 DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070080 IntArray* dimensions_array = down_cast<IntArray*>(dimensions_obj);
81
82 // Verify dimensions.
83 //
84 // The caller is responsible for verifying that "dimArray" is non-null
85 // and has a length > 0 and <= 255.
86 int num_dimensions = dimensions_array->GetLength();
87 DCHECK_GT(num_dimensions, 0);
88 DCHECK_LE(num_dimensions, 255);
89
90 for (int i = 0; i < num_dimensions; i++) {
Elliott Hughes6271c422011-10-11 15:43:35 -070091 int dimension = dimensions_array->Get(i);
92 if (dimension < 0) {
Ian Rogers365c1022012-06-22 15:05:28 -070093 ts.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
Elliott Hughes6271c422011-10-11 15:43:35 -070094 "Dimension %d: %d", i, dimension);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070095 return NULL;
96 }
97 }
98
99 // Generate the full name of the array class.
100 std::string descriptor(num_dimensions, '[');
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800101 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700102
103 // Find/generate the array class.
104 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800105 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700106 if (array_class == NULL) {
107 CHECK(Thread::Current()->IsExceptionPending());
108 return NULL;
109 }
110 // create the array
111 Array* new_array = CreateMultiArray(array_class, 0, dimensions_array);
112 if (new_array == NULL) {
113 CHECK(Thread::Current()->IsExceptionPending());
114 return NULL;
115 }
Ian Rogers365c1022012-06-22 15:05:28 -0700116 return ts.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700117}
118
Elliott Hughes0512f022012-03-15 22:10:52 -0700119static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Ian Rogers365c1022012-06-22 15:05:28 -0700120 ScopedJniThreadState ts(env);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700121 DCHECK(javaElementClass != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700122 Class* element_class = ts.Decode<Class*>(javaElementClass);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700123 if (length < 0) {
Ian Rogers365c1022012-06-22 15:05:28 -0700124 ts.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700125 return NULL;
126 }
127 std::string descriptor;
128 descriptor += '[';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700130
131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800132 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700133 if (array_class == NULL) {
Ian Rogers365c1022012-06-22 15:05:28 -0700134 CHECK(ts.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700135 return NULL;
136 }
137 DCHECK(array_class->IsArrayClass());
138 Array* new_array = Array::Alloc(array_class, length);
139 if (new_array == NULL) {
Ian Rogers365c1022012-06-22 15:05:28 -0700140 CHECK(ts.Self()->IsExceptionPending());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700141 return NULL;
142 }
Ian Rogers365c1022012-06-22 15:05:28 -0700143 return ts.AddLocalReference<jobject>(new_array);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700144}
145
146static JNINativeMethod gMethods[] = {
147 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
148 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
149};
150
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700151void register_java_lang_reflect_Array(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700152 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700153}
154
155} // namespace art