Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 19 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 20 | #include "object_utils.h" |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 21 | #include "reflection.h" |
| 22 | |
| 23 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 24 | |
| 25 | namespace art { |
| 26 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 27 | /* |
| 28 | * We get here through Constructor.newInstance(). The Constructor object |
| 29 | * would not be available if the constructor weren't public (per the |
| 30 | * definition of Class.getConstructor), so we can skip the method access |
| 31 | * check. We can also safely assume the constructor isn't associated |
| 32 | * with an interface, array, or primitive class. |
| 33 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 34 | static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 35 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 36 | Method* m = Decode<Object*>(env, javaMethod)->AsMethod(); |
| 37 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 38 | if (c->IsAbstract()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 39 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 40 | "Can't instantiate abstract class %s", PrettyDescriptor(c).c_str()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 44 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) { |
| 45 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 49 | Object* receiver = c->AllocObject(); |
| 50 | if (receiver == NULL) { |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | jobject javaReceiver = AddLocalReference<jobject>(env, receiver); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 55 | InvokeMethod(env, javaMethod, javaReceiver, javaArgs); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 56 | |
| 57 | // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod. |
| 58 | return javaReceiver; |
| 59 | } |
| 60 | |
| 61 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 62 | NATIVE_METHOD(Constructor, newInstance, "([Ljava/lang/Object;)Ljava/lang/Object;"), |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 65 | void register_java_lang_reflect_Constructor(JNIEnv* env) { |
| 66 | jniRegisterNativeMethods(env, "java/lang/reflect/Constructor", gMethods, NELEM(gMethods)); |
| 67 | } |
| 68 | |
| 69 | } // namespace art |