blob: f1456b10d310c7ab002260b0b1090ec7500e66b6 [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"
20
21#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
22
23namespace art {
24
25namespace {
26
27
28// Recursively create an array with multiple dimensions. Elements may be
29// Objects or primitive types.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070030Array* CreateMultiArray(Class* array_class, int current_dimension, IntArray* dimensions) {
31 int32_t array_length = dimensions->Get(current_dimension++);
32 Array* new_array = Array::Alloc(array_class, array_length);
33 if (new_array == NULL) {
34 CHECK(Thread::Current()->IsExceptionPending());
35 return NULL;
36 }
37 if (current_dimension == dimensions->GetLength()) {
38 return new_array;
39 }
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 }
46 std::string sub_array_descriptor(array_class->GetDescriptor()->ToModifiedUtf8(), 1);
47 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
48 Class* sub_array_class = class_linker->FindClass(sub_array_descriptor,
49 array_class->GetClassLoader());
50 if (sub_array_class == NULL) {
51 CHECK(Thread::Current()->IsExceptionPending());
52 return NULL;
53 }
54 DCHECK(sub_array_class->IsArrayClass());
55 // Create a new sub-array in every element of the array.
56 ObjectArray<Array>* object_array = new_array->AsObjectArray<Array>();
57 for (int32_t i = 0; i < array_length; i++) {
58 Array* sub_array = CreateMultiArray(sub_array_class, current_dimension, dimensions);
59 if (sub_array == NULL) {
60 CHECK(Thread::Current()->IsExceptionPending());
61 return NULL;
62 }
63 object_array->Set(i, sub_array);
64 }
65 return new_array;
66}
67
68// Create a multi-dimensional array of Objects or primitive types.
69//
70// We have to generate the names for X[], X[][], X[][][], and so on. The
71// easiest way to deal with that is to create the full name once and then
72// subtract pieces off. Besides, we want to start with the outermost
73// piece and work our way in.
74jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
75 DCHECK(javaElementClass != NULL);
76 Class* element_class = Decode<Class*>(env, javaElementClass);
77 DCHECK(element_class->IsClass());
78 DCHECK(javaDimArray != NULL);
79 Object* dimensions_obj = Decode<Class*>(env, javaDimArray);
80 DCHECK(dimensions_obj->IsArrayInstance());
81 DCHECK(dimensions_obj->GetClass()->GetDescriptor()->Equals("[I"));
82 IntArray* dimensions_array = down_cast<IntArray*>(dimensions_obj);
83
84 // Verify dimensions.
85 //
86 // The caller is responsible for verifying that "dimArray" is non-null
87 // and has a length > 0 and <= 255.
88 int num_dimensions = dimensions_array->GetLength();
89 DCHECK_GT(num_dimensions, 0);
90 DCHECK_LE(num_dimensions, 255);
91
92 for (int i = 0; i < num_dimensions; i++) {
Elliott Hughes6271c422011-10-11 15:43:35 -070093 int dimension = dimensions_array->Get(i);
94 if (dimension < 0) {
95 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
96 "Dimension %d: %d", i, dimension);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070097 return NULL;
98 }
99 }
100
101 // Generate the full name of the array class.
102 std::string descriptor(num_dimensions, '[');
103 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
104
105 // Find/generate the array class.
106 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
107 Class* array_class = class_linker->FindClass(descriptor, element_class->GetClassLoader());
108 if (array_class == NULL) {
109 CHECK(Thread::Current()->IsExceptionPending());
110 return NULL;
111 }
112 // create the array
113 Array* new_array = CreateMultiArray(array_class, 0, dimensions_array);
114 if (new_array == NULL) {
115 CHECK(Thread::Current()->IsExceptionPending());
116 return NULL;
117 }
118 return AddLocalReference<jobject>(env, new_array);
119}
120
121jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length)
122{
123 DCHECK(javaElementClass != NULL);
124 Class* element_class = Decode<Class*>(env, javaElementClass);
125 if (length < 0) {
Elliott Hughes6271c422011-10-11 15:43:35 -0700126 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700127 return NULL;
128 }
129 std::string descriptor;
130 descriptor += '[';
131 descriptor += element_class->GetDescriptor()->ToModifiedUtf8();
132
133 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
134 Class* array_class = class_linker->FindClass(descriptor, element_class->GetClassLoader());
135 if (array_class == NULL) {
136 CHECK(Thread::Current()->IsExceptionPending());
137 return NULL;
138 }
139 DCHECK(array_class->IsArrayClass());
140 Array* new_array = Array::Alloc(array_class, length);
141 if (new_array == NULL) {
142 CHECK(Thread::Current()->IsExceptionPending());
143 return NULL;
144 }
145 return AddLocalReference<jobject>(env, new_array);
146}
147
148static JNINativeMethod gMethods[] = {
149 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
150 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
151};
152
153} // namespace
154
155void register_java_lang_reflect_Array(JNIEnv* env) {
156 jniRegisterNativeMethods(env, "java/lang/reflect/Array", gMethods, NELEM(gMethods));
157}
158
159} // namespace art