• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-inl.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   ObjPtr<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(ObjPtr<mirror::IntArray>::DownCast(dimensions_obj)));
44   mirror::Array* new_array = mirror::Array::CreateMultiArray(soa.Self(),
45                                                              element_class,
46                                                              dimensions_array);
47   return soa.AddLocalReference<jobject>(new_array);
48 }
49 
Array_createObjectArray(JNIEnv * env,jclass,jclass javaElementClass,jint length)50 static jobject Array_createObjectArray(JNIEnv* env, jclass, jclass javaElementClass, jint length) {
51   ScopedFastNativeObjectAccess soa(env);
52   DCHECK(javaElementClass != nullptr);
53   if (UNLIKELY(length < 0)) {
54     ThrowNegativeArraySizeException(length);
55     return nullptr;
56   }
57   ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
58   Runtime* runtime = Runtime::Current();
59   ClassLinker* class_linker = runtime->GetClassLinker();
60   ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
61   if (UNLIKELY(array_class == nullptr)) {
62     CHECK(soa.Self()->IsExceptionPending());
63     return nullptr;
64   }
65   DCHECK(array_class->IsObjectArrayClass());
66   ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
67       soa.Self(),
68       array_class,
69       length,
70       runtime->GetHeap()->GetCurrentAllocator());
71   return soa.AddLocalReference<jobject>(new_array);
72 }
73 
74 static JNINativeMethod gMethods[] = {
75   FAST_NATIVE_METHOD(Array, createMultiArray, "(Ljava/lang/Class;[I)Ljava/lang/Object;"),
76   FAST_NATIVE_METHOD(Array, createObjectArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
77 };
78 
register_java_lang_reflect_Array(JNIEnv * env)79 void register_java_lang_reflect_Array(JNIEnv* env) {
80   REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
81 }
82 
83 }  // namespace art
84