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.h"
     18 #include "dex_file-inl.h"
     19 #include "jni_internal.h"
     20 #include "nth_caller_visitor.h"
     21 #include "mirror/class-inl.h"
     22 #include "mirror/class_loader.h"
     23 #include "mirror/object-inl.h"
     24 #include "scoped_thread_state_change.h"
     25 #include "scoped_fast_native_object_access.h"
     26 #include "ScopedLocalRef.h"
     27 #include "ScopedUtfChars.h"
     28 #include "well_known_classes.h"
     29 
     30 namespace art {
     31 
     32 static mirror::Class* DecodeClass(const ScopedFastNativeObjectAccess& soa, jobject java_class)
     33     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     34   mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
     35   DCHECK(c != NULL);
     36   DCHECK(c->IsClass());
     37   // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
     38   // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
     39   // every time probably doesn't make much difference to reflection performance anyway.
     40   return c;
     41 }
     42 
     43 // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
     44 static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize,
     45                                  jobject javaLoader) {
     46   ScopedFastNativeObjectAccess soa(env);
     47   ScopedUtfChars name(env, javaName);
     48   if (name.c_str() == nullptr) {
     49     return nullptr;
     50   }
     51 
     52   // We need to validate and convert the name (from x.y.z to x/y/z).  This
     53   // is especially handy for array types, since we want to avoid
     54   // auto-generating bogus array classes.
     55   if (!IsValidBinaryClassName(name.c_str())) {
     56     ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
     57     soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ClassNotFoundException;",
     58                                    "Invalid name: %s", name.c_str());
     59     return nullptr;
     60   }
     61 
     62   std::string descriptor(DotToDescriptor(name.c_str()));
     63   StackHandleScope<2> hs(soa.Self());
     64   Handle<mirror::ClassLoader> class_loader(hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
     65   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
     66   Handle<mirror::Class> c(
     67       hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader)));
     68   if (c.Get() == nullptr) {
     69     ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
     70     env->ExceptionClear();
     71     jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
     72                                                                   WellKnownClasses::java_lang_ClassNotFoundException_init,
     73                                                                   javaName, cause.get()));
     74     if (cnfe != nullptr) {
     75       // Make sure allocation didn't fail with an OOME.
     76       env->Throw(cnfe);
     77     }
     78     return nullptr;
     79   }
     80   if (initialize) {
     81     class_linker->EnsureInitialized(c, true, true);
     82   }
     83   return soa.AddLocalReference<jclass>(c.Get());
     84 }
     85 
     86 static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
     87   ScopedFastNativeObjectAccess soa(env);
     88   StackHandleScope<1> hs(soa.Self());
     89   mirror::Class* const c = DecodeClass(soa, javaThis);
     90   return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c)));
     91 }
     92 
     93 static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
     94   ScopedFastNativeObjectAccess soa(env);
     95   mirror::Class* c = DecodeClass(soa, javaThis);
     96   return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
     97 }
     98 
     99 static JNINativeMethod gMethods[] = {
    100   NATIVE_METHOD(Class, classForName, "!(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
    101   NATIVE_METHOD(Class, getNameNative, "!()Ljava/lang/String;"),
    102   NATIVE_METHOD(Class, getProxyInterfaces, "!()[Ljava/lang/Class;"),
    103 };
    104 
    105 void register_java_lang_Class(JNIEnv* env) {
    106   REGISTER_NATIVE_METHODS("java/lang/Class");
    107 }
    108 
    109 }  // namespace art
    110