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