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