blob: 567a067088c36b36f688bc518cb20f71c000d156 [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
Brian Carlstromf91c8c32011-09-21 17:30:34 -070029// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070030static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070031 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070032 ScopedUtfChars name(env, javaName);
33 if (name.c_str() == NULL) {
34 return NULL;
35 }
36
37 // We need to validate and convert the name (from x.y.z to x/y/z). This
38 // is especially handy for array types, since we want to avoid
39 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070040 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070041 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042 "Invalid name: %s", name.c_str());
43 return NULL;
44 }
45
46 std::string descriptor(DotToDescriptor(name.c_str()));
47 Object* loader = Decode<Object*>(env, javaLoader);
48 ClassLoader* class_loader = down_cast<ClassLoader*>(loader);
49 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
50 Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070051 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080052 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070053 env->ExceptionClear();
Elliott Hughes844f9a02012-01-24 20:19:58 -080054 static jclass ClassNotFoundException_class = CacheClass(env, "java/lang/ClassNotFoundException");
55 static jmethodID ctor = env->GetMethodID(ClassNotFoundException_class, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
56 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(ClassNotFoundException_class, ctor, javaName, cause.get()));
57 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070058 return NULL;
59 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060 if (initialize) {
61 class_linker->EnsureInitialized(c, true);
62 }
63 return AddLocalReference<jclass>(env, c);
64}
65
Elliott Hughes0512f022012-03-15 22:10:52 -070066static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067 Class* c = Decode<Class*>(env, javaClass);
68 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
69 return 0; // primitive, array and proxy classes don't have class definitions
70 }
71 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
72 if (class_def == NULL) {
73 return 0; // not found
74 } else {
75 return class_def->annotations_off_;
76 }
77}
78
Elliott Hughes80609252011-09-23 17:24:51 -070079template<typename T>
Elliott Hughes0512f022012-03-15 22:10:52 -070080static jobjectArray ToArray(JNIEnv* env, const char* array_class_name, const std::vector<T*>& objects) {
Elliott Hughes80609252011-09-23 17:24:51 -070081 jclass array_class = env->FindClass(array_class_name);
82 jobjectArray result = env->NewObjectArray(objects.size(), array_class, NULL);
83 for (size_t i = 0; i < objects.size(); ++i) {
84 ScopedLocalRef<jobject> object(env, AddLocalReference<jobject>(env, objects[i]));
85 env->SetObjectArrayElement(result, i, object.get());
86 }
87 return result;
88}
89
Ian Rogersd418eda2012-01-30 12:14:28 -080090static bool IsVisibleConstructor(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -070091 if (public_only && !m->IsPublic()) {
92 return false;
93 }
Ian Rogers9074b992011-10-26 17:41:55 -070094 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -070095 return false;
96 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080097 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -070098}
99
Elliott Hughes0512f022012-03-15 22:10:52 -0700100static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes80609252011-09-23 17:24:51 -0700101 Class* c = Decode<Class*>(env, javaClass);
102
103 std::vector<Method*> constructors;
104 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
105 Method* m = c->GetDirectMethod(i);
106 if (IsVisibleConstructor(m, publicOnly)) {
107 constructors.push_back(m);
108 }
109 }
110
111 return ToArray(env, "java/lang/reflect/Constructor", constructors);
112}
113
Ian Rogersd418eda2012-01-30 12:14:28 -0800114static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700115 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700116 return false;
117 }
Elliott Hughes80609252011-09-23 17:24:51 -0700118 return true;
119}
120
Elliott Hughes0512f022012-03-15 22:10:52 -0700121static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes80609252011-09-23 17:24:51 -0700122 Class* c = Decode<Class*>(env, javaClass);
123
124 std::vector<Field*> fields;
125 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
126 Field* f = c->GetInstanceField(i);
127 if (IsVisibleField(f, publicOnly)) {
128 fields.push_back(f);
129 }
Jesse Wilson53494312011-11-29 16:43:09 -0500130 if (env->ExceptionOccurred()) {
131 return NULL;
132 }
Elliott Hughes80609252011-09-23 17:24:51 -0700133 }
134 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
135 Field* f = c->GetStaticField(i);
136 if (IsVisibleField(f, publicOnly)) {
137 fields.push_back(f);
138 }
Jesse Wilson53494312011-11-29 16:43:09 -0500139 if (env->ExceptionOccurred()) {
140 return NULL;
141 }
Elliott Hughes80609252011-09-23 17:24:51 -0700142 }
143
144 return ToArray(env, "java/lang/reflect/Field", fields);
145}
146
Ian Rogersd418eda2012-01-30 12:14:28 -0800147static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700148 if (public_only && !m->IsPublic()) {
149 return false;
150 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800151 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700152 return false;
153 }
Ian Rogers56437422012-02-06 21:46:00 -0800154 if (m->IsMiranda()) {
155 return false;
156 }
Elliott Hughes80609252011-09-23 17:24:51 -0700157 return true;
158}
159
Elliott Hughes0512f022012-03-15 22:10:52 -0700160static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Elliott Hughes80609252011-09-23 17:24:51 -0700161 Class* c = Decode<Class*>(env, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700162 std::vector<Method*> methods;
163 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
164 Method* m = c->GetVirtualMethod(i);
165 if (IsVisibleMethod(m, publicOnly)) {
166 methods.push_back(m);
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 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
173 Method* m = c->GetDirectMethod(i);
174 if (IsVisibleMethod(m, publicOnly)) {
175 methods.push_back(m);
176 }
Jesse Wilson53494312011-11-29 16:43:09 -0500177 if (env->ExceptionOccurred()) {
178 return NULL;
179 }
Elliott Hughes80609252011-09-23 17:24:51 -0700180 }
181
182 return ToArray(env, "java/lang/reflect/Method", methods);
183}
184
Elliott Hughes0512f022012-03-15 22:10:52 -0700185static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Jesse Wilson6bf19152011-09-29 13:12:33 -0400186 Class* c = Decode<Class*>(env, javaClass);
187
188 DexCache* dex_cache = c->GetDexCache();
189 if (dex_cache == NULL) {
190 return NULL;
191 }
192
193 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
194}
195
Ian Rogersd418eda2012-01-30 12:14:28 -0800196static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800197 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700198 return false;
199 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800200 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
201 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
202 uint32_t sig_length = arg_array->GetLength();
203
204 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700205 return false;
206 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207
208 for (uint32_t i = 0; i < sig_length; i++) {
209 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
210 return false;
211 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500212 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700213 return true;
214}
215
Ian Rogersd418eda2012-01-30 12:14:28 -0800216static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 ObjectArray<Class>* arg_array) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500218 if (methods == NULL) {
219 return NULL;
220 }
221 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500223 for (int32_t i = 0; i < methods->GetLength(); ++i) {
224 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 mh.ChangeMethod(method);
226 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500227 continue;
228 }
229
230 result = method;
231
232 // Covariant return types permit the class to define multiple
233 // methods with the same name and parameter types. Prefer to return
234 // a non-synthetic method in such situations. We may still return
235 // a synthetic method to handle situations like escalated visibility.
236 if (!method->IsSynthetic()) {
237 break;
238 }
239 }
240 return result;
241}
242
Elliott Hughes0512f022012-03-15 22:10:52 -0700243static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
244 jobjectArray javaArgs) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 Class* c = Decode<Class*>(env, javaClass);
Elliott Hughes95572412011-12-13 18:14:20 -0800246 std::string name(Decode<String*>(env, javaName)->ToModifiedUtf8());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 ObjectArray<Class>* arg_array = Decode<ObjectArray<Class>*>(env, javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700248
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500250 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800251 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700252 }
253
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500254 if (m != NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500255 return AddLocalReference<jobject>(env, m);
256 } else {
257 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700258 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700259}
260
Elliott Hughes0512f022012-03-15 22:10:52 -0700261static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass jklass, jobject jname) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700262 Class* klass = Decode<Class*>(env, jklass);
263 DCHECK(klass->IsClass());
264 String* name = Decode<String*>(env, jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800265 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700266
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 FieldHelper fh;
Elliott Hughes80609252011-09-23 17:24:51 -0700268 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700269 Field* f = klass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 fh.ChangeField(f);
271 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700272 return AddLocalReference<jclass>(env, f);
273 }
274 }
275 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
276 Field* f = klass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 fh.ChangeField(f);
278 if (name->Equals(fh.GetName())) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700279 return AddLocalReference<jclass>(env, f);
280 }
281 }
282 return NULL;
283}
284
Elliott Hughes0512f022012-03-15 22:10:52 -0700285static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogersf45b1542012-02-03 18:03:48 -0800286 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700287 Class* c = Decode<Class*>(env, javaThis);
Ian Rogersd418eda2012-01-30 12:14:28 -0800288 return AddLocalReference<jstring>(env, c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700289}
290
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700291static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
292 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
293 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(Decode<Class*>(env, javaThis));
294 return AddLocalReference<jobjectArray>(env, c->GetInterfaces()->Clone());
295}
296
Elliott Hughes0512f022012-03-15 22:10:52 -0700297static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700298 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughesdd8df692011-09-23 14:42:41 -0700299 Class* lhs = Decode<Class*>(env, javaLhs);
300 Class* rhs = Decode<Class*>(env, javaRhs);
301 if (rhs == NULL) {
302 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
303 return JNI_FALSE;
304 }
305 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
306}
307
Elliott Hughes0512f022012-03-15 22:10:52 -0700308static jboolean Class_isInstance(JNIEnv* env, jobject javaClass, jobject javaObject) {
Elliott Hughesdd8df692011-09-23 14:42:41 -0700309 Class* c = Decode<Class*>(env, javaClass);
310 Object* o = Decode<Object*>(env, javaObject);
311 if (o == NULL) {
312 return JNI_FALSE;
313 }
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700314 return o->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughesdd8df692011-09-23 14:42:41 -0700315}
316
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700317// Validate method/field access.
Ian Rogersd418eda2012-01-30 12:14:28 -0800318static bool CheckMemberAccess(const Class* access_from, Class* access_to, uint32_t member_flags) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700319 // quick accept for public access */
320 if (member_flags & kAccPublic) {
321 return true;
322 }
323
324 // quick accept for access from same class
325 if (access_from == access_to) {
326 return true;
327 }
328
329 // quick reject for private access from another class
330 if (member_flags & kAccPrivate) {
331 return false;
332 }
333
334 // Semi-quick test for protected access from a sub-class, which may or
335 // may not be in the same package.
336 if (member_flags & kAccProtected) {
337 if (access_from->IsSubClass(access_to)) {
338 return true;
339 }
340 }
341
342 // Allow protected and private access from other classes in the same
343 return access_from->IsInSamePackage(access_to);
344}
345
Elliott Hughes0512f022012-03-15 22:10:52 -0700346static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700347 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700348 Class* c = Decode<Class*>(env, javaThis);
349 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700350 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800351 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700352 return NULL;
353 }
354
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700355 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
356 return NULL;
357 }
358
Brian Carlstrom01a96582012-03-12 15:17:29 -0700359 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700360 if (init == NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700361 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800362 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700363 return NULL;
364 }
365
366 // Verify access from the call site.
367 //
368 // First, make sure the method invoking Class.newInstance() has permission
369 // to access the class.
370 //
371 // Second, make sure it has permission to invoke the constructor. The
372 // constructor must be public or, if the caller is in the same package,
373 // have package scope.
374 // TODO: need SmartFrame (Thread::WalkStack-like iterator).
375 Frame frame = Thread::Current()->GetTopOfStack();
376 frame.Next();
377 frame.Next();
378 Method* caller_caller = frame.GetMethod();
379 Class* caller_class = caller_caller->GetDeclaringClass();
380
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700382 if (!caller_class->CanAccess(c)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700383 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
384 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800385 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
386 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700387 return NULL;
388 }
389 if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700390 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
391 "%s is not accessible from class %s",
392 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800393 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700394 return NULL;
395 }
396
397 Object* new_obj = c->AllocObject();
398 if (new_obj == NULL) {
399 DCHECK(Thread::Current()->IsExceptionPending());
400 return NULL;
401 }
402
403 // invoke constructor; unlike reflection calls, we don't wrap exceptions
404 jclass jklass = AddLocalReference<jclass>(env, c);
405 jmethodID mid = EncodeMethod(init);
406 return env->NewObject(jklass, mid);
407}
408
Elliott Hughesd369bb72011-09-12 14:41:14 -0700409static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700410 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800411 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
413 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
414 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
415 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
416 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400417 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700418 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700419 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700420 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
421 NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700422 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700423};
424
Elliott Hughesd369bb72011-09-12 14:41:14 -0700425void register_java_lang_Class(JNIEnv* env) {
426 jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods));
427}
428
429} // namespace art