blob: 488df80e6503e41612692b753190896778f3ba9d [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)) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700202 // TODO: the use of GetParameterTypes creates an unused array here.
203 if (mh.GetReturnType() == NULL || mh.GetParameterTypes(soa.Self()) == NULL) {
jeffhao441d9122012-03-21 17:29:10 -0700204 DCHECK(env->ExceptionOccurred());
205 return NULL;
206 }
Elliott Hughes80609252011-09-23 17:24:51 -0700207 methods.push_back(m);
208 }
Jesse Wilson53494312011-11-29 16:43:09 -0500209 if (env->ExceptionOccurred()) {
210 return NULL;
211 }
Elliott Hughes80609252011-09-23 17:24:51 -0700212 }
213 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700214 AbstractMethod* m = c->GetDirectMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700215 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700216 if (IsVisibleMethod(m, publicOnly)) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700217 // TODO: the use of GetParameterTypes creates an unused array here.
218 if (mh.GetReturnType() == NULL || mh.GetParameterTypes(soa.Self()) == NULL) {
jeffhao441d9122012-03-21 17:29:10 -0700219 DCHECK(env->ExceptionOccurred());
220 return NULL;
221 }
Elliott Hughes80609252011-09-23 17:24:51 -0700222 methods.push_back(m);
223 }
Jesse Wilson53494312011-11-29 16:43:09 -0500224 if (env->ExceptionOccurred()) {
225 return NULL;
226 }
Elliott Hughes80609252011-09-23 17:24:51 -0700227 }
228
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 return ToArray(soa, "java/lang/reflect/Method", methods);
Elliott Hughes80609252011-09-23 17:24:51 -0700230}
231
Elliott Hughes0512f022012-03-15 22:10:52 -0700232static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 ScopedObjectAccess soa(env);
234 Class* c = DecodeClass(soa, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400235
236 DexCache* dex_cache = c->GetDexCache();
237 if (dex_cache == NULL) {
238 return NULL;
239 }
240
241 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
242}
243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800246 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 return false;
248 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
250 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
251 uint32_t sig_length = arg_array->GetLength();
252
253 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700254 return false;
255 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800256
257 for (uint32_t i = 0; i < sig_length; i++) {
258 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
259 return false;
260 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500261 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700262 return true;
263}
264
Mathieu Chartier66f19252012-09-18 08:57:04 -0700265static AbstractMethod* FindConstructorOrMethodInArray(ObjectArray<AbstractMethod>* methods,
266 const std::string& name,
267 ObjectArray<Class>* arg_array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500269 if (methods == NULL) {
270 return NULL;
271 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700272 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500274 for (int32_t i = 0; i < methods->GetLength(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700275 AbstractMethod* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 mh.ChangeMethod(method);
277 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500278 continue;
279 }
280
281 result = method;
282
283 // Covariant return types permit the class to define multiple
284 // methods with the same name and parameter types. Prefer to return
285 // a non-synthetic method in such situations. We may still return
286 // a synthetic method to handle situations like escalated visibility.
287 if (!method->IsSynthetic()) {
288 break;
289 }
290 }
291 return result;
292}
293
Elliott Hughes0512f022012-03-15 22:10:52 -0700294static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
295 jobjectArray javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 ScopedObjectAccess soa(env);
297 Class* c = DecodeClass(soa, javaClass);
298 std::string name(soa.Decode<String*>(javaName)->ToModifiedUtf8());
299 ObjectArray<Class>* arg_array = soa.Decode<ObjectArray<Class>*>(javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700300
Mathieu Chartier66f19252012-09-18 08:57:04 -0700301 AbstractMethod* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500302 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700304 }
305
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500306 if (m != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 return soa.AddLocalReference<jobject>(m);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500308 } else {
309 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700310 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700311}
312
Elliott Hughes15216932012-03-21 21:53:06 -0700313static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 ScopedObjectAccess soa(env);
315 Class* c = DecodeClass(soa, java_class);
316 String* name = soa.Decode<String*>(jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800317 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700318
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700320 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
321 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 fh.ChangeField(f);
323 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700324 if (fh.GetType() == NULL) {
325 DCHECK(env->ExceptionOccurred());
326 return NULL;
327 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328 return soa.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700329 }
330 }
Elliott Hughes15216932012-03-21 21:53:06 -0700331 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
332 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800333 fh.ChangeField(f);
334 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700335 if (fh.GetType() == NULL) {
336 DCHECK(env->ExceptionOccurred());
337 return NULL;
338 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 return soa.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700340 }
341 }
342 return NULL;
343}
344
Elliott Hughes0512f022012-03-15 22:10:52 -0700345static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 ScopedObjectAccess soa(env);
347 Class* c = DecodeClass(soa, javaThis);
348 return soa.AddLocalReference<jstring>(c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700349}
350
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700351static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 ScopedObjectAccess soa(env);
353 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
Ian Rogers50b35e22012-10-04 10:09:15 -0700354 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700355}
356
Elliott Hughes0512f022012-03-15 22:10:52 -0700357static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 ScopedObjectAccess soa(env);
359 Class* lhs = DecodeClass(soa, javaLhs);
360 Class* rhs = soa.Decode<Class*>(javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700361 if (rhs == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
Elliott Hughesdd8df692011-09-23 14:42:41 -0700363 return JNI_FALSE;
364 }
365 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
366}
367
Elliott Hughes0512f022012-03-15 22:10:52 -0700368static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 ScopedObjectAccess soa(env);
370 Class* c = DecodeClass(soa, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700371 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700374 return NULL;
375 }
376
Ian Rogers0045a292012-03-31 21:08:41 -0700377 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700378 return NULL;
379 }
380
Mathieu Chartier66f19252012-09-18 08:57:04 -0700381 AbstractMethod* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700382 if (init == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800384 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700385 return NULL;
386 }
387
388 // Verify access from the call site.
389 //
390 // First, make sure the method invoking Class.newInstance() has permission
391 // to access the class.
392 //
393 // Second, make sure it has permission to invoke the constructor. The
394 // constructor must be public or, if the caller is in the same package,
395 // have package scope.
Elliott Hughes6a144332012-04-03 13:07:11 -0700396
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700397 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetTraceStack(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -0700398 visitor.WalkStack();
TDYa1273f9137d2012-04-08 15:59:19 -0700399 Class* caller_class = visitor.caller->GetDeclaringClass();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700400
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700402 if (!caller_class->CanAccess(c)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700403 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700404 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800405 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
406 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700407 return NULL;
408 }
Elliott Hughes26c5e152012-07-11 11:47:22 -0700409 if (!caller_class->CanAccessMember(init->GetDeclaringClass(), init->GetAccessFlags())) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700411 "%s is not accessible from class %s",
412 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800413 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700414 return NULL;
415 }
416
Ian Rogers50b35e22012-10-04 10:09:15 -0700417 Object* new_obj = c->AllocObject(soa.Self());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700418 if (new_obj == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700419 DCHECK(soa.Self()->IsExceptionPending());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700420 return NULL;
421 }
422
423 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424 jclass java_class = soa.AddLocalReference<jclass>(c);
425 jmethodID mid = soa.EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700426 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700427}
428
Elliott Hughesd369bb72011-09-12 14:41:14 -0700429static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700430 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800431 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800432 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
433 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
434 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
435 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
436 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400437 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700438 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700439 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700440 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700441 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700442};
443
Elliott Hughesd369bb72011-09-12 14:41:14 -0700444void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700445 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700446}
447
448} // namespace art