| 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 | |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_reflect_Constructor.h" |
| 18 | |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 19 | #include "class_linker.h" |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 20 | #include "jni_internal.h" |
| Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "mirror/art_method.h" |
| 22 | #include "mirror/art_method-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/object-inl.h" |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 25 | #include "reflection.h" |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 26 | #include "scoped_fast_native_object_access.h" |
| Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 27 | #include "well_known_classes.h" |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 28 | |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 31 | /* |
| 32 | * We get here through Constructor.newInstance(). The Constructor object |
| 33 | * would not be available if the constructor weren't public (per the |
| 34 | * definition of Class.getConstructor), so we can skip the method access |
| 35 | * check. We can also safely assume the constructor isn't associated |
| 36 | * with an interface, array, or primitive class. |
| 37 | */ |
| Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 38 | static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs, |
| 39 | jboolean accessible) { |
| Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 40 | ScopedFastNativeObjectAccess soa(env); |
| Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 41 | mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod); |
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 42 | StackHandleScope<1> hs(soa.Self()); |
| 43 | Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass())); |
| Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 44 | if (UNLIKELY(c->IsAbstract())) { |
| 45 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 46 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/InstantiationException;", |
| 47 | "Can't instantiate %s %s", |
| 48 | c->IsInterface() ? "interface" : "abstract class", |
| Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 49 | PrettyDescriptor(c.Get()).c_str()); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 50 | return nullptr; |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 53 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 54 | DCHECK(soa.Self()->IsExceptionPending()); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 55 | return nullptr; |
| Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 58 | bool movable = true; |
| 59 | if (!kMovingMethods && c->IsArtMethodClass()) { |
| 60 | movable = false; |
| 61 | } else if (!kMovingFields && c->IsArtFieldClass()) { |
| 62 | movable = false; |
| 63 | } else if (!kMovingClasses && c->IsClassClass()) { |
| 64 | movable = false; |
| 65 | } |
| 66 | mirror::Object* receiver = |
| 67 | movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self()); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 68 | if (receiver == nullptr) { |
| 69 | return nullptr; |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | jobject javaReceiver = soa.AddLocalReference<jobject>(receiver); |
| Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 73 | InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, (accessible == JNI_TRUE)); |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 74 | |
| 75 | // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod. |
| 76 | return javaReceiver; |
| 77 | } |
| 78 | |
| 79 | static JNINativeMethod gMethods[] = { |
| Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 80 | NATIVE_METHOD(Constructor, newInstance, "!([Ljava/lang/Object;Z)Ljava/lang/Object;"), |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 83 | void register_java_lang_reflect_Constructor(JNIEnv* env) { |
| Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 84 | REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor"); |
| Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } // namespace art |