blob: ea635d3647c6516ac9de24d173cf90ac8b0b54ca [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"
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070021
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070022namespace art {
23
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070024// Recursively create an array with multiple dimensions. Elements may be
25// Objects or primitive types.
Elliott Hughes0512f022012-03-15 22:10:52 -070026static Array* CreateMultiArray(Class* array_class, int current_dimension, IntArray* dimensions) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070027 int32_t array_length = dimensions->Get(current_dimension++);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070028 SirtRef<Array> new_array(Array::Alloc(array_class, array_length));
29 if (new_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070030 CHECK(Thread::Current()->IsExceptionPending());
31 return NULL;
32 }
33 if (current_dimension == dimensions->GetLength()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070034 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070035 }
36
37 if (!array_class->GetComponentType()->IsArrayClass()) {
38 // TODO: throw an exception, not relying on class_linker->FindClass to throw.
39 // old code assumed this but if you recurse from "[Foo" to "Foo" to "oo",
40 // you shouldn't assume there isn't a class "oo".
41 }
Elliott Hughes91250e02011-12-13 22:30:35 -080042 std::string sub_array_descriptor(ClassHelper(array_class).GetDescriptor() + 1);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070043 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080044 Class* sub_array_class = class_linker->FindClass(sub_array_descriptor.c_str(),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070045 array_class->GetClassLoader());
46 if (sub_array_class == NULL) {
47 CHECK(Thread::Current()->IsExceptionPending());
48 return NULL;
49 }
50 DCHECK(sub_array_class->IsArrayClass());
51 // Create a new sub-array in every element of the array.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070052 SirtRef<ObjectArray<Array> > object_array(new_array->AsObjectArray<Array>());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070053 for (int32_t i = 0; i < array_length; i++) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070054 SirtRef<Array> sub_array(CreateMultiArray(sub_array_class, current_dimension, dimensions));
55 if (sub_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070056 CHECK(Thread::Current()->IsExceptionPending());
57 return NULL;
58 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070059 object_array->Set(i, sub_array.get());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070060 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -070061 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070062}
63
64// Create a multi-dimensional array of Objects or primitive types.
65//
66// We have to generate the names for X[], X[][], X[][][], and so on. The
67// easiest way to deal with that is to create the full name once and then
68// subtract pieces off. Besides, we want to start with the outermost
69// piece and work our way in.
Elliott Hughes0512f022012-03-15 22:10:52 -070070static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
Elliott Hughes34e06962012-04-09 13:55:55 -070071 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070072 DCHECK(javaElementClass != NULL);
73 Class* element_class = Decode<Class*>(env, javaElementClass);
74 DCHECK(element_class->IsClass());
75 DCHECK(javaDimArray != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080076 Object* dimensions_obj = Decode<Object*>(env, javaDimArray);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070077 DCHECK(dimensions_obj->IsArrayInstance());
Elliott Hughes91250e02011-12-13 22:30:35 -080078 DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070079 IntArray* dimensions_array = down_cast<IntArray*>(dimensions_obj);
80
81 // Verify dimensions.
82 //
83 // The caller is responsible for verifying that "dimArray" is non-null
84 // and has a length > 0 and <= 255.
85 int num_dimensions = dimensions_array->GetLength();
86 DCHECK_GT(num_dimensions, 0);
87 DCHECK_LE(num_dimensions, 255);
88
89 for (int i = 0; i < num_dimensions; i++) {
Elliott Hughes6271c422011-10-11 15:43:35 -070090 int dimension = dimensions_array->Get(i);
91 if (dimension < 0) {
92 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
93 "Dimension %d: %d", i, dimension);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070094 return NULL;
95 }
96 }
97
98 // Generate the full name of the array class.
99 std::string descriptor(num_dimensions, '[');
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800100 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700101
102 // Find/generate the array class.
103 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800104 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700105 if (array_class == NULL) {
106 CHECK(Thread::Current()->IsExceptionPending());
107 return NULL;
108 }
109 // create the array
110 Array* new_array = CreateMultiArray(array_class, 0, dimensions_array);
111 if (new_array == NULL) {
112 CHECK(Thread::Current()->IsExceptionPending());
113 return NULL;
114 }
115 return AddLocalReference<jobject>(env, new_array);
116}
117
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700119 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700120 DCHECK(javaElementClass != NULL);
121 Class* element_class = Decode<Class*>(env, javaElementClass);
122 if (length < 0) {
Elliott Hughes6271c422011-10-11 15:43:35 -0700123 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700124 return NULL;
125 }
126 std::string descriptor;
127 descriptor += '[';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700129
130 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800131 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700132 if (array_class == NULL) {
133 CHECK(Thread::Current()->IsExceptionPending());
134 return NULL;
135 }
136 DCHECK(array_class->IsArrayClass());
137 Array* new_array = Array::Alloc(array_class, length);
138 if (new_array == NULL) {
139 CHECK(Thread::Current()->IsExceptionPending());
140 return NULL;
141 }
142 return AddLocalReference<jobject>(env, new_array);
143}
144
145static JNINativeMethod gMethods[] = {
146 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
147 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
148};
149
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700150void register_java_lang_reflect_Array(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700151 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700152}
153
154} // namespace art