blob: 6cf7a2c82fb55c73e83ce5cf17cedc3f4ae21dfa [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;
jeffhao441d9122012-03-21 17:29:10 -0700141 FieldHelper fh;
Elliott Hughes80609252011-09-23 17:24:51 -0700142 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
143 Field* f = c->GetInstanceField(i);
jeffhao441d9122012-03-21 17:29:10 -0700144 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700145 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700146 if (fh.GetType() == NULL) {
147 DCHECK(env->ExceptionOccurred());
148 return NULL;
149 }
Elliott Hughes80609252011-09-23 17:24:51 -0700150 fields.push_back(f);
151 }
Jesse Wilson53494312011-11-29 16:43:09 -0500152 if (env->ExceptionOccurred()) {
153 return NULL;
154 }
Elliott Hughes80609252011-09-23 17:24:51 -0700155 }
156 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
157 Field* f = c->GetStaticField(i);
jeffhao441d9122012-03-21 17:29:10 -0700158 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700159 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700160 if (fh.GetType() == NULL) {
161 DCHECK(env->ExceptionOccurred());
162 return NULL;
163 }
Elliott Hughes80609252011-09-23 17:24:51 -0700164 fields.push_back(f);
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
171 return ToArray(env, "java/lang/reflect/Field", fields);
172}
173
Ian Rogersd418eda2012-01-30 12:14:28 -0800174static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700175 if (public_only && !m->IsPublic()) {
176 return false;
177 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800178 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700179 return false;
180 }
Ian Rogers56437422012-02-06 21:46:00 -0800181 if (m->IsMiranda()) {
182 return false;
183 }
Elliott Hughes80609252011-09-23 17:24:51 -0700184 return true;
185}
186
Elliott Hughes0512f022012-03-15 22:10:52 -0700187static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
jeffhao441d9122012-03-21 17:29:10 -0700188 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700189 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700190 if (c == NULL) {
191 return NULL;
192 }
193
Elliott Hughes80609252011-09-23 17:24:51 -0700194 std::vector<Method*> methods;
jeffhao441d9122012-03-21 17:29:10 -0700195 MethodHelper mh;
Elliott Hughes80609252011-09-23 17:24:51 -0700196 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
197 Method* m = c->GetVirtualMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700198 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700199 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700200 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
201 DCHECK(env->ExceptionOccurred());
202 return NULL;
203 }
Elliott Hughes80609252011-09-23 17:24:51 -0700204 methods.push_back(m);
205 }
Jesse Wilson53494312011-11-29 16:43:09 -0500206 if (env->ExceptionOccurred()) {
207 return NULL;
208 }
Elliott Hughes80609252011-09-23 17:24:51 -0700209 }
210 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
211 Method* m = c->GetDirectMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700212 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700213 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700214 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
215 DCHECK(env->ExceptionOccurred());
216 return NULL;
217 }
Elliott Hughes80609252011-09-23 17:24:51 -0700218 methods.push_back(m);
219 }
Jesse Wilson53494312011-11-29 16:43:09 -0500220 if (env->ExceptionOccurred()) {
221 return NULL;
222 }
Elliott Hughes80609252011-09-23 17:24:51 -0700223 }
224
225 return ToArray(env, "java/lang/reflect/Method", methods);
226}
227
Elliott Hughes0512f022012-03-15 22:10:52 -0700228static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700229 Class* c = DecodeClass(env, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400230
231 DexCache* dex_cache = c->GetDexCache();
232 if (dex_cache == NULL) {
233 return NULL;
234 }
235
236 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
237}
238
Ian Rogersd418eda2012-01-30 12:14:28 -0800239static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 return false;
242 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
244 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
245 uint32_t sig_length = arg_array->GetLength();
246
247 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700248 return false;
249 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800250
251 for (uint32_t i = 0; i < sig_length; i++) {
252 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
253 return false;
254 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500255 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700256 return true;
257}
258
Ian Rogersd418eda2012-01-30 12:14:28 -0800259static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Elliott Hughes15216932012-03-21 21:53:06 -0700260 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500261 if (methods == NULL) {
262 return NULL;
263 }
264 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500266 for (int32_t i = 0; i < methods->GetLength(); ++i) {
267 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 mh.ChangeMethod(method);
269 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500270 continue;
271 }
272
273 result = method;
274
275 // Covariant return types permit the class to define multiple
276 // methods with the same name and parameter types. Prefer to return
277 // a non-synthetic method in such situations. We may still return
278 // a synthetic method to handle situations like escalated visibility.
279 if (!method->IsSynthetic()) {
280 break;
281 }
282 }
283 return result;
284}
285
Elliott Hughes0512f022012-03-15 22:10:52 -0700286static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
287 jobjectArray javaArgs) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700288 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700289 if (c == NULL) {
290 return NULL;
291 }
292
Elliott Hughes95572412011-12-13 18:14:20 -0800293 std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700295
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500297 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700299 }
300
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500301 if (m != NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500302 return AddLocalReference<jobject>(env, m);
303 } else {
304 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700305 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700306}
307
Elliott Hughes15216932012-03-21 21:53:06 -0700308static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
jeffhao441d9122012-03-21 17:29:10 -0700309 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700310 Class* c = DecodeClass(env, java_class);
Elliott Hughes15216932012-03-21 21:53:06 -0700311 if (c == NULL) {
312 return NULL;
313 }
314
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700315 String* name = Decode<String*>(env, jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800316 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700317
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700319 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
320 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 fh.ChangeField(f);
322 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700323 if (fh.GetType() == NULL) {
324 DCHECK(env->ExceptionOccurred());
325 return NULL;
326 }
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700327 return AddLocalReference<jclass>(env, f);
328 }
329 }
Elliott Hughes15216932012-03-21 21:53:06 -0700330 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
331 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800332 fh.ChangeField(f);
333 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700334 if (fh.GetType() == NULL) {
335 DCHECK(env->ExceptionOccurred());
336 return NULL;
337 }
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700338 return AddLocalReference<jclass>(env, f);
339 }
340 }
341 return NULL;
342}
343
Elliott Hughes0512f022012-03-15 22:10:52 -0700344static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogersf45b1542012-02-03 18:03:48 -0800345 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700346 Class* c = DecodeClass(env, javaThis);
Ian Rogersd418eda2012-01-30 12:14:28 -0800347 return AddLocalReference<jstring>(env, c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700348}
349
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700350static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
351 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700352 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(env, javaThis));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700353 return AddLocalReference<jobjectArray>(env, c->GetInterfaces()->Clone());
354}
355
Elliott Hughes0512f022012-03-15 22:10:52 -0700356static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700357 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700358 Class* lhs = DecodeClass(env, javaLhs);
359 Class* rhs = Decode<Class*>(env, javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700360 if (rhs == NULL) {
361 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
362 return JNI_FALSE;
363 }
364 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
365}
366
Elliott Hughes0512f022012-03-15 22:10:52 -0700367static jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700368 Class* c = DecodeClass(env, javaClass);
Elliott Hughesdd8df692011-09-23 14:42:41 -0700369 Object* o = Decode<Object*>(env, javaObject);
370 if (o == NULL) {
371 return JNI_FALSE;
372 }
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700373 return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughesdd8df692011-09-23 14:42:41 -0700374}
375
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700376// Validate method/field access.
Ian Rogersd418eda2012-01-30 12:14:28 -0800377static bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700378 // quick accept for public access */
379 if (member_flags & kAccPublic) {
380 return true;
381 }
382
383 // quick accept for access from same class
384 if (access_from == access_to) {
385 return true;
386 }
387
388 // quick reject for private access from another class
389 if (member_flags & kAccPrivate) {
390 return false;
391 }
392
393 // Semi-quick test for protected access from a sub-class, which may or
394 // may not be in the same package.
395 if (member_flags & kAccProtected) {
396 if (access_from->IsSubClass(access_to)) {
397 return true;
398 }
399 }
400
401 // Allow protected and private access from other classes in the same
402 return access_from->IsInSamePackage(access_to);
403}
404
Elliott Hughes0512f022012-03-15 22:10:52 -0700405static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700406 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700407 Class* c = DecodeClass(env, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700408 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700409 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700411 return NULL;
412 }
413
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700414 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
415 return NULL;
416 }
417
Brian Carlstrom01a96582012-03-12 15:17:29 -0700418 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700419 if (init == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700420 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700422 return NULL;
423 }
424
425 // Verify access from the call site.
426 //
427 // First, make sure the method invoking Class.newInstance() has permission
428 // to access the class.
429 //
430 // Second, make sure it has permission to invoke the constructor. The
431 // constructor must be public or, if the caller is in the same package,
432 // have package scope.
433 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
434 Frame frame = Thread::Current()->GetTopOfStack();
435 frame.Next();
436 frame.Next();
437 Method* caller_caller = frame.GetMethod();
438 Class* caller_class = caller_caller->GetDeclaringClass();
439
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700441 if (!caller_class->CanAccess(c)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700442 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
443 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
445 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700446 return NULL;
447 }
448 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700449 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
450 "%s is not accessible from class %s",
451 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800452 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700453 return NULL;
454 }
455
456 Object* new_obj = c->AllocObject();
457 if (new_obj == NULL) {
458 DCHECK(Thread::Current()->IsExceptionPending());
459 return NULL;
460 }
461
462 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Elliott Hughes15216932012-03-21 21:53:06 -0700463 jclass java_class = AddLocalReference<jclass>(env, c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700464 jmethodID mid = EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700465 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700466}
467
Elliott Hughesd369bb72011-09-12 14:41:14 -0700468static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700469 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800470 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
472 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
473 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
474 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
475 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400476 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700477 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700478 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700479 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
480 NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700481 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700482};
483
Elliott Hughesd369bb72011-09-12 14:41:14 -0700484void register_java_lang_Class(JNIEnv* env) {
485 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
486}
487
488} // namespace art