Home | History | Annotate | Download | only in native
      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_internal.h"
     26 #include "mirror/class-inl.h"
     27 #include "mirror/object-inl.h"
     28 #include "native_util.h"
     29 #include "scoped_fast_native_object_access-inl.h"
     30 
     31 namespace art {
     32 
     33 static jobject Array_createMultiArray(
     34     JNIEnv* env, jclass, jclass javaElementClass, jintArray javaDimArray) {
     35   ScopedFastNativeObjectAccess soa(env);
     36   DCHECK(javaElementClass != nullptr);
     37   StackHandleScope<2> hs(soa.Self());
     38   Handle<mirror::Class> element_class(hs.NewHandle(soa.Decode<mirror::Class>(javaElementClass)));
     39   DCHECK(element_class->IsClass());
     40   DCHECK(javaDimArray != nullptr);
     41   ObjPtr<mirror::Object> dimensions_obj = soa.Decode<mirror::Object>(javaDimArray);
     42   DCHECK(dimensions_obj->IsArrayInstance());
     43   DCHECK_EQ(dimensions_obj->GetClass()->GetComponentType()->GetPrimitiveType(),
     44             Primitive::kPrimInt);
     45   Handle<mirror::IntArray> dimensions_array(
     46       hs.NewHandle(ObjPtr<mirror::IntArray>::DownCast(dimensions_obj)));
     47   mirror::Array* new_array = mirror::Array::CreateMultiArray(soa.Self(),
     48                                                              element_class,
     49                                                              dimensions_array);
     50   return soa.AddLocalReference<jobject>(new_array);
     51 }
     52 
     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   ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
     61   Runtime* runtime = Runtime::Current();
     62   ClassLinker* class_linker = runtime->GetClassLinker();
     63   ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(soa.Self(), &element_class);
     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 
     82 void register_java_lang_reflect_Array(JNIEnv* env) {
     83   REGISTER_NATIVE_METHODS("java/lang/reflect/Array");
     84 }
     85 
     86 }  // namespace art
     87