blob: 3bf26808defdf9ee78b6019b12dee39e661bec8b [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
26namespace {
27
28
29// Recursively create an array with multiple dimensions. Elements may be
30// Objects or primitive types.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070031Array* CreateMultiArray(Class* array_class, int current_dimension, IntArray* dimensions) {
32 int32_t array_length = dimensions->Get(current_dimension++);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070033 SirtRef<Array> new_array(Array::Alloc(array_class, array_length));
34 if (new_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070035 CHECK(Thread::Current()->IsExceptionPending());
36 return NULL;
37 }
38 if (current_dimension == dimensions->GetLength()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070039 return new_array.get();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070040 }
41
42 if (!array_class->GetComponentType()->IsArrayClass()) {
43 // TODO: throw an exception, not relying on class_linker->FindClass to throw.
44 // old code assumed this but if you recurse from "[Foo" to "Foo" to "oo",
45 // you shouldn't assume there isn't a class "oo".
46 }
Elliott Hughes91250e02011-12-13 22:30:35 -080047 std::string sub_array_descriptor(ClassHelper(array_class).GetDescriptor() + 1);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070048 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080049 Class* sub_array_class = class_linker->FindClass(sub_array_descriptor.c_str(),
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070050 array_class->GetClassLoader());
51 if (sub_array_class == NULL) {
52 CHECK(Thread::Current()->IsExceptionPending());
53 return NULL;
54 }
55 DCHECK(sub_array_class->IsArrayClass());
56 // Create a new sub-array in every element of the array.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070057 SirtRef<ObjectArray<Array> > object_array(new_array->AsObjectArray<Array>());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070058 for (int32_t i = 0; i < array_length; i++) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070059 SirtRef<Array> sub_array(CreateMultiArray(sub_array_class, current_dimension, dimensions));
60 if (sub_array.get() == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070061 CHECK(Thread::Current()->IsExceptionPending());
62 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.
75jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070076 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -070077 DCHECK(javaElementClass != NULL);
78 Class* element_class = Decode<Class*>(env, javaElementClass);
79 DCHECK(element_class->IsClass());
80 DCHECK(javaDimArray != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080081 Object* dimensions_obj = Decode<Object*>(env, 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) {
97 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
98 "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) {
111 CHECK(Thread::Current()->IsExceptionPending());
112 return NULL;
113 }
114 // create the array
115 Array* new_array = CreateMultiArray(array_class, 0, dimensions_array);
116 if (new_array == NULL) {
117 CHECK(Thread::Current()->IsExceptionPending());
118 return NULL;
119 }
120 return AddLocalReference<jobject>(env, new_array);
121}
122
123jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length)
124{
Brian Carlstromb82b6872011-10-26 17:18:07 -0700125 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700126 DCHECK(javaElementClass != NULL);
127 Class* element_class = Decode<Class*>(env, javaElementClass);
128 if (length < 0) {
Elliott Hughes6271c422011-10-11 15:43:35 -0700129 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700130 return NULL;
131 }
132 std::string descriptor;
133 descriptor += '[';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 descriptor += ClassHelper(element_class).GetDescriptor();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700135
136 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800137 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700138 if (array_class == NULL) {
139 CHECK(Thread::Current()->IsExceptionPending());
140 return NULL;
141 }
142 DCHECK(array_class->IsArrayClass());
143 Array* new_array = Array::Alloc(array_class, length);
144 if (new_array == NULL) {
145 CHECK(Thread::Current()->IsExceptionPending());
146 return NULL;
147 }
148 return AddLocalReference<jobject>(env, new_array);
149}
150
151static JNINativeMethod gMethods[] = {
152 NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
153 NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
154};
155
156} // namespace
157
158void register_java_lang_reflect_Array(JNIEnv* env) {
159 jniRegisterNativeMethods(env, "java/lang/reflect/Array", gMethods, NELEM(gMethods));
160}
161
162} // namespace art