blob: bc1d0def39adf38c23820c2a23d224e92b18bbc1 [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 Rogers0399dde2012-06-06 17:09:28 -070023#include "scoped_jni_thread_state.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 Rogers365c1022012-06-22 15:05:28 -070030static Class* DecodeClass(const ScopedJniThreadState& ts, jobject java_class) {
31 Class* c = ts.Decode<Class*>(java_class);
Elliott Hughes15216932012-03-21 21:53:06 -070032 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) {
Ian Rogers365c1022012-06-22 15:05:28 -070042 ScopedJniThreadState ts(env);
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()));
Ian Rogers365c1022012-06-22 15:05:28 -070058 ClassLoader* class_loader = ts.Decode<ClassLoader*>(javaLoader);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070059 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 Hugheseac76672012-05-24 21:56:51 -070064 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
65 WellKnownClasses::java_lang_ClassNotFoundException_init,
66 javaName, cause.get()));
Elliott Hughes844f9a02012-01-24 20:19:58 -080067 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070068 return NULL;
69 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070070 if (initialize) {
Ian Rogers0045a292012-03-31 21:08:41 -070071 class_linker->EnsureInitialized(c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070072 }
Ian Rogers365c1022012-06-22 15:05:28 -070073 return ts.AddLocalReference<jclass>(c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070074}
75
Elliott Hughes0512f022012-03-15 22:10:52 -070076static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Ian Rogers365c1022012-06-22 15:05:28 -070077 ScopedJniThreadState ts(env);
78 Class* c = DecodeClass(ts, 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>
Ian Rogers365c1022012-06-22 15:05:28 -070091static jobjectArray ToArray(const ScopedJniThreadState& ts, const char* array_class_name,
92 const std::vector<T*>& objects) {
93 ScopedLocalRef<jclass> array_class(ts.Env(), ts.Env()->FindClass(array_class_name));
94 jobjectArray result = ts.Env()->NewObjectArray(objects.size(), array_class.get(), NULL);
Elliott Hughes80609252011-09-23 17:24:51 -070095 for (size_t i = 0; i < objects.size(); ++i) {
Ian Rogers365c1022012-06-22 15:05:28 -070096 ScopedLocalRef<jobject> object(ts.Env(), ts.AddLocalReference<jobject>(objects[i]));
97 ts.Env()->SetObjectArrayElement(result, i, object.get());
Elliott Hughes80609252011-09-23 17:24:51 -070098 }
99 return result;
100}
101
Ian Rogersd418eda2012-01-30 12:14:28 -0800102static bool IsVisibleConstructor(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700103 if (public_only && !m->IsPublic()) {
104 return false;
105 }
Ian Rogers9074b992011-10-26 17:41:55 -0700106 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700107 return false;
108 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800109 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -0700110}
111
Elliott Hughes0512f022012-03-15 22:10:52 -0700112static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers365c1022012-06-22 15:05:28 -0700113 ScopedJniThreadState ts(env);
114 Class* c = DecodeClass(ts, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700115 std::vector<Method*> constructors;
116 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
117 Method* m = c->GetDirectMethod(i);
118 if (IsVisibleConstructor(m, publicOnly)) {
119 constructors.push_back(m);
120 }
121 }
122
Ian Rogers365c1022012-06-22 15:05:28 -0700123 return ToArray(ts, "java/lang/reflect/Constructor", constructors);
Elliott Hughes80609252011-09-23 17:24:51 -0700124}
125
Ian Rogersd418eda2012-01-30 12:14:28 -0800126static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700127 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700128 return false;
129 }
Elliott Hughes80609252011-09-23 17:24:51 -0700130 return true;
131}
132
Elliott Hughes0512f022012-03-15 22:10:52 -0700133static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers365c1022012-06-22 15:05:28 -0700134 ScopedJniThreadState ts(env);
135 Class* c = DecodeClass(ts, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700136 std::vector<Field*> fields;
jeffhao441d9122012-03-21 17:29:10 -0700137 FieldHelper fh;
Elliott Hughes80609252011-09-23 17:24:51 -0700138 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
139 Field* f = c->GetInstanceField(i);
jeffhao441d9122012-03-21 17:29:10 -0700140 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700141 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700142 if (fh.GetType() == NULL) {
143 DCHECK(env->ExceptionOccurred());
144 return NULL;
145 }
Elliott Hughes80609252011-09-23 17:24:51 -0700146 fields.push_back(f);
147 }
Jesse Wilson53494312011-11-29 16:43:09 -0500148 if (env->ExceptionOccurred()) {
149 return NULL;
150 }
Elliott Hughes80609252011-09-23 17:24:51 -0700151 }
152 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
153 Field* f = c->GetStaticField(i);
jeffhao441d9122012-03-21 17:29:10 -0700154 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700155 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700156 if (fh.GetType() == NULL) {
157 DCHECK(env->ExceptionOccurred());
158 return NULL;
159 }
Elliott Hughes80609252011-09-23 17:24:51 -0700160 fields.push_back(f);
161 }
Jesse Wilson53494312011-11-29 16:43:09 -0500162 if (env->ExceptionOccurred()) {
163 return NULL;
164 }
Elliott Hughes80609252011-09-23 17:24:51 -0700165 }
166
Ian Rogers365c1022012-06-22 15:05:28 -0700167 return ToArray(ts, "java/lang/reflect/Field", fields);
Elliott Hughes80609252011-09-23 17:24:51 -0700168}
169
Ian Rogersd418eda2012-01-30 12:14:28 -0800170static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700171 if (public_only && !m->IsPublic()) {
172 return false;
173 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700175 return false;
176 }
Ian Rogers56437422012-02-06 21:46:00 -0800177 if (m->IsMiranda()) {
178 return false;
179 }
Elliott Hughes80609252011-09-23 17:24:51 -0700180 return true;
181}
182
Elliott Hughes0512f022012-03-15 22:10:52 -0700183static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers365c1022012-06-22 15:05:28 -0700184 ScopedJniThreadState ts(env);
185 Class* c = DecodeClass(ts, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700186 if (c == NULL) {
187 return NULL;
188 }
189
Elliott Hughes80609252011-09-23 17:24:51 -0700190 std::vector<Method*> methods;
jeffhao441d9122012-03-21 17:29:10 -0700191 MethodHelper mh;
Elliott Hughes80609252011-09-23 17:24:51 -0700192 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
193 Method* m = c->GetVirtualMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700194 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700195 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700196 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
197 DCHECK(env->ExceptionOccurred());
198 return NULL;
199 }
Elliott Hughes80609252011-09-23 17:24:51 -0700200 methods.push_back(m);
201 }
Jesse Wilson53494312011-11-29 16:43:09 -0500202 if (env->ExceptionOccurred()) {
203 return NULL;
204 }
Elliott Hughes80609252011-09-23 17:24:51 -0700205 }
206 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
207 Method* m = c->GetDirectMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700208 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700209 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700210 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
211 DCHECK(env->ExceptionOccurred());
212 return NULL;
213 }
Elliott Hughes80609252011-09-23 17:24:51 -0700214 methods.push_back(m);
215 }
Jesse Wilson53494312011-11-29 16:43:09 -0500216 if (env->ExceptionOccurred()) {
217 return NULL;
218 }
Elliott Hughes80609252011-09-23 17:24:51 -0700219 }
220
Ian Rogers365c1022012-06-22 15:05:28 -0700221 return ToArray(ts, "java/lang/reflect/Method", methods);
Elliott Hughes80609252011-09-23 17:24:51 -0700222}
223
Elliott Hughes0512f022012-03-15 22:10:52 -0700224static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Ian Rogers365c1022012-06-22 15:05:28 -0700225 ScopedJniThreadState ts(env);
226 Class* c = DecodeClass(ts, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400227
228 DexCache* dex_cache = c->GetDexCache();
229 if (dex_cache == NULL) {
230 return NULL;
231 }
232
233 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
234}
235
Ian Rogersd418eda2012-01-30 12:14:28 -0800236static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800237 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 return false;
239 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
241 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
242 uint32_t sig_length = arg_array->GetLength();
243
244 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 return false;
246 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247
248 for (uint32_t i = 0; i < sig_length; i++) {
249 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
250 return false;
251 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500252 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 return true;
254}
255
Ian Rogersd418eda2012-01-30 12:14:28 -0800256static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Elliott Hughes15216932012-03-21 21:53:06 -0700257 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500258 if (methods == NULL) {
259 return NULL;
260 }
261 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500263 for (int32_t i = 0; i < methods->GetLength(); ++i) {
264 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 mh.ChangeMethod(method);
266 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500267 continue;
268 }
269
270 result = method;
271
272 // Covariant return types permit the class to define multiple
273 // methods with the same name and parameter types. Prefer to return
274 // a non-synthetic method in such situations. We may still return
275 // a synthetic method to handle situations like escalated visibility.
276 if (!method->IsSynthetic()) {
277 break;
278 }
279 }
280 return result;
281}
282
Elliott Hughes0512f022012-03-15 22:10:52 -0700283static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
284 jobjectArray javaArgs) {
Ian Rogers365c1022012-06-22 15:05:28 -0700285 ScopedJniThreadState ts(env);
286 Class* c = DecodeClass(ts, javaClass);
287 std::string name(ts.Decode<String*>(javaName)->ToModifiedUtf8());
288 ObjectArray<Class>* arg_array = ts.Decode<ObjectArray<Class>*>(javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700289
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800290 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500291 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700293 }
294
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500295 if (m != NULL) {
Ian Rogers365c1022012-06-22 15:05:28 -0700296 return ts.AddLocalReference<jobject>(m);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500297 } else {
298 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700299 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700300}
301
Elliott Hughes15216932012-03-21 21:53:06 -0700302static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700303 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700304 Class* c = DecodeClass(ts, java_class);
305 String* name = ts.Decode<String*>(jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800306 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700307
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800308 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700309 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
310 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800311 fh.ChangeField(f);
312 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700313 if (fh.GetType() == NULL) {
314 DCHECK(env->ExceptionOccurred());
315 return NULL;
316 }
Ian Rogers365c1022012-06-22 15:05:28 -0700317 return ts.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700318 }
319 }
Elliott Hughes15216932012-03-21 21:53:06 -0700320 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
321 Field* f = c->GetStaticField(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 Rogers365c1022012-06-22 15:05:28 -0700328 return ts.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700329 }
330 }
331 return NULL;
332}
333
Elliott Hughes0512f022012-03-15 22:10:52 -0700334static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700335 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700336 Class* c = DecodeClass(ts, javaThis);
337 return ts.AddLocalReference<jstring>(c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700338}
339
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700340static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700341 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700342 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(ts, javaThis));
343 return ts.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700344}
345
Elliott Hughes0512f022012-03-15 22:10:52 -0700346static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700347 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700348 Class* lhs = DecodeClass(ts, javaLhs);
349 Class* rhs = ts.Decode<Class*>(javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700350 if (rhs == NULL) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700351 ts.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
Elliott Hughesdd8df692011-09-23 14:42:41 -0700352 return JNI_FALSE;
353 }
354 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
355}
356
Elliott Hughes0512f022012-03-15 22:10:52 -0700357static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700358 ScopedJniThreadState ts(env);
Ian Rogers365c1022012-06-22 15:05:28 -0700359 Class* c = DecodeClass(ts, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700360 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700361 ts.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800362 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700363 return NULL;
364 }
365
Ian Rogers0045a292012-03-31 21:08:41 -0700366 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700367 return NULL;
368 }
369
Brian Carlstrom01a96582012-03-12 15:17:29 -0700370 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700371 if (init == NULL) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700372 ts.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700374 return NULL;
375 }
376
377 // Verify access from the call site.
378 //
379 // First, make sure the method invoking Class.newInstance() has permission
380 // to access the class.
381 //
382 // Second, make sure it has permission to invoke the constructor. The
383 // constructor must be public or, if the caller is in the same package,
384 // have package scope.
Elliott Hughes6a144332012-04-03 13:07:11 -0700385
Ian Rogers0399dde2012-06-06 17:09:28 -0700386 NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 2);
387 visitor.WalkStack();
TDYa1273f9137d2012-04-08 15:59:19 -0700388 Class* caller_class = visitor.caller->GetDeclaringClass();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700389
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800390 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700391 if (!caller_class->CanAccess(c)) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700392 ts.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700393 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800394 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
395 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700396 return NULL;
397 }
Elliott Hughes26c5e152012-07-11 11:47:22 -0700398 if (!caller_class->CanAccessMember(init->GetDeclaringClass(), init->GetAccessFlags())) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700399 ts.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700400 "%s is not accessible from class %s",
401 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800402 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700403 return NULL;
404 }
405
406 Object* new_obj = c->AllocObject();
407 if (new_obj == NULL) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700408 DCHECK(ts.Self()->IsExceptionPending());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700409 return NULL;
410 }
411
412 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Ian Rogers365c1022012-06-22 15:05:28 -0700413 jclass java_class = ts.AddLocalReference<jclass>(c);
414 jmethodID mid = ts.EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700415 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700416}
417
Elliott Hughesd369bb72011-09-12 14:41:14 -0700418static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700419 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800420 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
422 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
423 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
424 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
425 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400426 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700427 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700428 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700429 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700430 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700431};
432
Elliott Hughesd369bb72011-09-12 14:41:14 -0700433void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700434 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700435}
436
437} // namespace art