blob: 43573b573b4efe99b827fc9d8d8f4c84c653a6f7 [file] [log] [blame]
Elliott Hughesd369bb72011-09-12 14:41:14 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "jni_internal.h"
18#include "class_linker.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070019#include "class_loader.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070020#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Elliott Hughes80609252011-09-23 17:24:51 -070022#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070023#include "ScopedUtfChars.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070024
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26
27namespace art {
28
Elliott Hughes15216932012-03-21 21:53:06 -070029static Class* DecodeInitializedClass(JNIEnv* env, jclass java_class) {
30 Class* c = Decode<Class*>(env, java_class);
31 DCHECK(c != NULL);
32 DCHECK(c->IsClass());
33 bool initialized = Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true);
34 return initialized ? c : NULL;
35}
36
Brian Carlstromf91c8c32011-09-21 17:30:34 -070037// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070038static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070039 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040 ScopedUtfChars name(env, javaName);
41 if (name.c_str() == NULL) {
42 return NULL;
43 }
44
45 // We need to validate and convert the name (from x.y.z to x/y/z). This
46 // is especially handy for array types, since we want to avoid
47 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070048 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070049 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070050 "Invalid name: %s", name.c_str());
51 return NULL;
52 }
53
54 std::string descriptor(DotToDescriptor(name.c_str()));
55 Object* loader = Decode<Object*>(env, javaLoader);
56 ClassLoader* class_loader = down_cast<ClassLoader*>(loader);
57 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
58 Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070059 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080060 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070061 env->ExceptionClear();
Elliott Hughes844f9a02012-01-24 20:19:58 -080062 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
63 static jmethodID ctor = env->GetMethodID(ClassNotFoundException_class, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
64 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(ClassNotFoundException_class, ctor, javaName, cause.get()));
65 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070066 return NULL;
67 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070068 if (initialize) {
69 class_linker->EnsureInitialized(c, true);
70 }
71 return AddLocalReference<jclass>(env, c);
72}
73
Elliott Hughes0512f022012-03-15 22:10:52 -070074static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080075 Class* c = Decode<Class*>(env, javaClass);
76 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
77 return 0; // primitive, array and proxy classes don't have class definitions
78 }
79 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
80 if (class_def == NULL) {
81 return 0; // not found
82 } else {
83 return class_def->annotations_off_;
84 }
85}
86
Elliott Hughes80609252011-09-23 17:24:51 -070087template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -070088static jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) {
Elliott Hughes80609252011-09-23 17:24:51 -070089 jclass array_class = env->FindClass(array_class_name);
90 jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL);
91 for (size_t i = 0; i < objects.size(); ++i) {
92 ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i]));
93 env->SetObjectArrayElement(result, i, object.get());
94 }
95 return result;
96}
97
Ian Rogersd418eda2012-01-30 12:14:28 -080098static bool IsVisibleConstructor(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -070099 if (public_only && !m->IsPublic()) {
100 return false;
101 }
Ian Rogers9074b992011-10-26 17:41:55 -0700102 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700103 return false;
104 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -0700106}
107
Elliott Hughes0512f022012-03-15 22:10:52 -0700108static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes15216932012-03-21 21:53:06 -0700109 Class* c = DecodeInitializedClass(env, javaClass);
110 if (c == NULL) {
111 return NULL;
112 }
Elliott Hughes80609252011-09-23 17:24:51 -0700113
114 std::vector<Method*> constructors;
115 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
116 Method* m = c->GetDirectMethod(i);
117 if (IsVisibleConstructor(m, publicOnly)) {
118 constructors.push_back(m);
119 }
120 }
121
122 return ToArray(env, "java/lang/reflect/Constructor", constructors);
123}
124
Ian Rogersd418eda2012-01-30 12:14:28 -0800125static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700126 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700127 return false;
128 }
Elliott Hughes80609252011-09-23 17:24:51 -0700129 return true;
130}
131
Elliott Hughes0512f022012-03-15 22:10:52 -0700132static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes15216932012-03-21 21:53:06 -0700133 Class* c = DecodeInitializedClass(env, javaClass);
134 if (c == NULL) {
135 return NULL;
136 }
Elliott Hughes80609252011-09-23 17:24:51 -0700137
138 std::vector<Field*> fields;
139 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
140 Field* f = c->GetInstanceField(i);
141 if (IsVisibleField(f, publicOnly)) {
142 fields.push_back(f);
143 }
Jesse Wilson53494312011-11-29 16:43:09 -0500144 if (env->ExceptionOccurred()) {
145 return NULL;
146 }
Elliott Hughes80609252011-09-23 17:24:51 -0700147 }
148 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
149 Field* f = c->GetStaticField(i);
150 if (IsVisibleField(f, publicOnly)) {
151 fields.push_back(f);
152 }
Jesse Wilson53494312011-11-29 16:43:09 -0500153 if (env->ExceptionOccurred()) {
154 return NULL;
155 }
Elliott Hughes80609252011-09-23 17:24:51 -0700156 }
157
158 return ToArray(env, "java/lang/reflect/Field", fields);
159}
160
Ian Rogersd418eda2012-01-30 12:14:28 -0800161static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700162 if (public_only && !m->IsPublic()) {
163 return false;
164 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800165 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700166 return false;
167 }
Ian Rogers56437422012-02-06 21:46:00 -0800168 if (m->IsMiranda()) {
169 return false;
170 }
Elliott Hughes80609252011-09-23 17:24:51 -0700171 return true;
172}
173
Elliott Hughes0512f022012-03-15 22:10:52 -0700174static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes15216932012-03-21 21:53:06 -0700175 Class* c = DecodeInitializedClass(env, javaClass);
176 if (c == NULL) {
177 return NULL;
178 }
179
Elliott Hughes80609252011-09-23 17:24:51 -0700180 std::vector<Method*> methods;
181 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
182 Method* m = c->GetVirtualMethod(i);
183 if (IsVisibleMethod(m, publicOnly)) {
184 methods.push_back(m);
185 }
Jesse Wilson53494312011-11-29 16:43:09 -0500186 if (env->ExceptionOccurred()) {
187 return NULL;
188 }
Elliott Hughes80609252011-09-23 17:24:51 -0700189 }
190 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
191 Method* m = c->GetDirectMethod(i);
192 if (IsVisibleMethod(m, publicOnly)) {
193 methods.push_back(m);
194 }
Jesse Wilson53494312011-11-29 16:43:09 -0500195 if (env->ExceptionOccurred()) {
196 return NULL;
197 }
Elliott Hughes80609252011-09-23 17:24:51 -0700198 }
199
200 return ToArray(env, "java/lang/reflect/Method", methods);
201}
202
Elliott Hughes0512f022012-03-15 22:10:52 -0700203static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Jesse Wilson6bf19152011-09-29 13:12:33 -0400204 Class* c = Decode<Class*>(env, javaClass);
205
206 DexCache* dex_cache = c->GetDexCache();
207 if (dex_cache == NULL) {
208 return NULL;
209 }
210
211 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
212}
213
Ian Rogersd418eda2012-01-30 12:14:28 -0800214static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800215 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 return false;
217 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
219 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
220 uint32_t sig_length = arg_array->GetLength();
221
222 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 return false;
224 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225
226 for (uint32_t i = 0; i < sig_length; i++) {
227 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
228 return false;
229 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500230 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 return true;
232}
233
Ian Rogersd418eda2012-01-30 12:14:28 -0800234static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Elliott Hughes15216932012-03-21 21:53:06 -0700235 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500236 if (methods == NULL) {
237 return NULL;
238 }
239 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500241 for (int32_t i = 0; i < methods->GetLength(); ++i) {
242 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 mh.ChangeMethod(method);
244 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500245 continue;
246 }
247
248 result = method;
249
250 // Covariant return types permit the class to define multiple
251 // methods with the same name and parameter types. Prefer to return
252 // a non-synthetic method in such situations. We may still return
253 // a synthetic method to handle situations like escalated visibility.
254 if (!method->IsSynthetic()) {
255 break;
256 }
257 }
258 return result;
259}
260
Elliott Hughes0512f022012-03-15 22:10:52 -0700261static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
262 jobjectArray javaArgs) {
Elliott Hughes15216932012-03-21 21:53:06 -0700263 Class* c = DecodeInitializedClass(env, javaClass);
264 if (c == NULL) {
265 return NULL;
266 }
267
Elliott Hughes95572412011-12-13 18:14:20 -0800268 std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700270
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500272 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700274 }
275
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500276 if (m != NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500277 return AddLocalReference<jobject>(env, m);
278 } else {
279 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700280 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700281}
282
Elliott Hughes15216932012-03-21 21:53:06 -0700283static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
284 Class* c = DecodeInitializedClass(env, java_class);
285 if (c == NULL) {
286 return NULL;
287 }
288
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700289 String* name = Decode<String*>(env, jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800290 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700291
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700293 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
294 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 fh.ChangeField(f);
296 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700297 return AddLocalReference<jclass>(env, f);
298 }
299 }
Elliott Hughes15216932012-03-21 21:53:06 -0700300 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
301 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800302 fh.ChangeField(f);
303 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700304 return AddLocalReference<jclass>(env, f);
305 }
306 }
307 return NULL;
308}
309
Elliott Hughes0512f022012-03-15 22:10:52 -0700310static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogersf45b1542012-02-03 18:03:48 -0800311 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700312 Class* c = Decode<Class*>(env, javaThis);
Ian Rogersd418eda2012-01-30 12:14:28 -0800313 return AddLocalReference<jstring>(env, c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700314}
315
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700316static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
317 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
318 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(Decode<Class*>(env, javaThis));
319 return AddLocalReference<jobjectArray>(env, c->GetInterfaces()->Clone());
320}
321
Elliott Hughes0512f022012-03-15 22:10:52 -0700322static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700323 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughesdd8df692011-09-23 14:42:41 -0700324 Class* lhs = Decode<Class*>(env, javaLhs);
325 Class* rhs = Decode<Class*>(env, javaRhs);
326 if (rhs == NULL) {
327 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
328 return JNI_FALSE;
329 }
330 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
331}
332
Elliott Hughes0512f022012-03-15 22:10:52 -0700333static jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) {
Elliott Hughesdd8df692011-09-23 14:42:41 -0700334 Class* c = Decode<Class*>(env, javaClass);
335 Object* o = Decode<Object*>(env, javaObject);
336 if (o == NULL) {
337 return JNI_FALSE;
338 }
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700339 return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughesdd8df692011-09-23 14:42:41 -0700340}
341
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700342// Validate method/field access.
Ian Rogersd418eda2012-01-30 12:14:28 -0800343static bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700344 // quick accept for public access */
345 if (member_flags & kAccPublic) {
346 return true;
347 }
348
349 // quick accept for access from same class
350 if (access_from == access_to) {
351 return true;
352 }
353
354 // quick reject for private access from another class
355 if (member_flags & kAccPrivate) {
356 return false;
357 }
358
359 // Semi-quick test for protected access from a sub-class, which may or
360 // may not be in the same package.
361 if (member_flags & kAccProtected) {
362 if (access_from->IsSubClass(access_to)) {
363 return true;
364 }
365 }
366
367 // Allow protected and private access from other classes in the same
368 return access_from->IsInSamePackage(access_to);
369}
370
Elliott Hughes0512f022012-03-15 22:10:52 -0700371static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700372 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700373 Class* c = Decode<Class*>(env, javaThis);
374 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700375 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800376 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700377 return NULL;
378 }
379
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700380 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
381 return NULL;
382 }
383
Brian Carlstrom01a96582012-03-12 15:17:29 -0700384 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700385 if (init == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700386 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800387 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700388 return NULL;
389 }
390
391 // Verify access from the call site.
392 //
393 // First, make sure the method invoking Class.newInstance() has permission
394 // to access the class.
395 //
396 // Second, make sure it has permission to invoke the constructor. The
397 // constructor must be public or, if the caller is in the same package,
398 // have package scope.
399 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
400 Frame frame = Thread::Current()->GetTopOfStack();
401 frame.Next();
402 frame.Next();
403 Method* caller_caller = frame.GetMethod();
404 Class* caller_class = caller_caller->GetDeclaringClass();
405
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700407 if (!caller_class->CanAccess(c)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700408 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
409 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
411 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700412 return NULL;
413 }
414 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700415 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
416 "%s is not accessible from class %s",
417 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700419 return NULL;
420 }
421
422 Object* new_obj = c->AllocObject();
423 if (new_obj == NULL) {
424 DCHECK(Thread::Current()->IsExceptionPending());
425 return NULL;
426 }
427
428 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Elliott Hughes15216932012-03-21 21:53:06 -0700429 jclass java_class = AddLocalReference<jclass>(env, c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700430 jmethodID mid = EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700431 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700432}
433
Elliott Hughesd369bb72011-09-12 14:41:14 -0700434static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700435 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800436 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
438 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
439 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
440 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
441 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400442 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700443 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700444 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700445 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
446 NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700447 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700448};
449
Elliott Hughesd369bb72011-09-12 14:41:14 -0700450void register_java_lang_Class(JNIEnv* env) {
451 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
452}
453
454} // namespace art