blob: c023b7e2daab3b077ea701759332b46357f56a08 [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
Elliott Hughesd369bb72011-09-12 14:41:14 -070017#include "class_linker.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070018#include "class_loader.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070019#include "jni_internal.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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Elliott Hughes80609252011-09-23 17:24:51 -070024#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070025#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "well_known_classes.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070027
28namespace art {
29
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030static Class* DecodeClass(const ScopedObjectAccess& soa, jobject java_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070031 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes15216932012-03-21 21:53:06 -070033 DCHECK(c != NULL);
34 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070035 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
36 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
37 // every time probably doesn't make much difference to reflection performance anyway.
38 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070039}
40
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070042static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 ScopedObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044 ScopedUtfChars name(env, javaName);
45 if (name.c_str() == NULL) {
46 return NULL;
47 }
48
49 // We need to validate and convert the name (from x.y.z to x/y/z). This
50 // is especially handy for array types, since we want to avoid
51 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070052 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070053 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070054 "Invalid name: %s", name.c_str());
55 return NULL;
56 }
57
58 std::string descriptor(DotToDescriptor(name.c_str()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 ClassLoader* class_loader = soa.Decode<ClassLoader*>(javaLoader);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060 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 Hugheseac76672012-05-24 21:56:51 -070065 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
66 WellKnownClasses::java_lang_ClassNotFoundException_init,
67 javaName, cause.get()));
Elliott Hughes844f9a02012-01-24 20:19:58 -080068 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 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 return soa.AddLocalReference<jclass>(c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075}
76
Elliott Hughes0512f022012-03-15 22:10:52 -070077static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 ScopedObjectAccess soa(env);
79 Class* c = DecodeClass(soa, javaClass);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
81 return 0; // primitive, array and proxy classes don't have class definitions
82 }
83 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
84 if (class_def == NULL) {
85 return 0; // not found
86 } else {
87 return class_def->annotations_off_;
88 }
89}
90
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091// TODO: Remove this redundant struct when GCC annotalysis works correctly on top-level functions.
92struct WorkAroundGccAnnotalysisBug {
Elliott Hughes80609252011-09-23 17:24:51 -070093template<typename T>
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094static jobjectArray ToArray(const ScopedObjectAccessUnchecked& soa, const char* array_class_name,
95 const std::vector<T*>& objects)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 ScopedLocalRef<jclass> array_class(soa.Env(), soa.Env()->FindClass(array_class_name));
98 jobjectArray result = soa.Env()->NewObjectArray(objects.size(), array_class.get(), NULL);
Elliott Hughes80609252011-09-23 17:24:51 -070099 for (size_t i = 0; i < objects.size(); ++i) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 ScopedLocalRef<jobject> object(soa.Env(), soa.AddLocalReference<jobject>(objects[i]));
101 soa.Env()->SetObjectArrayElement(result, i, object.get());
Elliott Hughes80609252011-09-23 17:24:51 -0700102 }
103 return result;
104}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105};
106#define ToArray(a, b, c) WorkAroundGccAnnotalysisBug::ToArray(a, b, c)
Elliott Hughes80609252011-09-23 17:24:51 -0700107
Mathieu Chartier66f19252012-09-18 08:57:04 -0700108static bool IsVisibleConstructor(AbstractMethod* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700109 if (public_only && !m->IsPublic()) {
110 return false;
111 }
Ian Rogers9074b992011-10-26 17:41:55 -0700112 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700113 return false;
114 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800115 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -0700116}
117
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 ScopedObjectAccess soa(env);
120 Class* c = DecodeClass(soa, javaClass);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700121 std::vector<AbstractMethod*> constructors; // TODO: Use Constructor instead of AbstractMethod
Elliott Hughes80609252011-09-23 17:24:51 -0700122 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700123 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes80609252011-09-23 17:24:51 -0700124 if (IsVisibleConstructor(m, publicOnly)) {
125 constructors.push_back(m);
126 }
127 }
128
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 return ToArray(soa, "java/lang/reflect/Constructor", constructors);
Elliott Hughes80609252011-09-23 17:24:51 -0700130}
131
Ian Rogersd418eda2012-01-30 12:14:28 -0800132static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700133 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700134 return false;
135 }
Elliott Hughes80609252011-09-23 17:24:51 -0700136 return true;
137}
138
Elliott Hughes0512f022012-03-15 22:10:52 -0700139static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 ScopedObjectAccess soa(env);
141 Class* c = DecodeClass(soa, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700142 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 return ToArray(soa, "java/lang/reflect/Field", fields);
Elliott Hughes80609252011-09-23 17:24:51 -0700174}
175
Mathieu Chartier66f19252012-09-18 08:57:04 -0700176static bool IsVisibleMethod(AbstractMethod* 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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 ScopedObjectAccess soa(env);
191 Class* c = DecodeClass(soa, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700192 if (c == NULL) {
193 return NULL;
194 }
195
Mathieu Chartier66f19252012-09-18 08:57:04 -0700196 std::vector<AbstractMethod*> 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) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700199 AbstractMethod* 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) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700213 AbstractMethod* 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 return ToArray(soa, "java/lang/reflect/Method", methods);
Elliott Hughes80609252011-09-23 17:24:51 -0700228}
229
Elliott Hughes0512f022012-03-15 22:10:52 -0700230static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 ScopedObjectAccess soa(env);
232 Class* c = DecodeClass(soa, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400233
234 DexCache* dex_cache = c->GetDexCache();
235 if (dex_cache == NULL) {
236 return NULL;
237 }
238
239 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
240}
241
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 return false;
246 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
248 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
249 uint32_t sig_length = arg_array->GetLength();
250
251 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700252 return false;
253 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800254
255 for (uint32_t i = 0; i < sig_length; i++) {
256 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
257 return false;
258 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500259 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700260 return true;
261}
262
Mathieu Chartier66f19252012-09-18 08:57:04 -0700263static AbstractMethod* FindConstructorOrMethodInArray(ObjectArray<AbstractMethod>* methods,
264 const std::string& name,
265 ObjectArray<Class>* arg_array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500267 if (methods == NULL) {
268 return NULL;
269 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700270 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500272 for (int32_t i = 0; i < methods->GetLength(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700273 AbstractMethod* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 mh.ChangeMethod(method);
275 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500276 continue;
277 }
278
279 result = method;
280
281 // Covariant return types permit the class to define multiple
282 // methods with the same name and parameter types. Prefer to return
283 // a non-synthetic method in such situations. We may still return
284 // a synthetic method to handle situations like escalated visibility.
285 if (!method->IsSynthetic()) {
286 break;
287 }
288 }
289 return result;
290}
291
Elliott Hughes0512f022012-03-15 22:10:52 -0700292static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
293 jobjectArray javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 ScopedObjectAccess soa(env);
295 Class* c = DecodeClass(soa, javaClass);
296 std::string name(soa.Decode<String*>(javaName)->ToModifiedUtf8());
297 ObjectArray<Class>* arg_array = soa.Decode<ObjectArray<Class>*>(javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700298
Mathieu Chartier66f19252012-09-18 08:57:04 -0700299 AbstractMethod* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500300 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800301 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700302 }
303
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500304 if (m != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 return soa.AddLocalReference<jobject>(m);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500306 } else {
307 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700308 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700309}
310
Elliott Hughes15216932012-03-21 21:53:06 -0700311static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700312 ScopedObjectAccess soa(env);
313 Class* c = DecodeClass(soa, java_class);
314 String* name = soa.Decode<String*>(jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800315 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700316
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700318 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
319 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800320 fh.ChangeField(f);
321 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700322 if (fh.GetType() == NULL) {
323 DCHECK(env->ExceptionOccurred());
324 return NULL;
325 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 return soa.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700327 }
328 }
Elliott Hughes15216932012-03-21 21:53:06 -0700329 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
330 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800331 fh.ChangeField(f);
332 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700333 if (fh.GetType() == NULL) {
334 DCHECK(env->ExceptionOccurred());
335 return NULL;
336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 return soa.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700338 }
339 }
340 return NULL;
341}
342
Elliott Hughes0512f022012-03-15 22:10:52 -0700343static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 ScopedObjectAccess soa(env);
345 Class* c = DecodeClass(soa, javaThis);
346 return soa.AddLocalReference<jstring>(c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700347}
348
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700349static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 ScopedObjectAccess soa(env);
351 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
352 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700353}
354
Elliott Hughes0512f022012-03-15 22:10:52 -0700355static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 ScopedObjectAccess soa(env);
357 Class* lhs = DecodeClass(soa, javaLhs);
358 Class* rhs = soa.Decode<Class*>(javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700359 if (rhs == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
Elliott Hughesdd8df692011-09-23 14:42:41 -0700361 return JNI_FALSE;
362 }
363 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
364}
365
Elliott Hughes0512f022012-03-15 22:10:52 -0700366static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367 ScopedObjectAccess soa(env);
368 Class* c = DecodeClass(soa, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700369 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700372 return NULL;
373 }
374
Ian Rogers0045a292012-03-31 21:08:41 -0700375 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700376 return NULL;
377 }
378
Mathieu Chartier66f19252012-09-18 08:57:04 -0700379 AbstractMethod* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700380 if (init == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700383 return NULL;
384 }
385
386 // Verify access from the call site.
387 //
388 // First, make sure the method invoking Class.newInstance() has permission
389 // to access the class.
390 //
391 // Second, make sure it has permission to invoke the constructor. The
392 // constructor must be public or, if the caller is in the same package,
393 // have package scope.
Elliott Hughes6a144332012-04-03 13:07:11 -0700394
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetTraceStack(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -0700396 visitor.WalkStack();
TDYa1273f9137d2012-04-08 15:59:19 -0700397 Class* caller_class = visitor.caller->GetDeclaringClass();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700398
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800399 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700400 if (!caller_class->CanAccess(c)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700402 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
404 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700405 return NULL;
406 }
Elliott Hughes26c5e152012-07-11 11:47:22 -0700407 if (!caller_class->CanAccessMember(init->GetDeclaringClass(), init->GetAccessFlags())) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700409 "%s is not accessible from class %s",
410 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800411 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700412 return NULL;
413 }
414
415 Object* new_obj = c->AllocObject();
416 if (new_obj == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700417 DCHECK(soa.Self()->IsExceptionPending());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700418 return NULL;
419 }
420
421 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 jclass java_class = soa.AddLocalReference<jclass>(c);
423 jmethodID mid = soa.EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700424 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700425}
426
Elliott Hughesd369bb72011-09-12 14:41:14 -0700427static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700428 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
431 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
432 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
433 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
434 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400435 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700436 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700437 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700438 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700439 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700440};
441
Elliott Hughesd369bb72011-09-12 14:41:14 -0700442void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700443 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700444}
445
446} // namespace art