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