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
17 #include "java_lang_reflect_Array.h"
18
19 #include "nativehelper/jni_macros.h"
20
21 #include "class_linker-inl.h"
22 #include "common_throws.h"
23 #include "dex/dex_file-inl.h"
24 #include "handle_scope-inl.h"
25 #include "jni/jni_internal.h"
26 #include "mirror/class-inl.h"
27 #include "mirror/object_array-alloc-inl.h"
28 #include "mirror/object-inl.h"
29 #include "native_util.h"
30 #include "scoped_fast_native_object_access-inl.h"
31
32 namespace art {
33
Array_createMultiArray(JNIEnv * env,jclass,jclass javaElementClass,jintArray javaDimArray)34 static jobject Array_createMultiArray(
35 JNIEnv* env, jclass, jclass javaElementClass, jintArray javaDimArray) {
36 ScopedFastNativeObjectAccess soa(env);
37 DCHECK(javaElementClass != nullptr);
38 StackHandleScope<2> hs(soa.Self());
39 Handle<mirror::Class> element_class(hs.NewHandle(soa.Decode<mirror::Class>(javaElementClass)));
40 DCHECK(element_class->IsClass());
41 DCHECK(javaDimArray != nullptr);
42 ObjPtr<mirror::Object> dimensions_obj = soa.Decode<mirror::Object>(javaDimArray);
43 DCHECK(dimensions_obj->IsArrayInstance());
44 DCHECK_EQ(dimensions_obj->GetClass()->GetComponentType()->GetPrimitiveType(),
45 Primitive::kPrimInt);
46 Handle<mirror::IntArray> dimensions_array(
47 hs.NewHandle(ObjPtr<mirror::IntArray>::DownCast(dimensions_obj)));
48 ObjPtr<mirror::Array> new_array =
49 mirror::Array::CreateMultiArray(soa.Self(), element_class, dimensions_array);
50 return soa.AddLocalReference<jobject>(new_array);
51 }
52
Array_createObjectArray(JNIEnv * env,jclass,jclass javaElementClass,jint length)53 static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
54 ScopedFastNativeObjectAccess soa(env);
55 DCHECK(javaElementClass != nullptr);
56 if (UNLIKELY(length < 0)) {
57 ThrowNegativeArraySizeException(length);
58 return nullptr;
59 }
60 Runtime* runtime = Runtime::Current();
61 ClassLinker* class_linker = runtime->GetClassLinker();
62 ObjPtr<mirror::Class> array_class =
63 class_linker->FindArrayClass(soa.Self(), soa.Decode<mirror::Class>(javaElementClass));
64 if (UNLIKELY(array_class == nullptr)) {
65 CHECK(soa.Self()->IsExceptionPending());
66 return nullptr;
67 }
68 DCHECK(array_class->IsObjectArrayClass());
69 ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object>::Alloc(
70 soa.Self(),
71 array_class,
72 length,
73 runtime->GetHeap()->GetCurrentAllocator());
74 return soa.AddLocalReference<jobject>(new_array);
75 }
76
77 static JNINativeMethod gMethods[] = {
78 FAST_NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
79 FAST_NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
80 };
81
register_java_lang_reflect_Array(JNIEnv * env)82 void register_java_lang_reflect_Array(JNIEnv* env) {
83 REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
84 }
85
86 } // namespace art
87