blob: 5c834f7afd2f3df8e70a42d640fc99997cd17c42 [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
Elliott Hughes923e8b82012-03-23 11:44:07 -070029static Class* DecodeClass(JNIEnv* env, jobject java_class) {
Elliott Hughes15216932012-03-21 21:53:06 -070030 Class* c = Decode<Class*>(env, java_class);
31 DCHECK(c != NULL);
32 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070033 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
34 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
35 // every time probably doesn't make much difference to reflection performance anyway.
36 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070037}
38
Brian Carlstromf91c8c32011-09-21 17:30:34 -070039// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070040static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070041 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042 ScopedUtfChars name(env, javaName);
43 if (name.c_str() == NULL) {
44 return NULL;
45 }
46
47 // We need to validate and convert the name (from x.y.z to x/y/z). This
48 // is especially handy for array types, since we want to avoid
49 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070050 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070051 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070052 "Invalid name: %s", name.c_str());
53 return NULL;
54 }
55
56 std::string descriptor(DotToDescriptor(name.c_str()));
57 Object* loader = Decode<Object*>(env, javaLoader);
58 ClassLoader* class_loader = down_cast<ClassLoader*>(loader);
59 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
60 Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070061 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080062 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070063 env->ExceptionClear();
Elliott Hughes844f9a02012-01-24 20:19:58 -080064 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
65 static jmethodID ctor = env->GetMethodID(ClassNotFoundException_class, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
66 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(ClassNotFoundException_class, ctor, javaName, cause.get()));
67 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070068 return NULL;
69 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070070 if (initialize) {
71 class_linker->EnsureInitialized(c, true);
72 }
73 return AddLocalReference<jclass>(env, c);
74}
75
Elliott Hughes0512f022012-03-15 22:10:52 -070076static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Elliott Hughes923e8b82012-03-23 11:44:07 -070077 Class* c = DecodeClass(env, javaClass);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
79 return 0; // primitive, array and proxy classes don't have class definitions
80 }
81 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
82 if (class_def == NULL) {
83 return 0; // not found
84 } else {
85 return class_def->annotations_off_;
86 }
87}
88
Elliott Hughes80609252011-09-23 17:24:51 -070089template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -070090static jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) {
Elliott Hughes80609252011-09-23 17:24:51 -070091 jclass array_class = env->FindClass(array_class_name);
92 jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL);
93 for (size_t i = 0; i < objects.size(); ++i) {
94 ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i]));
95 env->SetObjectArrayElement(result, i, object.get());
96 }
97 return result;
98}
99
Ian Rogersd418eda2012-01-30 12:14:28 -0800100static bool IsVisibleConstructor(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700101 if (public_only && !m->IsPublic()) {
102 return false;
103 }
Ian Rogers9074b992011-10-26 17:41:55 -0700104 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700105 return false;
106 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800107 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -0700108}
109
Elliott Hughes0512f022012-03-15 22:10:52 -0700110static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700111 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700112 if (c == NULL) {
113 return NULL;
114 }
Elliott Hughes80609252011-09-23 17:24:51 -0700115
116 std::vector<Method*> constructors;
117 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
118 Method* m = c->GetDirectMethod(i);
119 if (IsVisibleConstructor(m, publicOnly)) {
120 constructors.push_back(m);
121 }
122 }
123
124 return ToArray(env, "java/lang/reflect/Constructor", constructors);
125}
126
Ian Rogersd418eda2012-01-30 12:14:28 -0800127static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700128 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700129 return false;
130 }
Elliott Hughes80609252011-09-23 17:24:51 -0700131 return true;
132}
133
Elliott Hughes0512f022012-03-15 22:10:52 -0700134static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700135 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700136 if (c == NULL) {
137 return NULL;
138 }
Elliott Hughes80609252011-09-23 17:24:51 -0700139
140 std::vector<Field*> fields;
141 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
142 Field* f = c->GetInstanceField(i);
143 if (IsVisibleField(f, publicOnly)) {
144 fields.push_back(f);
145 }
Jesse Wilson53494312011-11-29 16:43:09 -0500146 if (env->ExceptionOccurred()) {
147 return NULL;
148 }
Elliott Hughes80609252011-09-23 17:24:51 -0700149 }
150 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
151 Field* f = c->GetStaticField(i);
152 if (IsVisibleField(f, publicOnly)) {
153 fields.push_back(f);
154 }
Jesse Wilson53494312011-11-29 16:43:09 -0500155 if (env->ExceptionOccurred()) {
156 return NULL;
157 }
Elliott Hughes80609252011-09-23 17:24:51 -0700158 }
159
160 return ToArray(env, "java/lang/reflect/Field", fields);
161}
162
Ian Rogersd418eda2012-01-30 12:14:28 -0800163static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700164 if (public_only && !m->IsPublic()) {
165 return false;
166 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800167 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700168 return false;
169 }
Ian Rogers56437422012-02-06 21:46:00 -0800170 if (m->IsMiranda()) {
171 return false;
172 }
Elliott Hughes80609252011-09-23 17:24:51 -0700173 return true;
174}
175
Elliott Hughes0512f022012-03-15 22:10:52 -0700176static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700177 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700178 if (c == NULL) {
179 return NULL;
180 }
181
Elliott Hughes80609252011-09-23 17:24:51 -0700182 std::vector<Method*> methods;
183 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
184 Method* m = c->GetVirtualMethod(i);
185 if (IsVisibleMethod(m, publicOnly)) {
186 methods.push_back(m);
187 }
Jesse Wilson53494312011-11-29 16:43:09 -0500188 if (env->ExceptionOccurred()) {
189 return NULL;
190 }
Elliott Hughes80609252011-09-23 17:24:51 -0700191 }
192 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
193 Method* m = c->GetDirectMethod(i);
194 if (IsVisibleMethod(m, publicOnly)) {
195 methods.push_back(m);
196 }
Jesse Wilson53494312011-11-29 16:43:09 -0500197 if (env->ExceptionOccurred()) {
198 return NULL;
199 }
Elliott Hughes80609252011-09-23 17:24:51 -0700200 }
201
202 return ToArray(env, "java/lang/reflect/Method", methods);
203}
204
Elliott Hughes0512f022012-03-15 22:10:52 -0700205static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700206 Class* c = DecodeClass(env, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400207
208 DexCache* dex_cache = c->GetDexCache();
209 if (dex_cache == NULL) {
210 return NULL;
211 }
212
213 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
214}
215
Ian Rogersd418eda2012-01-30 12:14:28 -0800216static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700218 return false;
219 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800220 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
221 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
222 uint32_t sig_length = arg_array->GetLength();
223
224 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225 return false;
226 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227
228 for (uint32_t i = 0; i < sig_length; i++) {
229 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
230 return false;
231 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500232 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700233 return true;
234}
235
Ian Rogersd418eda2012-01-30 12:14:28 -0800236static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Elliott Hughes15216932012-03-21 21:53:06 -0700237 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500238 if (methods == NULL) {
239 return NULL;
240 }
241 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800242 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500243 for (int32_t i = 0; i < methods->GetLength(); ++i) {
244 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 mh.ChangeMethod(method);
246 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500247 continue;
248 }
249
250 result = method;
251
252 // Covariant return types permit the class to define multiple
253 // methods with the same name and parameter types. Prefer to return
254 // a non-synthetic method in such situations. We may still return
255 // a synthetic method to handle situations like escalated visibility.
256 if (!method->IsSynthetic()) {
257 break;
258 }
259 }
260 return result;
261}
262
Elliott Hughes0512f022012-03-15 22:10:52 -0700263static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
264 jobjectArray javaArgs) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700265 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700266 if (c == NULL) {
267 return NULL;
268 }
269
Elliott Hughes95572412011-12-13 18:14:20 -0800270 std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700272
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500274 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700276 }
277
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500278 if (m != NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500279 return AddLocalReference<jobject>(env, m);
280 } else {
281 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700282 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700283}
284
Elliott Hughes15216932012-03-21 21:53:06 -0700285static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700286 Class* c = DecodeClass(env, java_class);
Elliott Hughes15216932012-03-21 21:53:06 -0700287 if (c == NULL) {
288 return NULL;
289 }
290
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700291 String* name = Decode<String*>(env, jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800292 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700293
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700295 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
296 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 fh.ChangeField(f);
298 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700299 return AddLocalReference<jclass>(env, f);
300 }
301 }
Elliott Hughes15216932012-03-21 21:53:06 -0700302 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
303 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 fh.ChangeField(f);
305 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700306 return AddLocalReference<jclass>(env, f);
307 }
308 }
309 return NULL;
310}
311
Elliott Hughes0512f022012-03-15 22:10:52 -0700312static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogersf45b1542012-02-03 18:03:48 -0800313 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700314 Class* c = DecodeClass(env, javaThis);
Ian Rogersd418eda2012-01-30 12:14:28 -0800315 return AddLocalReference<jstring>(env, c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700316}
317
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700318static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
319 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700320 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(env, javaThis));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700321 return AddLocalReference<jobjectArray>(env, c->GetInterfaces()->Clone());
322}
323
Elliott Hughes0512f022012-03-15 22:10:52 -0700324static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700325 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700326 Class* lhs = DecodeClass(env, javaLhs);
327 Class* rhs = Decode<Class*>(env, javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700328 if (rhs == NULL) {
329 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
330 return JNI_FALSE;
331 }
332 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
333}
334
Elliott Hughes0512f022012-03-15 22:10:52 -0700335static jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700336 Class* c = DecodeClass(env, javaClass);
Elliott Hughesdd8df692011-09-23 14:42:41 -0700337 Object* o = Decode<Object*>(env, javaObject);
338 if (o == NULL) {
339 return JNI_FALSE;
340 }
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700341 return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughesdd8df692011-09-23 14:42:41 -0700342}
343
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700344// Validate method/field access.
Ian Rogersd418eda2012-01-30 12:14:28 -0800345static bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700346 // quick accept for public access */
347 if (member_flags & kAccPublic) {
348 return true;
349 }
350
351 // quick accept for access from same class
352 if (access_from == access_to) {
353 return true;
354 }
355
356 // quick reject for private access from another class
357 if (member_flags & kAccPrivate) {
358 return false;
359 }
360
361 // Semi-quick test for protected access from a sub-class, which may or
362 // may not be in the same package.
363 if (member_flags & kAccProtected) {
364 if (access_from->IsSubClass(access_to)) {
365 return true;
366 }
367 }
368
369 // Allow protected and private access from other classes in the same
370 return access_from->IsInSamePackage(access_to);
371}
372
Elliott Hughes0512f022012-03-15 22:10:52 -0700373static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700374 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700375 Class* c = DecodeClass(env, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700376 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700377 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800378 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700379 return NULL;
380 }
381
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700382 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
383 return NULL;
384 }
385
Brian Carlstrom01a96582012-03-12 15:17:29 -0700386 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700387 if (init == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700388 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700390 return NULL;
391 }
392
393 // Verify access from the call site.
394 //
395 // First, make sure the method invoking Class.newInstance() has permission
396 // to access the class.
397 //
398 // Second, make sure it has permission to invoke the constructor. The
399 // constructor must be public or, if the caller is in the same package,
400 // have package scope.
401 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
402 Frame frame = Thread::Current()->GetTopOfStack();
403 frame.Next();
404 frame.Next();
405 Method* caller_caller = frame.GetMethod();
406 Class* caller_class = caller_caller->GetDeclaringClass();
407
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700409 if (!caller_class->CanAccess(c)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700410 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
411 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
413 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700414 return NULL;
415 }
416 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700417 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
418 "%s is not accessible from class %s",
419 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800420 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700421 return NULL;
422 }
423
424 Object* new_obj = c->AllocObject();
425 if (new_obj == NULL) {
426 DCHECK(Thread::Current()->IsExceptionPending());
427 return NULL;
428 }
429
430 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Elliott Hughes15216932012-03-21 21:53:06 -0700431 jclass java_class = AddLocalReference<jclass>(env, c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700432 jmethodID mid = EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700433 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700434}
435
Elliott Hughesd369bb72011-09-12 14:41:14 -0700436static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700437 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
440 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
441 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
442 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
443 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400444 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700445 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700446 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700447 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
448 NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700449 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700450};
451
Elliott Hughesd369bb72011-09-12 14:41:14 -0700452void register_java_lang_Class(JNIEnv* env) {
453 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
454}
455
456} // namespace art