Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 18 | #include "jni_internal.h" |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 19 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 20 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 21 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 22 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 25 | // Recursively create an array with multiple dimensions. Elements may be |
| 26 | // Objects or primitive types. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 27 | static Array* CreateMultiArray(Class* array_class, int current_dimension, IntArray* dimensions) |
| 28 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 29 | int32_t array_length = dimensions->Get(current_dimension++); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 30 | SirtRef<Array> new_array(Array::Alloc(array_class, array_length)); |
| 31 | if (new_array.get() == NULL) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 32 | CHECK(Thread::Current()->IsExceptionPending()); |
| 33 | return NULL; |
| 34 | } |
| 35 | if (current_dimension == dimensions->GetLength()) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 36 | return new_array.get(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | if (!array_class->GetComponentType()->IsArrayClass()) { |
| 40 | // TODO: throw an exception, not relying on class_linker->FindClass to throw. |
| 41 | // old code assumed this but if you recurse from "[Foo" to "Foo" to "oo", |
| 42 | // you shouldn't assume there isn't a class "oo". |
| 43 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 44 | std::string sub_array_descriptor(ClassHelper(array_class).GetDescriptor() + 1); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 45 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 46 | Class* sub_array_class = class_linker->FindClass(sub_array_descriptor.c_str(), |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 47 | array_class->GetClassLoader()); |
| 48 | if (sub_array_class == NULL) { |
| 49 | CHECK(Thread::Current()->IsExceptionPending()); |
| 50 | return NULL; |
| 51 | } |
| 52 | DCHECK(sub_array_class->IsArrayClass()); |
| 53 | // Create a new sub-array in every element of the array. |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 54 | SirtRef<ObjectArray<Array> > object_array(new_array->AsObjectArray<Array>()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 55 | for (int32_t i = 0; i < array_length; i++) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 56 | SirtRef<Array> sub_array(CreateMultiArray(sub_array_class, current_dimension, dimensions)); |
| 57 | if (sub_array.get() == NULL) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 58 | CHECK(Thread::Current()->IsExceptionPending()); |
| 59 | return NULL; |
| 60 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 61 | object_array->Set(i, sub_array.get()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 62 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 63 | return new_array.get(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Create a multi-dimensional array of Objects or primitive types. |
| 67 | // |
| 68 | // We have to generate the names for X[], X[][], X[][][], and so on. The |
| 69 | // easiest way to deal with that is to create the full name once and then |
| 70 | // subtract pieces off. Besides, we want to start with the outermost |
| 71 | // piece and work our way in. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 72 | static jobject Array_createMultiArray(JNIEnv* env, jclass, jclass javaElementClass, jobject javaDimArray) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 73 | ScopedObjectAccess soa(env); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 74 | DCHECK(javaElementClass != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 75 | Class* element_class = soa.Decode<Class*>(javaElementClass); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 76 | DCHECK(element_class->IsClass()); |
| 77 | DCHECK(javaDimArray != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 78 | Object* dimensions_obj = soa.Decode<Object*>(javaDimArray); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 79 | DCHECK(dimensions_obj->IsArrayInstance()); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 80 | DCHECK_STREQ(ClassHelper(dimensions_obj->GetClass()).GetDescriptor(), "[I"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 81 | IntArray* dimensions_array = down_cast<IntArray*>(dimensions_obj); |
| 82 | |
| 83 | // Verify dimensions. |
| 84 | // |
| 85 | // The caller is responsible for verifying that "dimArray" is non-null |
| 86 | // and has a length > 0 and <= 255. |
| 87 | int num_dimensions = dimensions_array->GetLength(); |
| 88 | DCHECK_GT(num_dimensions, 0); |
| 89 | DCHECK_LE(num_dimensions, 255); |
| 90 | |
| 91 | for (int i = 0; i < num_dimensions; i++) { |
Elliott Hughes | 6271c42 | 2011-10-11 15:43:35 -0700 | [diff] [blame] | 92 | int dimension = dimensions_array->Get(i); |
| 93 | if (dimension < 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 94 | soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", |
Elliott Hughes | 6271c42 | 2011-10-11 15:43:35 -0700 | [diff] [blame] | 95 | "Dimension %d: %d", i, dimension); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Generate the full name of the array class. |
| 101 | std::string descriptor(num_dimensions, '['); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 102 | descriptor += ClassHelper(element_class).GetDescriptor(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 103 | |
| 104 | // Find/generate the array class. |
| 105 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 106 | Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 107 | if (array_class == NULL) { |
| 108 | CHECK(Thread::Current()->IsExceptionPending()); |
| 109 | return NULL; |
| 110 | } |
| 111 | // create the array |
| 112 | Array* new_array = CreateMultiArray(array_class, 0, dimensions_array); |
| 113 | if (new_array == NULL) { |
| 114 | CHECK(Thread::Current()->IsExceptionPending()); |
| 115 | return NULL; |
| 116 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 117 | return soa.AddLocalReference<jobject>(new_array); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 120 | static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 121 | ScopedObjectAccess soa(env); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 122 | DCHECK(javaElementClass != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 123 | Class* element_class = soa.Decode<Class*>(javaElementClass); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 124 | if (length < 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 125 | soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
| 128 | std::string descriptor; |
| 129 | descriptor += '['; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 130 | descriptor += ClassHelper(element_class).GetDescriptor(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 131 | |
| 132 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 133 | Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 134 | if (array_class == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 135 | CHECK(soa.Self()->IsExceptionPending()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
| 138 | DCHECK(array_class->IsArrayClass()); |
| 139 | Array* new_array = Array::Alloc(array_class, length); |
| 140 | if (new_array == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 141 | CHECK(soa.Self()->IsExceptionPending()); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 144 | return soa.AddLocalReference<jobject>(new_array); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static JNINativeMethod gMethods[] = { |
| 148 | NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"), |
| 149 | NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"), |
| 150 | }; |
| 151 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 152 | void register_java_lang_reflect_Array(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 153 | REGISTER_NATIVE_METHODS("java/lang/reflect/Array"); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace art |