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 | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 54 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 55 | env->ExceptionClear(); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 56 | static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException"); |
| 57 | static jmethodID ctor = env->GetMethodID(ClassNotFoundException_class, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); |
| 58 | jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(ClassNotFoundException_class, ctor, javaName, cause.get())); |
| 59 | env->Throw(cnfe); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 60 | return NULL; |
| 61 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 62 | if (initialize) { |
| 63 | class_linker->EnsureInitialized(c, true); |
| 64 | } |
| 65 | return AddLocalReference<jclass>(env, c); |
| 66 | } |
| 67 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 68 | jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) { |
| 69 | Class* c = Decode<Class*>(env, javaClass); |
| 70 | if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) { |
| 71 | return 0; // primitive, array and proxy classes don't have class definitions |
| 72 | } |
| 73 | const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef(); |
| 74 | if (class_def == NULL) { |
| 75 | return 0; // not found |
| 76 | } else { |
| 77 | return class_def->annotations_off_; |
| 78 | } |
| 79 | } |
| 80 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 81 | template<typename T> |
| 82 | jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) { |
| 83 | jclass array_class = env->FindClass(array_class_name); |
| 84 | jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL); |
| 85 | for (size_t i = 0; i < objects.size(); ++i) { |
| 86 | ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i])); |
| 87 | env->SetObjectArrayElement(result, i, object.get()); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 92 | static bool IsVisibleConstructor(Method* m, bool public_only) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 93 | if (public_only && !m->IsPublic()) { |
| 94 | return false; |
| 95 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 96 | if (m->IsStatic()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 99 | return m->IsConstructor(); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 102 | jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 103 | Class* c = Decode<Class*>(env, javaClass); |
| 104 | |
| 105 | std::vector<Method*> constructors; |
| 106 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 107 | Method* m = c->GetDirectMethod(i); |
| 108 | if (IsVisibleConstructor(m, publicOnly)) { |
| 109 | constructors.push_back(m); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return ToArray(env, "java/lang/reflect/Constructor", constructors); |
| 114 | } |
| 115 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 116 | static bool IsVisibleField(Field* f, bool public_only) { |
Elliott Hughes | c0dd312 | 2011-09-26 10:15:43 -0700 | [diff] [blame] | 117 | if (public_only && !f->IsPublic()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 123 | jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 124 | Class* c = Decode<Class*>(env, javaClass); |
| 125 | |
| 126 | std::vector<Field*> fields; |
| 127 | for (size_t i = 0; i < c->NumInstanceFields(); ++i) { |
| 128 | Field* f = c->GetInstanceField(i); |
| 129 | if (IsVisibleField(f, publicOnly)) { |
| 130 | fields.push_back(f); |
| 131 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 132 | if (env->ExceptionOccurred()) { |
| 133 | return NULL; |
| 134 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 135 | } |
| 136 | for (size_t i = 0; i < c->NumStaticFields(); ++i) { |
| 137 | Field* f = c->GetStaticField(i); |
| 138 | if (IsVisibleField(f, publicOnly)) { |
| 139 | fields.push_back(f); |
| 140 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 141 | if (env->ExceptionOccurred()) { |
| 142 | return NULL; |
| 143 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return ToArray(env, "java/lang/reflect/Field", fields); |
| 147 | } |
| 148 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 149 | static bool IsVisibleMethod(Method* m, bool public_only) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 150 | if (public_only && !m->IsPublic()) { |
| 151 | return false; |
| 152 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 153 | if (m->IsConstructor()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 154 | return false; |
| 155 | } |
Ian Rogers | 5643742 | 2012-02-06 21:46:00 -0800 | [diff] [blame] | 156 | if (m->IsMiranda()) { |
| 157 | return false; |
| 158 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 162 | jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 163 | Class* c = Decode<Class*>(env, javaClass); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 164 | std::vector<Method*> methods; |
| 165 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 166 | Method* m = c->GetVirtualMethod(i); |
| 167 | if (IsVisibleMethod(m, publicOnly)) { |
| 168 | methods.push_back(m); |
| 169 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 170 | if (env->ExceptionOccurred()) { |
| 171 | return NULL; |
| 172 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 173 | } |
| 174 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 175 | Method* m = c->GetDirectMethod(i); |
| 176 | if (IsVisibleMethod(m, publicOnly)) { |
| 177 | methods.push_back(m); |
| 178 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 179 | if (env->ExceptionOccurred()) { |
| 180 | return NULL; |
| 181 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return ToArray(env, "java/lang/reflect/Method", methods); |
| 185 | } |
| 186 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 187 | jobject Class_getDex(JNIEnv* env, jobject javaClass) { |
| 188 | Class* c = Decode<Class*>(env, javaClass); |
| 189 | |
| 190 | DexCache* dex_cache = c->GetDexCache(); |
| 191 | if (dex_cache == NULL) { |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env); |
| 196 | } |
| 197 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 198 | static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 199 | if (name != mh->GetName()) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 202 | const DexFile::TypeList* m_type_list = mh->GetParameterTypeList(); |
| 203 | uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size(); |
| 204 | uint32_t sig_length = arg_array->GetLength(); |
| 205 | |
| 206 | if (m_type_list_size != sig_length) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 209 | |
| 210 | for (uint32_t i = 0; i < sig_length; i++) { |
| 211 | if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) { |
| 212 | return false; |
| 213 | } |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 214 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 218 | static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name, |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 219 | ObjectArray<Class>* arg_array) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 220 | if (methods == NULL) { |
| 221 | return NULL; |
| 222 | } |
| 223 | Method* result = NULL; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 224 | MethodHelper mh; |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 225 | for (int32_t i = 0; i < methods->GetLength(); ++i) { |
| 226 | Method* method = methods->Get(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 227 | mh.ChangeMethod(method); |
| 228 | if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 229 | continue; |
| 230 | } |
| 231 | |
| 232 | result = method; |
| 233 | |
| 234 | // Covariant return types permit the class to define multiple |
| 235 | // methods with the same name and parameter types. Prefer to return |
| 236 | // a non-synthetic method in such situations. We may still return |
| 237 | // a synthetic method to handle situations like escalated visibility. |
| 238 | if (!method->IsSynthetic()) { |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | return result; |
| 243 | } |
| 244 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 245 | jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName, |
| 246 | jobjectArray javaArgs) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 247 | Class* c = Decode<Class*>(env, javaClass); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 248 | std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 249 | ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 250 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 251 | Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array); |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 252 | if (m == NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 253 | m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 256 | if (m != NULL) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 257 | return AddLocalReference<jobject>(env, m); |
| 258 | } else { |
| 259 | return NULL; |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 260 | } |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 263 | jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass jklass, jobject jname) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 264 | Class* klass = Decode<Class*>(env, jklass); |
| 265 | DCHECK(klass->IsClass()); |
| 266 | String* name = Decode<String*>(env, jname); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 267 | DCHECK(name->GetClass()->IsStringClass()); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 268 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 269 | FieldHelper fh; |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 270 | for (size_t i = 0; i < klass->NumInstanceFields(); ++i) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 271 | Field* f = klass->GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 272 | fh.ChangeField(f); |
| 273 | if (name->Equals(fh.GetName())) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 274 | return AddLocalReference<jclass>(env, f); |
| 275 | } |
| 276 | } |
| 277 | for (size_t i = 0; i < klass->NumStaticFields(); ++i) { |
| 278 | Field* f = klass->GetStaticField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 279 | fh.ChangeField(f); |
| 280 | if (name->Equals(fh.GetName())) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 281 | return AddLocalReference<jclass>(env, f); |
| 282 | } |
| 283 | } |
| 284 | return NULL; |
| 285 | } |
| 286 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 287 | jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
Ian Rogers | f45b154 | 2012-02-03 18:03:48 -0800 | [diff] [blame] | 288 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 289 | Class* c = Decode<Class*>(env, javaThis); |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 290 | return AddLocalReference<jstring>(env, c->ComputeName()); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 293 | jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 294 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 295 | Class* lhs = Decode<Class*>(env, javaLhs); |
| 296 | Class* rhs = Decode<Class*>(env, javaRhs); |
| 297 | if (rhs == NULL) { |
| 298 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null"); |
| 299 | return JNI_FALSE; |
| 300 | } |
| 301 | return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE; |
| 302 | } |
| 303 | |
| 304 | jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) { |
| 305 | Class* c = Decode<Class*>(env, javaClass); |
| 306 | Object* o = Decode<Object*>(env, javaObject); |
| 307 | if (o == NULL) { |
| 308 | return JNI_FALSE; |
| 309 | } |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 310 | return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 313 | // Validate method/field access. |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 314 | static 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] | 315 | // quick accept for public access */ |
| 316 | if (member_flags & kAccPublic) { |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | // quick accept for access from same class |
| 321 | if (access_from == access_to) { |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | // quick reject for private access from another class |
| 326 | if (member_flags & kAccPrivate) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Semi-quick test for protected access from a sub-class, which may or |
| 331 | // may not be in the same package. |
| 332 | if (member_flags & kAccProtected) { |
| 333 | if (access_from->IsSubClass(access_to)) { |
| 334 | return true; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Allow protected and private access from other classes in the same |
| 339 | return access_from->IsInSamePackage(access_to); |
| 340 | } |
| 341 | |
| 342 | jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 343 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 344 | Class* c = Decode<Class*>(env, javaThis); |
| 345 | if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 346 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 347 | "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
| 350 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 351 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) { |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 355 | Method* init = c->FindDirectMethod("<init>", "()V"); |
| 356 | if (init == NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 357 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 358 | "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] | 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | // Verify access from the call site. |
| 363 | // |
| 364 | // First, make sure the method invoking Class.newInstance() has permission |
| 365 | // to access the class. |
| 366 | // |
| 367 | // Second, make sure it has permission to invoke the constructor. The |
| 368 | // constructor must be public or, if the caller is in the same package, |
| 369 | // have package scope. |
| 370 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
| 371 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 372 | frame.Next(); |
| 373 | frame.Next(); |
| 374 | Method* caller_caller = frame.GetMethod(); |
| 375 | Class* caller_class = caller_caller->GetDeclaringClass(); |
| 376 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 377 | ClassHelper caller_ch(caller_class); |
Brian Carlstrom | bc2f3e3 | 2011-09-22 17:16:54 -0700 | [diff] [blame] | 378 | if (!caller_class->CanAccess(c)) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 379 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;", |
| 380 | "Class %s is not accessible from class %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 381 | PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(), |
| 382 | PrettyDescriptor(caller_ch.GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
| 385 | if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 386 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;", |
| 387 | "%s is not accessible from class %s", |
| 388 | PrettyMethod(init).c_str(), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 389 | PrettyDescriptor(caller_ch.GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | Object* new_obj = c->AllocObject(); |
| 394 | if (new_obj == NULL) { |
| 395 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | // invoke constructor; unlike reflection calls, we don't wrap exceptions |
| 400 | jclass jklass = AddLocalReference<jclass>(env, c); |
| 401 | jmethodID mid = EncodeMethod(init); |
| 402 | return env->NewObject(jklass, mid); |
| 403 | } |
| 404 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 405 | static JNINativeMethod gMethods[] = { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 406 | NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 407 | NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 408 | NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"), |
| 409 | NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"), |
| 410 | NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"), |
| 411 | NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"), |
| 412 | NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"), |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 413 | NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 414 | NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 415 | NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"), |
| 416 | NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 417 | NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | } // namespace |
| 421 | |
| 422 | void register_java_lang_Class(JNIEnv* env) { |
| 423 | jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods)); |
| 424 | } |
| 425 | |
| 426 | } // namespace art |