blob: 0a5a9edde41faafeccb2a64df9c93deb8d30185c [file] [log] [blame]
Elliott Hughesd369bb72011-09-12 14:41:14 -07001/*
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 Carlstromf91c8c32011-09-21 17:30:34 -070019#include "class_loader.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070020#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Elliott Hughes80609252011-09-23 17:24:51 -070022#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070023#include "ScopedUtfChars.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070024
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26
27namespace art {
28
29namespace {
30
Brian Carlstromf91c8c32011-09-21 17:30:34 -070031// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
32jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070033 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070034 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 Hughes906e6852011-10-28 14:52:10 -070042 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070043 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044 "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 Carlstrom395520e2011-09-25 19:35:00 -070053 if (c == NULL) {
54 // Convert NoClassDefFoundError to ClassNotFoundException
55 // TODO: chain exceptions?
56 DCHECK(env->ExceptionCheck());
57 env->ExceptionClear();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070058 Thread::Current()->ThrowNewException("Ljava/lang/ClassNotFoundException;", name.c_str());
Brian Carlstrom395520e2011-09-25 19:35:00 -070059 return NULL;
60 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070061 if (initialize) {
62 class_linker->EnsureInitialized(c, true);
63 }
64 return AddLocalReference<jclass>(env, c);
65}
66
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
68 Class* c = Decode<Class*>(env, javaClass);
69 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
70 return 0; // primitive, array and proxy classes don't have class definitions
71 }
72 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
73 if (class_def == NULL) {
74 return 0; // not found
75 } else {
76 return class_def->annotations_off_;
77 }
78}
79
Elliott Hughes80609252011-09-23 17:24:51 -070080template<typename T>
81jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) {
82 jclass array_class = env->FindClass(array_class_name);
83 jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL);
84 for (size_t i = 0; i < objects.size(); ++i) {
85 ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i]));
86 env->SetObjectArrayElement(result, i, object.get());
87 }
88 return result;
89}
90
91bool IsVisibleConstructor(Method* m, bool public_only) {
92 if (public_only && !m->IsPublic()) {
93 return false;
94 }
Ian Rogers9074b992011-10-26 17:41:55 -070095 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -070096 return false;
97 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080098 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -070099}
100
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800101jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes80609252011-09-23 17:24:51 -0700102 Class* c = Decode<Class*>(env, javaClass);
103
104 std::vector<Method*> constructors;
105 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
106 Method* m = c->GetDirectMethod(i);
107 if (IsVisibleConstructor(m, publicOnly)) {
108 constructors.push_back(m);
109 }
110 }
111
112 return ToArray(env, "java/lang/reflect/Constructor", constructors);
113}
114
115bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700116 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700117 return false;
118 }
Elliott Hughes80609252011-09-23 17:24:51 -0700119 return true;
120}
121
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800122jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes80609252011-09-23 17:24:51 -0700123 Class* c = Decode<Class*>(env, javaClass);
124
125 std::vector<Field*> fields;
126 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
127 Field* f = c->GetInstanceField(i);
128 if (IsVisibleField(f, publicOnly)) {
129 fields.push_back(f);
130 }
Jesse Wilson53494312011-11-29 16:43:09 -0500131 if (env->ExceptionOccurred()) {
132 return NULL;
133 }
Elliott Hughes80609252011-09-23 17:24:51 -0700134 }
135 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
136 Field* f = c->GetStaticField(i);
137 if (IsVisibleField(f, publicOnly)) {
138 fields.push_back(f);
139 }
Jesse Wilson53494312011-11-29 16:43:09 -0500140 if (env->ExceptionOccurred()) {
141 return NULL;
142 }
Elliott Hughes80609252011-09-23 17:24:51 -0700143 }
144
145 return ToArray(env, "java/lang/reflect/Field", fields);
146}
147
148bool IsVisibleMethod(Method* m, bool public_only) {
149 if (public_only && !m->IsPublic()) {
150 return false;
151 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800152 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700153 return false;
154 }
Elliott Hughes80609252011-09-23 17:24:51 -0700155 return true;
156}
157
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800158jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes80609252011-09-23 17:24:51 -0700159 Class* c = Decode<Class*>(env, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700160 std::vector<Method*> methods;
161 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
162 Method* m = c->GetVirtualMethod(i);
163 if (IsVisibleMethod(m, publicOnly)) {
164 methods.push_back(m);
165 }
Jesse Wilson53494312011-11-29 16:43:09 -0500166 if (env->ExceptionOccurred()) {
167 return NULL;
168 }
Elliott Hughes80609252011-09-23 17:24:51 -0700169 }
170 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
171 Method* m = c->GetDirectMethod(i);
172 if (IsVisibleMethod(m, publicOnly)) {
173 methods.push_back(m);
174 }
Jesse Wilson53494312011-11-29 16:43:09 -0500175 if (env->ExceptionOccurred()) {
176 return NULL;
177 }
Elliott Hughes80609252011-09-23 17:24:51 -0700178 }
179
180 return ToArray(env, "java/lang/reflect/Method", methods);
181}
182
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700183jboolean Class_desiredAssertionStatus(JNIEnv* env, jobject javaThis) {
184 return JNI_FALSE;
185}
186
Jesse Wilson6bf19152011-09-29 13:12:33 -0400187jobject 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 Rogers6d4d9fc2011-11-30 16:24:48 -0800198jint Class_getNonInnerClassModifiers(JNIEnv* env, jclass javaClass) {
Elliott Hughes9dcc79d2011-10-02 16:31:10 -0700199 Class* c = Decode<Class*>(env, javaClass);
Jesse Wilson163c4642011-10-12 10:42:15 -0400200 return c->GetAccessFlags() & kAccJavaFlagsMask;
Elliott Hughes9dcc79d2011-10-02 16:31:10 -0700201}
202
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203jobject Class_getClassLoaderNative(JNIEnv* env, jclass javaClass) {
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700204 Class* c = Decode<Class*>(env, javaClass);
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800205 Object* result = c->GetClassLoader();
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700206 return AddLocalReference<jobject>(env, result);
207}
208
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209jclass Class_getComponentType(JNIEnv* env, jclass javaClass) {
210 return AddLocalReference<jclass>(env, Decode<Class*>(env, javaClass)->GetComponentType());
Elliott Hughesd369bb72011-09-12 14:41:14 -0700211}
212
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800213bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
214 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700215 return false;
216 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
218 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
219 uint32_t sig_length = arg_array->GetLength();
220
221 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700222 return false;
223 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800224
225 for (uint32_t i = 0; i < sig_length; i++) {
226 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
227 return false;
228 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500229 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700230 return true;
231}
232
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
234 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500235 if (methods == NULL) {
236 return NULL;
237 }
238 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800239 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500240 for (int32_t i = 0; i < methods->GetLength(); ++i) {
241 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800242 mh.ChangeMethod(method);
243 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500244 continue;
245 }
246
247 result = method;
248
249 // Covariant return types permit the class to define multiple
250 // methods with the same name and parameter types. Prefer to return
251 // a non-synthetic method in such situations. We may still return
252 // a synthetic method to handle situations like escalated visibility.
253 if (!method->IsSynthetic()) {
254 break;
255 }
256 }
257 return result;
258}
259
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
261 jobjectArray javaArgs) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700262 Class* c = Decode<Class*>(env, javaClass);
Elliott Hughes95572412011-12-13 18:14:20 -0800263 std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700265
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500267 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700269 }
270
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500271 if (m != NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500272 return AddLocalReference<jobject>(env, m);
273 } else {
274 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700275 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700276}
277
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800278jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass jklass, jobject jname) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700279 Class* klass = Decode<Class*>(env, jklass);
280 DCHECK(klass->IsClass());
281 String* name = Decode<String*>(env, jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800282 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700283
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 FieldHelper fh;
Elliott Hughes80609252011-09-23 17:24:51 -0700285 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700286 Field* f = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 fh.ChangeField(f);
288 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700289 return AddLocalReference<jclass>(env, f);
290 }
291 }
292 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
293 Field* f = klass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 fh.ChangeField(f);
295 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700296 return AddLocalReference<jclass>(env, f);
297 }
298 }
299 return NULL;
300}
301
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700302/*
303 * private native String getNameNative()
304 *
305 * Return the class' name. The exact format is bizarre, but it's the specified
306 * behavior: keywords for primitive types, regular "[I" form for primitive
307 * arrays (so "int" but "[I"), and arrays of reference types written
308 * between "L" and ";" but with dots rather than slashes (so "java.lang.String"
309 * but "[Ljava.lang.String;"). Madness.
310 */
311jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
312 Class* c = Decode<Class*>(env, javaThis);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 std::string descriptor(ClassHelper(c).GetDescriptor());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700314 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
315 // The descriptor indicates that this is the class for
316 // a primitive type; special-case the return value.
317 const char* name = NULL;
318 switch (descriptor[0]) {
319 case 'Z': name = "boolean"; break;
320 case 'B': name = "byte"; break;
321 case 'C': name = "char"; break;
322 case 'S': name = "short"; break;
323 case 'I': name = "int"; break;
324 case 'J': name = "long"; break;
325 case 'F': name = "float"; break;
326 case 'D': name = "double"; break;
327 case 'V': name = "void"; break;
328 default:
329 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
330 }
331 return env->NewStringUTF(name);
332 }
333
334 // Convert the UTF-8 name to a java.lang.String. The
335 // name must use '.' to separate package components.
336 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
337 descriptor.erase(0, 1);
338 descriptor.erase(descriptor.size() - 1);
339 }
340 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
341 return env->NewStringUTF(descriptor.c_str());
342}
343
344jclass Class_getSuperclass(JNIEnv* env, jobject javaThis) {
345 Class* c = Decode<Class*>(env, javaThis);
346 Class* result = c->GetSuperClass();
347 return AddLocalReference<jclass>(env, result);
348}
349
Elliott Hughesdd8df692011-09-23 14:42:41 -0700350jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700351 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughesdd8df692011-09-23 14:42:41 -0700352 Class* lhs = Decode<Class*>(env, javaLhs);
353 Class* rhs = Decode<Class*>(env, javaRhs);
354 if (rhs == NULL) {
355 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
356 return JNI_FALSE;
357 }
358 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
359}
360
361jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) {
362 Class* c = Decode<Class*>(env, javaClass);
363 Object* o = Decode<Object*>(env, javaObject);
364 if (o == NULL) {
365 return JNI_FALSE;
366 }
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700367 return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughesdd8df692011-09-23 14:42:41 -0700368}
369
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700370jboolean Class_isInterface(JNIEnv* env, jobject javaThis) {
371 Class* c = Decode<Class*>(env, javaThis);
372 return c->IsInterface();
373}
374
375jboolean Class_isPrimitive(JNIEnv* env, jobject javaThis) {
376 Class* c = Decode<Class*>(env, javaThis);
377 return c->IsPrimitive();
378}
379
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700380// Validate method/field access.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700382 // quick accept for public access */
383 if (member_flags & kAccPublic) {
384 return true;
385 }
386
387 // quick accept for access from same class
388 if (access_from == access_to) {
389 return true;
390 }
391
392 // quick reject for private access from another class
393 if (member_flags & kAccPrivate) {
394 return false;
395 }
396
397 // Semi-quick test for protected access from a sub-class, which may or
398 // may not be in the same package.
399 if (member_flags & kAccProtected) {
400 if (access_from->IsSubClass(access_to)) {
401 return true;
402 }
403 }
404
405 // Allow protected and private access from other classes in the same
406 return access_from->IsInSamePackage(access_to);
407}
408
409jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700410 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700411 Class* c = Decode<Class*>(env, javaThis);
412 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700413 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700415 return NULL;
416 }
417
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700418 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
419 return NULL;
420 }
421
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700422 Method* init = c->FindDirectMethod("<init>", "()V");
423 if (init == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700424 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800425 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700426 return NULL;
427 }
428
429 // Verify access from the call site.
430 //
431 // First, make sure the method invoking Class.newInstance() has permission
432 // to access the class.
433 //
434 // Second, make sure it has permission to invoke the constructor. The
435 // constructor must be public or, if the caller is in the same package,
436 // have package scope.
437 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
438 Frame frame = Thread::Current()->GetTopOfStack();
439 frame.Next();
440 frame.Next();
441 Method* caller_caller = frame.GetMethod();
442 Class* caller_class = caller_caller->GetDeclaringClass();
443
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700445 if (!caller_class->CanAccess(c)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700446 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
447 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800448 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
449 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700450 return NULL;
451 }
452 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700453 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
454 "%s is not accessible from class %s",
455 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800456 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700457 return NULL;
458 }
459
460 Object* new_obj = c->AllocObject();
461 if (new_obj == NULL) {
462 DCHECK(Thread::Current()->IsExceptionPending());
463 return NULL;
464 }
465
466 // invoke constructor; unlike reflection calls, we don't wrap exceptions
467 jclass jklass = AddLocalReference<jclass>(env, c);
468 jmethodID mid = EncodeMethod(init);
469 return env->NewObject(jklass, mid);
470}
471
Elliott Hughesd369bb72011-09-12 14:41:14 -0700472static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700473 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700474 NATIVE_METHOD(Class, desiredAssertionStatus, "()Z"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800475 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
476 NATIVE_METHOD(Class, getClassLoaderNative, "()Ljava/lang/ClassLoader;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700477 NATIVE_METHOD(Class, getComponentType, "()Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800478 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
479 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
480 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
481 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
482 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400483 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800484 NATIVE_METHOD(Class, getNonInnerClassModifiers, "()I"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700485 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700486 NATIVE_METHOD(Class, getSuperclass, "()Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700487 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
488 NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700489 NATIVE_METHOD(Class, isInterface, "()Z"),
490 NATIVE_METHOD(Class, isPrimitive, "()Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700491 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700492};
493
494} // namespace
495
496void register_java_lang_Class(JNIEnv* env) {
497 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
498}
499
500} // namespace art