Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 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 "jni_internal.h" |
| 18 | #include "class_linker.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 19 | #include "class_loader.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 20 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "object_utils.h" |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 23 | #include "ScopedUtfChars.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 24 | |
| 25 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | namespace { |
| 30 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 31 | // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1". |
| 32 | jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 33 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 34 | ScopedUtfChars name(env, javaName); |
| 35 | if (name.c_str() == NULL) { |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | // We need to validate and convert the name (from x.y.z to x/y/z). This |
| 40 | // is especially handy for array types, since we want to avoid |
| 41 | // auto-generating bogus array classes. |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 42 | if (!IsValidBinaryClassName(name.c_str())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 43 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 44 | "Invalid name: %s", name.c_str()); |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | std::string descriptor(DotToDescriptor(name.c_str())); |
| 49 | Object* loader = Decode<Object*>(env, javaLoader); |
| 50 | ClassLoader* class_loader = down_cast<ClassLoader*>(loader); |
| 51 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 52 | Class* c = class_linker->FindClass(descriptor.c_str(), class_loader); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 53 | if (c == NULL) { |
Elliott Hughes | a7679b6 | 2012-01-24 17:15:23 -0800 | [diff] [blame] | 54 | // Convert NoClassDefFoundError to ClassNotFoundException. |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame^] | 55 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 56 | env->ExceptionClear(); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame^] | 57 | static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException"); |
| 58 | static jmethodID ctor = env->GetMethodID(ClassNotFoundException_class, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); |
| 59 | jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(ClassNotFoundException_class, ctor, javaName, cause.get())); |
| 60 | env->Throw(cnfe); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 63 | if (initialize) { |
| 64 | class_linker->EnsureInitialized(c, true); |
| 65 | } |
| 66 | return AddLocalReference<jclass>(env, c); |
| 67 | } |
| 68 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 69 | jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) { |
| 70 | Class* c = Decode<Class*>(env, javaClass); |
| 71 | if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) { |
| 72 | return 0; // primitive, array and proxy classes don't have class definitions |
| 73 | } |
| 74 | const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef(); |
| 75 | if (class_def == NULL) { |
| 76 | return 0; // not found |
| 77 | } else { |
| 78 | return class_def->annotations_off_; |
| 79 | } |
| 80 | } |
| 81 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 82 | template<typename T> |
| 83 | jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) { |
| 84 | jclass array_class = env->FindClass(array_class_name); |
| 85 | jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL); |
| 86 | for (size_t i = 0; i < objects.size(); ++i) { |
| 87 | ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i])); |
| 88 | env->SetObjectArrayElement(result, i, object.get()); |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | bool IsVisibleConstructor(Method* m, bool public_only) { |
| 94 | if (public_only && !m->IsPublic()) { |
| 95 | return false; |
| 96 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 97 | if (m->IsStatic()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 100 | return m->IsConstructor(); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 103 | jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 104 | Class* c = Decode<Class*>(env, javaClass); |
| 105 | |
| 106 | std::vector<Method*> constructors; |
| 107 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 108 | Method* m = c->GetDirectMethod(i); |
| 109 | if (IsVisibleConstructor(m, publicOnly)) { |
| 110 | constructors.push_back(m); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return ToArray(env, "java/lang/reflect/Constructor", constructors); |
| 115 | } |
| 116 | |
| 117 | bool IsVisibleField(Field* f, bool public_only) { |
Elliott Hughes | c0dd312 | 2011-09-26 10:15:43 -0700 | [diff] [blame] | 118 | if (public_only && !f->IsPublic()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 124 | jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 125 | Class* c = Decode<Class*>(env, javaClass); |
| 126 | |
| 127 | std::vector<Field*> fields; |
| 128 | for (size_t i = 0; i < c->NumInstanceFields(); ++i) { |
| 129 | Field* f = c->GetInstanceField(i); |
| 130 | if (IsVisibleField(f, publicOnly)) { |
| 131 | fields.push_back(f); |
| 132 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 133 | if (env->ExceptionOccurred()) { |
| 134 | return NULL; |
| 135 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 136 | } |
| 137 | for (size_t i = 0; i < c->NumStaticFields(); ++i) { |
| 138 | Field* f = c->GetStaticField(i); |
| 139 | if (IsVisibleField(f, publicOnly)) { |
| 140 | fields.push_back(f); |
| 141 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 142 | if (env->ExceptionOccurred()) { |
| 143 | return NULL; |
| 144 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return ToArray(env, "java/lang/reflect/Field", fields); |
| 148 | } |
| 149 | |
| 150 | bool IsVisibleMethod(Method* m, bool public_only) { |
| 151 | if (public_only && !m->IsPublic()) { |
| 152 | return false; |
| 153 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 154 | if (m->IsConstructor()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 155 | return false; |
| 156 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 160 | jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 161 | Class* c = Decode<Class*>(env, javaClass); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 162 | std::vector<Method*> methods; |
| 163 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 164 | Method* m = c->GetVirtualMethod(i); |
| 165 | if (IsVisibleMethod(m, publicOnly)) { |
| 166 | methods.push_back(m); |
| 167 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 168 | if (env->ExceptionOccurred()) { |
| 169 | return NULL; |
| 170 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 171 | } |
| 172 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 173 | Method* m = c->GetDirectMethod(i); |
| 174 | if (IsVisibleMethod(m, publicOnly)) { |
| 175 | methods.push_back(m); |
| 176 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 177 | if (env->ExceptionOccurred()) { |
| 178 | return NULL; |
| 179 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return ToArray(env, "java/lang/reflect/Method", methods); |
| 183 | } |
| 184 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 185 | jboolean Class_desiredAssertionStatus(JNIEnv* env, jobject javaThis) { |
| 186 | return JNI_FALSE; |
| 187 | } |
| 188 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 189 | jobject Class_getDex(JNIEnv* env, jobject javaClass) { |
| 190 | Class* c = Decode<Class*>(env, javaClass); |
| 191 | |
| 192 | DexCache* dex_cache = c->GetDexCache(); |
| 193 | if (dex_cache == NULL) { |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env); |
| 198 | } |
| 199 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 200 | jint Class_getNonInnerClassModifiers(JNIEnv* env, jclass javaClass) { |
Elliott Hughes | 9dcc79d | 2011-10-02 16:31:10 -0700 | [diff] [blame] | 201 | Class* c = Decode<Class*>(env, javaClass); |
Jesse Wilson | 163c464 | 2011-10-12 10:42:15 -0400 | [diff] [blame] | 202 | return c->GetAccessFlags() & kAccJavaFlagsMask; |
Elliott Hughes | 9dcc79d | 2011-10-02 16:31:10 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 205 | jobject Class_getClassLoaderNative(JNIEnv* env, jclass javaClass) { |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 206 | Class* c = Decode<Class*>(env, javaClass); |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 207 | Object* result = c->GetClassLoader(); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 208 | return AddLocalReference<jobject>(env, result); |
| 209 | } |
| 210 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 211 | jclass Class_getComponentType(JNIEnv* env, jclass javaClass) { |
| 212 | return AddLocalReference<jclass>(env, Decode<Class*>(env, javaClass)->GetComponentType()); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 215 | bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) { |
| 216 | if (name != mh->GetName()) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 217 | return false; |
| 218 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 219 | const DexFile::TypeList* m_type_list = mh->GetParameterTypeList(); |
| 220 | uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size(); |
| 221 | uint32_t sig_length = arg_array->GetLength(); |
| 222 | |
| 223 | if (m_type_list_size != sig_length) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 224 | return false; |
| 225 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 226 | |
| 227 | for (uint32_t i = 0; i < sig_length; i++) { |
| 228 | if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) { |
| 229 | return false; |
| 230 | } |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 231 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 235 | Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name, |
| 236 | ObjectArray<Class>* arg_array) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 237 | if (methods == NULL) { |
| 238 | return NULL; |
| 239 | } |
| 240 | Method* result = NULL; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 241 | MethodHelper mh; |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 242 | for (int32_t i = 0; i < methods->GetLength(); ++i) { |
| 243 | Method* method = methods->Get(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 244 | mh.ChangeMethod(method); |
| 245 | if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 246 | continue; |
| 247 | } |
| 248 | |
| 249 | result = method; |
| 250 | |
| 251 | // Covariant return types permit the class to define multiple |
| 252 | // methods with the same name and parameter types. Prefer to return |
| 253 | // a non-synthetic method in such situations. We may still return |
| 254 | // a synthetic method to handle situations like escalated visibility. |
| 255 | if (!method->IsSynthetic()) { |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | return result; |
| 260 | } |
| 261 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 262 | jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName, |
| 263 | jobjectArray javaArgs) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 264 | Class* c = Decode<Class*>(env, javaClass); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 265 | std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 266 | ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 267 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 268 | Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array); |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 269 | if (m == NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 270 | m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 273 | if (m != NULL) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 274 | return AddLocalReference<jobject>(env, m); |
| 275 | } else { |
| 276 | return NULL; |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 277 | } |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 280 | jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass jklass, jobject jname) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 281 | Class* klass = Decode<Class*>(env, jklass); |
| 282 | DCHECK(klass->IsClass()); |
| 283 | String* name = Decode<String*>(env, jname); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 284 | DCHECK(name->GetClass()->IsStringClass()); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 285 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 286 | FieldHelper fh; |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 287 | for (size_t i = 0; i < klass->NumInstanceFields(); ++i) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 288 | Field* f = klass->GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 289 | fh.ChangeField(f); |
| 290 | if (name->Equals(fh.GetName())) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 291 | return AddLocalReference<jclass>(env, f); |
| 292 | } |
| 293 | } |
| 294 | for (size_t i = 0; i < klass->NumStaticFields(); ++i) { |
| 295 | Field* f = klass->GetStaticField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 296 | fh.ChangeField(f); |
| 297 | if (name->Equals(fh.GetName())) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 298 | return AddLocalReference<jclass>(env, f); |
| 299 | } |
| 300 | } |
| 301 | return NULL; |
| 302 | } |
| 303 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 304 | /* |
| 305 | * private native String getNameNative() |
| 306 | * |
| 307 | * Return the class' name. The exact format is bizarre, but it's the specified |
| 308 | * behavior: keywords for primitive types, regular "[I" form for primitive |
| 309 | * arrays (so "int" but "[I"), and arrays of reference types written |
| 310 | * between "L" and ";" but with dots rather than slashes (so "java.lang.String" |
| 311 | * but "[Ljava.lang.String;"). Madness. |
| 312 | */ |
| 313 | jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
| 314 | Class* c = Decode<Class*>(env, javaThis); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 315 | std::string descriptor(ClassHelper(c).GetDescriptor()); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 316 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 317 | // The descriptor indicates that this is the class for |
| 318 | // a primitive type; special-case the return value. |
| 319 | const char* name = NULL; |
| 320 | switch (descriptor[0]) { |
| 321 | case 'Z': name = "boolean"; break; |
| 322 | case 'B': name = "byte"; break; |
| 323 | case 'C': name = "char"; break; |
| 324 | case 'S': name = "short"; break; |
| 325 | case 'I': name = "int"; break; |
| 326 | case 'J': name = "long"; break; |
| 327 | case 'F': name = "float"; break; |
| 328 | case 'D': name = "double"; break; |
| 329 | case 'V': name = "void"; break; |
| 330 | default: |
| 331 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 332 | } |
| 333 | return env->NewStringUTF(name); |
| 334 | } |
| 335 | |
| 336 | // Convert the UTF-8 name to a java.lang.String. The |
| 337 | // name must use '.' to separate package components. |
| 338 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 339 | descriptor.erase(0, 1); |
| 340 | descriptor.erase(descriptor.size() - 1); |
| 341 | } |
| 342 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 343 | return env->NewStringUTF(descriptor.c_str()); |
| 344 | } |
| 345 | |
| 346 | jclass Class_getSuperclass(JNIEnv* env, jobject javaThis) { |
| 347 | Class* c = Decode<Class*>(env, javaThis); |
| 348 | Class* result = c->GetSuperClass(); |
| 349 | return AddLocalReference<jclass>(env, result); |
| 350 | } |
| 351 | |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 352 | jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 353 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 354 | Class* lhs = Decode<Class*>(env, javaLhs); |
| 355 | Class* rhs = Decode<Class*>(env, javaRhs); |
| 356 | if (rhs == NULL) { |
| 357 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null"); |
| 358 | return JNI_FALSE; |
| 359 | } |
| 360 | return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE; |
| 361 | } |
| 362 | |
| 363 | jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) { |
| 364 | Class* c = Decode<Class*>(env, javaClass); |
| 365 | Object* o = Decode<Object*>(env, javaObject); |
| 366 | if (o == NULL) { |
| 367 | return JNI_FALSE; |
| 368 | } |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 369 | return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 372 | jboolean Class_isInterface(JNIEnv* env, jobject javaThis) { |
| 373 | Class* c = Decode<Class*>(env, javaThis); |
| 374 | return c->IsInterface(); |
| 375 | } |
| 376 | |
| 377 | jboolean Class_isPrimitive(JNIEnv* env, jobject javaThis) { |
| 378 | Class* c = Decode<Class*>(env, javaThis); |
| 379 | return c->IsPrimitive(); |
| 380 | } |
| 381 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 382 | // Validate method/field access. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 383 | bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 384 | // quick accept for public access */ |
| 385 | if (member_flags & kAccPublic) { |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | // quick accept for access from same class |
| 390 | if (access_from == access_to) { |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | // quick reject for private access from another class |
| 395 | if (member_flags & kAccPrivate) { |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | // Semi-quick test for protected access from a sub-class, which may or |
| 400 | // may not be in the same package. |
| 401 | if (member_flags & kAccProtected) { |
| 402 | if (access_from->IsSubClass(access_to)) { |
| 403 | return true; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Allow protected and private access from other classes in the same |
| 408 | return access_from->IsInSamePackage(access_to); |
| 409 | } |
| 410 | |
| 411 | jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 412 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 413 | Class* c = Decode<Class*>(env, javaThis); |
| 414 | if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 415 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 416 | "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 417 | return NULL; |
| 418 | } |
| 419 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 420 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) { |
| 421 | return NULL; |
| 422 | } |
| 423 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 424 | Method* init = c->FindDirectMethod("<init>", "()V"); |
| 425 | if (init == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 426 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 427 | "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | // Verify access from the call site. |
| 432 | // |
| 433 | // First, make sure the method invoking Class.newInstance() has permission |
| 434 | // to access the class. |
| 435 | // |
| 436 | // Second, make sure it has permission to invoke the constructor. The |
| 437 | // constructor must be public or, if the caller is in the same package, |
| 438 | // have package scope. |
| 439 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
| 440 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 441 | frame.Next(); |
| 442 | frame.Next(); |
| 443 | Method* caller_caller = frame.GetMethod(); |
| 444 | Class* caller_class = caller_caller->GetDeclaringClass(); |
| 445 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 446 | ClassHelper caller_ch(caller_class); |
Brian Carlstrom | bc2f3e3 | 2011-09-22 17:16:54 -0700 | [diff] [blame] | 447 | if (!caller_class->CanAccess(c)) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 448 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;", |
| 449 | "Class %s is not accessible from class %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 450 | PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(), |
| 451 | PrettyDescriptor(caller_ch.GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 452 | return NULL; |
| 453 | } |
| 454 | if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 455 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;", |
| 456 | "%s is not accessible from class %s", |
| 457 | PrettyMethod(init).c_str(), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 458 | PrettyDescriptor(caller_ch.GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | Object* new_obj = c->AllocObject(); |
| 463 | if (new_obj == NULL) { |
| 464 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | // invoke constructor; unlike reflection calls, we don't wrap exceptions |
| 469 | jclass jklass = AddLocalReference<jclass>(env, c); |
| 470 | jmethodID mid = EncodeMethod(init); |
| 471 | return env->NewObject(jklass, mid); |
| 472 | } |
| 473 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 474 | static JNINativeMethod gMethods[] = { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 475 | NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 476 | NATIVE_METHOD(Class, desiredAssertionStatus, "()Z"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 477 | NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"), |
| 478 | NATIVE_METHOD(Class, getClassLoaderNative, "()Ljava/lang/ClassLoader;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 479 | NATIVE_METHOD(Class, getComponentType, "()Ljava/lang/Class;"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 480 | NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"), |
| 481 | NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"), |
| 482 | NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"), |
| 483 | NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"), |
| 484 | NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"), |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 485 | NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 486 | NATIVE_METHOD(Class, getNonInnerClassModifiers, "()I"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 487 | NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 488 | NATIVE_METHOD(Class, getSuperclass, "()Ljava/lang/Class;"), |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 489 | NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"), |
| 490 | NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 491 | NATIVE_METHOD(Class, isInterface, "()Z"), |
| 492 | NATIVE_METHOD(Class, isPrimitive, "()Z"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 493 | NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | } // namespace |
| 497 | |
| 498 | void register_java_lang_Class(JNIEnv* env) { |
| 499 | jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods)); |
| 500 | } |
| 501 | |
| 502 | } // namespace art |