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