java_lang_Class.cc revision 98d1cc8033251c93786e2fa8c59a2e555a9493be
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 "object_utils.h" 25#include "scoped_thread_state_change.h" 26#include "scoped_fast_native_object_access.h" 27#include "ScopedLocalRef.h" 28#include "ScopedUtfChars.h" 29#include "well_known_classes.h" 30 31namespace art { 32 33static mirror::Class* DecodeClass(const ScopedFastNativeObjectAccess& 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". 45static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, 46 jobject javaLoader) { 47 ScopedFastNativeObjectAccess soa(env); 48 ScopedUtfChars name(env, javaName); 49 if (name.c_str() == nullptr) { 50 return nullptr; 51 } 52 53 // We need to validate and convert the name (from x.y.z to x/y/z). This 54 // is especially handy for array types, since we want to avoid 55 // auto-generating bogus array classes. 56 if (!IsValidBinaryClassName(name.c_str())) { 57 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); 58 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ClassNotFoundException;", 59 "Invalid name: %s", name.c_str()); 60 return nullptr; 61 } 62 63 std::string descriptor(DotToDescriptor(name.c_str())); 64 StackHandleScope<2> hs(soa.Self()); 65 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); 66 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 67 Handle<mirror::Class> c( 68 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader))); 69 if (c.Get() == nullptr) { 70 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); 71 env->ExceptionClear(); 72 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException, 73 WellKnownClasses::java_lang_ClassNotFoundException_init, 74 javaName, cause.get())); 75 env->Throw(cnfe); 76 return nullptr; 77 } 78 if (initialize) { 79 class_linker->EnsureInitialized(c, true, true); 80 } 81 return soa.AddLocalReference<jclass>(c.Get()); 82} 83 84static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { 85 ScopedFastNativeObjectAccess soa(env); 86 StackHandleScope<1> hs(soa.Self()); 87 mirror::Class* const c = DecodeClass(soa, javaThis); 88 return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c))); 89} 90 91static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) { 92 ScopedFastNativeObjectAccess soa(env); 93 mirror::Class* c = DecodeClass(soa, javaThis); 94 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self())); 95} 96 97static JNINativeMethod gMethods[] = { 98 NATIVE_METHOD(Class, classForName, "!(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), 99 NATIVE_METHOD(Class, getNameNative, "!()Ljava/lang/String;"), 100 NATIVE_METHOD(Class, getProxyInterfaces, "!()[Ljava/lang/Class;"), 101}; 102 103void register_java_lang_Class(JNIEnv* env) { 104 REGISTER_NATIVE_METHODS("java/lang/Class"); 105} 106 107} // namespace art 108