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 "mirror/proxy.h"
     25 #include "object_utils.h"
     26 #include "scoped_thread_state_change.h"
     27 #include "ScopedLocalRef.h"
     28 #include "ScopedUtfChars.h"
     29 #include "well_known_classes.h"
     30 
     31 namespace art {
     32 
     33 static mirror::Class* DecodeClass(const ScopedObjectAccess& soa, jobject java_class)
     34     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     35   mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
     36   DCHECK(c != NULL);
     37   DCHECK(c->IsClass());
     38   // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
     39   // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
     40   // every time probably doesn't make much difference to reflection performance anyway.
     41   return c;
     42 }
     43 
     44 // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
     45 static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
     46   ScopedObjectAccess soa(env);
     47   ScopedUtfChars name(env, javaName);
     48   if (name.c_str() == NULL) {
     49     return NULL;
     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 NULL;
     60   }
     61 
     62   std::string descriptor(DotToDescriptor(name.c_str()));
     63   mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
     64   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
     65   mirror::Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
     66   if (c == NULL) {
     67     ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
     68     env->ExceptionClear();
     69     jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
     70                                                                   WellKnownClasses::java_lang_ClassNotFoundException_init,
     71                                                                   javaName, cause.get()));
     72     env->Throw(cnfe);
     73     return NULL;
     74   }
     75   if (initialize) {
     76     class_linker->EnsureInitialized(c, true, true);
     77   }
     78   return soa.AddLocalReference<jclass>(c);
     79 }
     80 
     81 static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
     82   ScopedObjectAccess soa(env);
     83   mirror::Class* c = DecodeClass(soa, javaThis);
     84   return soa.AddLocalReference<jstring>(c->ComputeName());
     85 }
     86 
     87 static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
     88   ScopedObjectAccess soa(env);
     89   mirror::SynthesizedProxyClass* c =
     90       down_cast<mirror::SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
     91   return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
     92 }
     93 
     94 static JNINativeMethod gMethods[] = {
     95   NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
     96   NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
     97   NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
     98 };
     99 
    100 void register_java_lang_Class(JNIEnv* env) {
    101   REGISTER_NATIVE_METHODS("java/lang/Class");
    102 }
    103 
    104 }  // namespace art
    105