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