blob: 2cd70900b10931e20be83688c3795aa74952970a [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 Hughes6a144332012-04-03 13:07:11 -070020#include "nth_caller_visitor.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070021#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Elliott Hughes80609252011-09-23 17:24:51 -070023#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070024#include "ScopedUtfChars.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070025
26#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
27
28namespace art {
29
Elliott Hughes923e8b82012-03-23 11:44:07 -070030static Class* DecodeClass(JNIEnv* env, jobject java_class) {
Elliott Hughes15216932012-03-21 21:53:06 -070031 Class* c = Decode<Class*>(env, java_class);
32 DCHECK(c != NULL);
33 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070034 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
35 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
36 // every time probably doesn't make much difference to reflection performance anyway.
37 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070038}
39
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070041static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Elliott Hughes34e06962012-04-09 13:55:55 -070042 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070043 ScopedUtfChars name(env, javaName);
44 if (name.c_str() == NULL) {
45 return NULL;
46 }
47
48 // We need to validate and convert the name (from x.y.z to x/y/z). This
49 // is especially handy for array types, since we want to avoid
50 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070051 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070052 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070053 "Invalid name: %s", name.c_str());
54 return NULL;
55 }
56
57 std::string descriptor(DotToDescriptor(name.c_str()));
58 Object* loader = Decode<Object*>(env, javaLoader);
59 ClassLoader* class_loader = down_cast<ClassLoader*>(loader);
60 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070062 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080063 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070064 env->ExceptionClear();
Elliott Hughes844f9a02012-01-24 20:19:58 -080065 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
66 static jmethodID ctor = env->GetMethodID(ClassNotFoundException_class, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
67 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(ClassNotFoundException_class, ctor, javaName, cause.get()));
68 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070069 return NULL;
70 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071 if (initialize) {
Ian Rogers0045a292012-03-31 21:08:41 -070072 class_linker->EnsureInitialized(c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070073 }
74 return AddLocalReference<jclass>(env, c);
75}
76
Elliott Hughes0512f022012-03-15 22:10:52 -070077static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Elliott Hughes923e8b82012-03-23 11:44:07 -070078 Class* c = DecodeClass(env, javaClass);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080079 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
80 return 0; // primitive, array and proxy classes don't have class definitions
81 }
82 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
83 if (class_def == NULL) {
84 return 0; // not found
85 } else {
86 return class_def->annotations_off_;
87 }
88}
89
Elliott Hughes80609252011-09-23 17:24:51 -070090template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -070091static jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) {
Elliott Hughes80609252011-09-23 17:24:51 -070092 jclass array_class = env->FindClass(array_class_name);
93 jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL);
94 for (size_t i = 0; i < objects.size(); ++i) {
95 ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i]));
96 env->SetObjectArrayElement(result, i, object.get());
97 }
98 return result;
99}
100
Ian Rogersd418eda2012-01-30 12:14:28 -0800101static bool IsVisibleConstructor(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700102 if (public_only && !m->IsPublic()) {
103 return false;
104 }
Ian Rogers9074b992011-10-26 17:41:55 -0700105 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700106 return false;
107 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800108 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -0700109}
110
Elliott Hughes0512f022012-03-15 22:10:52 -0700111static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700112 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700113 if (c == NULL) {
114 return NULL;
115 }
Elliott Hughes80609252011-09-23 17:24:51 -0700116
117 std::vector<Method*> constructors;
118 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
119 Method* m = c->GetDirectMethod(i);
120 if (IsVisibleConstructor(m, publicOnly)) {
121 constructors.push_back(m);
122 }
123 }
124
125 return ToArray(env, "java/lang/reflect/Constructor", constructors);
126}
127
Ian Rogersd418eda2012-01-30 12:14:28 -0800128static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700129 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700130 return false;
131 }
Elliott Hughes80609252011-09-23 17:24:51 -0700132 return true;
133}
134
Elliott Hughes0512f022012-03-15 22:10:52 -0700135static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700136 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700137 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700138 if (c == NULL) {
139 return NULL;
140 }
Elliott Hughes80609252011-09-23 17:24:51 -0700141
142 std::vector<Field*> fields;
jeffhao441d9122012-03-21 17:29:10 -0700143 FieldHelper fh;
Elliott Hughes80609252011-09-23 17:24:51 -0700144 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
145 Field* f = c->GetInstanceField(i);
jeffhao441d9122012-03-21 17:29:10 -0700146 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700147 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700148 if (fh.GetType() == NULL) {
149 DCHECK(env->ExceptionOccurred());
150 return NULL;
151 }
Elliott Hughes80609252011-09-23 17:24:51 -0700152 fields.push_back(f);
153 }
Jesse Wilson53494312011-11-29 16:43:09 -0500154 if (env->ExceptionOccurred()) {
155 return NULL;
156 }
Elliott Hughes80609252011-09-23 17:24:51 -0700157 }
158 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
159 Field* f = c->GetStaticField(i);
jeffhao441d9122012-03-21 17:29:10 -0700160 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700161 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700162 if (fh.GetType() == NULL) {
163 DCHECK(env->ExceptionOccurred());
164 return NULL;
165 }
Elliott Hughes80609252011-09-23 17:24:51 -0700166 fields.push_back(f);
167 }
Jesse Wilson53494312011-11-29 16:43:09 -0500168 if (env->ExceptionOccurred()) {
169 return NULL;
170 }
Elliott Hughes80609252011-09-23 17:24:51 -0700171 }
172
173 return ToArray(env, "java/lang/reflect/Field", fields);
174}
175
Ian Rogersd418eda2012-01-30 12:14:28 -0800176static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700177 if (public_only && !m->IsPublic()) {
178 return false;
179 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700181 return false;
182 }
Ian Rogers56437422012-02-06 21:46:00 -0800183 if (m->IsMiranda()) {
184 return false;
185 }
Elliott Hughes80609252011-09-23 17:24:51 -0700186 return true;
187}
188
Elliott Hughes0512f022012-03-15 22:10:52 -0700189static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700190 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700191 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700192 if (c == NULL) {
193 return NULL;
194 }
195
Elliott Hughes80609252011-09-23 17:24:51 -0700196 std::vector<Method*> methods;
jeffhao441d9122012-03-21 17:29:10 -0700197 MethodHelper mh;
Elliott Hughes80609252011-09-23 17:24:51 -0700198 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
199 Method* m = c->GetVirtualMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700200 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700201 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700202 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
203 DCHECK(env->ExceptionOccurred());
204 return NULL;
205 }
Elliott Hughes80609252011-09-23 17:24:51 -0700206 methods.push_back(m);
207 }
Jesse Wilson53494312011-11-29 16:43:09 -0500208 if (env->ExceptionOccurred()) {
209 return NULL;
210 }
Elliott Hughes80609252011-09-23 17:24:51 -0700211 }
212 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
213 Method* m = c->GetDirectMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700214 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700215 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700216 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
217 DCHECK(env->ExceptionOccurred());
218 return NULL;
219 }
Elliott Hughes80609252011-09-23 17:24:51 -0700220 methods.push_back(m);
221 }
Jesse Wilson53494312011-11-29 16:43:09 -0500222 if (env->ExceptionOccurred()) {
223 return NULL;
224 }
Elliott Hughes80609252011-09-23 17:24:51 -0700225 }
226
227 return ToArray(env, "java/lang/reflect/Method", methods);
228}
229
Elliott Hughes0512f022012-03-15 22:10:52 -0700230static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700231 Class* c = DecodeClass(env, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400232
233 DexCache* dex_cache = c->GetDexCache();
234 if (dex_cache == NULL) {
235 return NULL;
236 }
237
238 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
239}
240
Ian Rogersd418eda2012-01-30 12:14:28 -0800241static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800242 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 return false;
244 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
246 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
247 uint32_t sig_length = arg_array->GetLength();
248
249 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700250 return false;
251 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252
253 for (uint32_t i = 0; i < sig_length; i++) {
254 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
255 return false;
256 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500257 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700258 return true;
259}
260
Ian Rogersd418eda2012-01-30 12:14:28 -0800261static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Elliott Hughes15216932012-03-21 21:53:06 -0700262 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500263 if (methods == NULL) {
264 return NULL;
265 }
266 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500268 for (int32_t i = 0; i < methods->GetLength(); ++i) {
269 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 mh.ChangeMethod(method);
271 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500272 continue;
273 }
274
275 result = method;
276
277 // Covariant return types permit the class to define multiple
278 // methods with the same name and parameter types. Prefer to return
279 // a non-synthetic method in such situations. We may still return
280 // a synthetic method to handle situations like escalated visibility.
281 if (!method->IsSynthetic()) {
282 break;
283 }
284 }
285 return result;
286}
287
Elliott Hughes0512f022012-03-15 22:10:52 -0700288static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
289 jobjectArray javaArgs) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700290 Class* c = DecodeClass(env, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700291 if (c == NULL) {
292 return NULL;
293 }
294
Elliott Hughes95572412011-12-13 18:14:20 -0800295 std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700297
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500299 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700301 }
302
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500303 if (m != NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500304 return AddLocalReference<jobject>(env, m);
305 } else {
306 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700307 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700308}
309
Elliott Hughes15216932012-03-21 21:53:06 -0700310static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700311 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700312 Class* c = DecodeClass(env, java_class);
Elliott Hughes15216932012-03-21 21:53:06 -0700313 if (c == NULL) {
314 return NULL;
315 }
316
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700317 String* name = Decode<String*>(env, jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800318 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700319
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800320 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700321 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
322 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800323 fh.ChangeField(f);
324 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700325 if (fh.GetType() == NULL) {
326 DCHECK(env->ExceptionOccurred());
327 return NULL;
328 }
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700329 return AddLocalReference<jclass>(env, f);
330 }
331 }
Elliott Hughes15216932012-03-21 21:53:06 -0700332 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
333 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 fh.ChangeField(f);
335 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700336 if (fh.GetType() == NULL) {
337 DCHECK(env->ExceptionOccurred());
338 return NULL;
339 }
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700340 return AddLocalReference<jclass>(env, f);
341 }
342 }
343 return NULL;
344}
345
Elliott Hughes0512f022012-03-15 22:10:52 -0700346static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700347 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700348 Class* c = DecodeClass(env, javaThis);
Ian Rogersd418eda2012-01-30 12:14:28 -0800349 return AddLocalReference<jstring>(env, c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700350}
351
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700352static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700353 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700354 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(env, javaThis));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700355 return AddLocalReference<jobjectArray>(env, c->GetInterfaces()->Clone());
356}
357
Elliott Hughes0512f022012-03-15 22:10:52 -0700358static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700359 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700360 Class* lhs = DecodeClass(env, javaLhs);
361 Class* rhs = Decode<Class*>(env, javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700362 if (rhs == NULL) {
363 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
364 return JNI_FALSE;
365 }
366 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
367}
368
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700369// Validate method/field access.
Ian Rogersd418eda2012-01-30 12:14:28 -0800370static bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700371 // quick accept for public access */
372 if (member_flags & kAccPublic) {
373 return true;
374 }
375
376 // quick accept for access from same class
377 if (access_from == access_to) {
378 return true;
379 }
380
381 // quick reject for private access from another class
382 if (member_flags & kAccPrivate) {
383 return false;
384 }
385
386 // Semi-quick test for protected access from a sub-class, which may or
387 // may not be in the same package.
388 if (member_flags & kAccProtected) {
389 if (access_from->IsSubClass(access_to)) {
390 return true;
391 }
392 }
393
394 // Allow protected and private access from other classes in the same
395 return access_from->IsInSamePackage(access_to);
396}
397
Elliott Hughes0512f022012-03-15 22:10:52 -0700398static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700399 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -0700400 Class* c = DecodeClass(env, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700401 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700402 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700404 return NULL;
405 }
406
Ian Rogers0045a292012-03-31 21:08:41 -0700407 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700408 return NULL;
409 }
410
Brian Carlstrom01a96582012-03-12 15:17:29 -0700411 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700412 if (init == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700413 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700415 return NULL;
416 }
417
418 // Verify access from the call site.
419 //
420 // First, make sure the method invoking Class.newInstance() has permission
421 // to access the class.
422 //
423 // Second, make sure it has permission to invoke the constructor. The
424 // constructor must be public or, if the caller is in the same package,
425 // have package scope.
Elliott Hughes6a144332012-04-03 13:07:11 -0700426
427 NthCallerVisitor visitor(2);
428 Thread::Current()->WalkStack(&visitor);
TDYa1273f9137d2012-04-08 15:59:19 -0700429 Class* caller_class = visitor.caller->GetDeclaringClass();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700430
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800431 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700432 if (!caller_class->CanAccess(c)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700433 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
434 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
436 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700437 return NULL;
438 }
439 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700440 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
441 "%s is not accessible from class %s",
442 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700444 return NULL;
445 }
446
447 Object* new_obj = c->AllocObject();
448 if (new_obj == NULL) {
449 DCHECK(Thread::Current()->IsExceptionPending());
450 return NULL;
451 }
452
453 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Elliott Hughes15216932012-03-21 21:53:06 -0700454 jclass java_class = AddLocalReference<jclass>(env, c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700455 jmethodID mid = EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700456 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700457}
458
Elliott Hughesd369bb72011-09-12 14:41:14 -0700459static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700460 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800461 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800462 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
463 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
464 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
465 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
466 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400467 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700468 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700469 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700470 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700471 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700472};
473
Elliott Hughesd369bb72011-09-12 14:41:14 -0700474void register_java_lang_Class(JNIEnv* env) {
475 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
476}
477
478} // namespace art