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_Constructor.h"
     18 
     19 #include "art_method-inl.h"
     20 #include "class_linker.h"
     21 #include "jni_internal.h"
     22 #include "mirror/class-inl.h"
     23 #include "mirror/method.h"
     24 #include "mirror/object-inl.h"
     25 #include "reflection.h"
     26 #include "scoped_fast_native_object_access.h"
     27 #include "well_known_classes.h"
     28 
     29 namespace art {
     30 
     31 /*
     32  * We can also safely assume the constructor isn't associated
     33  * with an interface, array, or primitive class. If this is coming from
     34  * native, it is OK to avoid access checks since JNI does not enforce them.
     35  */
     36 static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
     37   ScopedFastNativeObjectAccess soa(env);
     38   mirror::Constructor* m = soa.Decode<mirror::Constructor*>(javaMethod);
     39   StackHandleScope<1> hs(soa.Self());
     40   Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
     41   if (UNLIKELY(c->IsAbstract())) {
     42     soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
     43                                    c->IsInterface() ? "interface" : "abstract class",
     44                                    PrettyDescriptor(c.Get()).c_str());
     45     return nullptr;
     46   }
     47   // Verify that we can access the class.
     48   if (!m->IsAccessible() && !c->IsPublic()) {
     49     auto* caller = GetCallingClass(soa.Self(), 1);
     50     // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
     51     // access checks anyways. TODO: Investigate if this the correct behavior.
     52     if (caller != nullptr && !caller->CanAccess(c.Get())) {
     53       if (PrettyDescriptor(c.Get()) == "dalvik.system.DexPathList$Element") {
     54         // b/20699073.
     55         LOG(WARNING) << "The dalvik.system.DexPathList$Element constructor is not accessible by "
     56                         "default. This is a temporary workaround for backwards compatibility "
     57                         "with class-loader hacks. Please update your application.";
     58       } else {
     59         soa.Self()->ThrowNewExceptionF(
     60             "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
     61             PrettyClass(c.Get()).c_str(), PrettyClass(caller).c_str());
     62         return nullptr;
     63       }
     64     }
     65   }
     66   if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
     67     DCHECK(soa.Self()->IsExceptionPending());
     68     return nullptr;
     69   }
     70   bool movable = true;
     71   if (!kMovingClasses && c->IsClassClass()) {
     72     movable = false;
     73   }
     74 
     75   // String constructor is replaced by a StringFactory method in InvokeMethod.
     76   if (c->IsStringClass()) {
     77     return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 1);
     78   }
     79 
     80   mirror::Object* receiver =
     81       movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
     82   if (receiver == nullptr) {
     83     return nullptr;
     84   }
     85   jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
     86   InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 1);
     87   // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
     88   return javaReceiver;
     89 }
     90 
     91 static JNINativeMethod gMethods[] = {
     92   NATIVE_METHOD(Constructor, newInstance, "!([Ljava/lang/Object;)Ljava/lang/Object;"),
     93 };
     94 
     95 void register_java_lang_reflect_Constructor(JNIEnv* env) {
     96   REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
     97 }
     98 
     99 }  // namespace art
    100