java_lang_Class.cc revision 7940e44f4517de5e2634a7e07d58d0fb26160513
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 31namespace art { 32 33static 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". 45static 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 81static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) { 82 ScopedObjectAccess soa(env); 83 mirror::Class* c = DecodeClass(soa, javaClass); 84 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) { 85 return 0; // primitive, array and proxy classes don't have class definitions 86 } 87 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef(); 88 if (class_def == NULL) { 89 return 0; // not found 90 } else { 91 return class_def->annotations_off_; 92 } 93} 94 95static jobject Class_getDex(JNIEnv* env, jobject javaClass) { 96 ScopedObjectAccess soa(env); 97 mirror::Class* c = DecodeClass(soa, javaClass); 98 99 mirror::DexCache* dex_cache = c->GetDexCache(); 100 if (dex_cache == NULL) { 101 return NULL; 102 } 103 const DexFile* dex_file = dex_cache->GetDexFile(); 104 if (dex_file == NULL) { 105 return NULL; 106 } 107 return dex_file->GetDexObject(env); 108} 109 110static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { 111 ScopedObjectAccess soa(env); 112 mirror::Class* c = DecodeClass(soa, javaThis); 113 return soa.AddLocalReference<jstring>(c->ComputeName()); 114} 115 116static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) { 117 ScopedObjectAccess soa(env); 118 mirror::SynthesizedProxyClass* c = 119 down_cast<mirror::SynthesizedProxyClass*>(DecodeClass(soa, javaThis)); 120 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self())); 121} 122 123static JNINativeMethod gMethods[] = { 124 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), 125 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"), 126 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"), 127 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), 128 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"), 129}; 130 131void register_java_lang_Class(JNIEnv* env) { 132 REGISTER_NATIVE_METHODS("java/lang/Class"); 133} 134 135} // namespace art 136