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