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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 22 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 24 | #include "mirror/method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 26 | #include "reflection.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 27 | #include "scoped_fast_native_object_access.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 28 | #include "well_known_classes.h" |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 32 | static jobject Constructor_getAnnotationNative(JNIEnv* env, jobject javaMethod, |
| 33 | jclass annotationType) { |
| 34 | ScopedFastNativeObjectAccess soa(env); |
| 35 | StackHandleScope<1> hs(soa.Self()); |
| 36 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 37 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType))); |
| 38 | return soa.AddLocalReference<jobject>( |
| 39 | method->GetDexFile()->GetAnnotationForMethod(method, klass)); |
| 40 | } |
| 41 | |
| 42 | static jobjectArray Constructor_getDeclaredAnnotations(JNIEnv* env, jobject javaMethod) { |
| 43 | ScopedFastNativeObjectAccess soa(env); |
| 44 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 45 | return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetAnnotationsForMethod(method)); |
| 46 | } |
| 47 | |
| 48 | static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) { |
| 49 | ScopedFastNativeObjectAccess soa(env); |
| 50 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 51 | mirror::ObjectArray<mirror::Class>* result_array = |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 52 | method->GetDexFile()->GetExceptionTypesForMethod(method); |
| 53 | if (result_array == nullptr) { |
| 54 | // Return an empty array instead of a null pointer. |
| 55 | mirror::Class* class_class = mirror::Class::GetJavaLangClass(); |
| 56 | mirror::Class* class_array_class = |
| 57 | Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 58 | if (class_array_class == nullptr) { |
| 59 | return nullptr; |
| 60 | } |
| 61 | mirror::ObjectArray<mirror::Class>* empty_array = |
| 62 | mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 63 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 64 | } else { |
| 65 | return soa.AddLocalReference<jobjectArray>(result_array); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static jobjectArray Constructor_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
| 70 | ScopedFastNativeObjectAccess soa(env); |
| 71 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 72 | return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetParameterAnnotations(method)); |
| 73 | } |
| 74 | |
| 75 | static jboolean Constructor_isAnnotationPresentNative(JNIEnv* env, jobject javaMethod, |
| 76 | jclass annotationType) { |
| 77 | ScopedFastNativeObjectAccess soa(env); |
| 78 | StackHandleScope<1> hs(soa.Self()); |
| 79 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 80 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType))); |
| 81 | return method->GetDexFile()->IsMethodAnnotationPresent(method, klass); |
| 82 | } |
| 83 | |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 84 | /* |
Andreas Gampe | 8208bdd | 2015-04-27 17:26:37 -0700 | [diff] [blame] | 85 | * We can also safely assume the constructor isn't associated |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 86 | * with an interface, array, or primitive class. If this is coming from |
| 87 | * native, it is OK to avoid access checks since JNI does not enforce them. |
| 88 | */ |
Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 89 | static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 90 | ScopedFastNativeObjectAccess soa(env); |
Sebastien Hertz | 2d2f2a9 | 2015-04-28 15:00:41 +0200 | [diff] [blame] | 91 | mirror::Constructor* m = soa.Decode<mirror::Constructor*>(javaMethod); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 92 | StackHandleScope<1> hs(soa.Self()); |
| 93 | Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 94 | if (UNLIKELY(c->IsAbstract())) { |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 95 | soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 96 | c->IsInterface() ? "interface" : "abstract class", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 97 | PrettyDescriptor(c.Get()).c_str()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 98 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 99 | } |
Andreas Gampe | 8208bdd | 2015-04-27 17:26:37 -0700 | [diff] [blame] | 100 | // Verify that we can access the class. |
Sebastien Hertz | 2d2f2a9 | 2015-04-28 15:00:41 +0200 | [diff] [blame] | 101 | if (!m->IsAccessible() && !c->IsPublic()) { |
Przemyslaw Szczepaniak | 3ddd593 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 102 | // Go 2 frames back, this method is always called from newInstance0, which is called from |
Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 103 | // Constructor.newInstance(Object... args). |
Przemyslaw Szczepaniak | 3ddd593 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 104 | auto* caller = GetCallingClass(soa.Self(), 2); |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 105 | // If caller is null, then we called from JNI, just avoid the check since JNI avoids most |
| 106 | // access checks anyways. TODO: Investigate if this the correct behavior. |
| 107 | if (caller != nullptr && !caller->CanAccess(c.Get())) { |
Andreas Gampe | 61d7ca8 | 2015-04-29 19:56:36 -0700 | [diff] [blame] | 108 | if (PrettyDescriptor(c.Get()) == "dalvik.system.DexPathList$Element") { |
| 109 | // b/20699073. |
| 110 | LOG(WARNING) << "The dalvik.system.DexPathList$Element constructor is not accessible by " |
| 111 | "default. This is a temporary workaround for backwards compatibility " |
| 112 | "with class-loader hacks. Please update your application."; |
| 113 | } else { |
| 114 | soa.Self()->ThrowNewExceptionF( |
| 115 | "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s", |
| 116 | PrettyClass(c.Get()).c_str(), PrettyClass(caller).c_str()); |
| 117 | return nullptr; |
| 118 | } |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 121 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 122 | DCHECK(soa.Self()->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 123 | return nullptr; |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 124 | } |
Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 125 | bool movable = true; |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 126 | if (!kMovingClasses && c->IsClassClass()) { |
Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 127 | movable = false; |
| 128 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 129 | |
| 130 | // String constructor is replaced by a StringFactory method in InvokeMethod. |
| 131 | if (c->IsStringClass()) { |
Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 132 | return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Mathieu Chartier | f4b9762 | 2014-03-10 13:26:27 -0700 | [diff] [blame] | 135 | mirror::Object* receiver = |
| 136 | movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 137 | if (receiver == nullptr) { |
| 138 | return nullptr; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 139 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 140 | jobject javaReceiver = soa.AddLocalReference<jobject>(receiver); |
Narayan Kamath | e091582 | 2015-11-18 13:00:18 +0000 | [diff] [blame] | 141 | InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 2); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 142 | // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod. |
| 143 | return javaReceiver; |
| 144 | } |
| 145 | |
Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 146 | static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, |
| 147 | jclass ctorClass, jclass allocClass) { |
| 148 | jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V"); |
| 149 | DCHECK(ctor != NULL); |
| 150 | return env->NewObject(allocClass, ctor); |
| 151 | } |
| 152 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 153 | static JNINativeMethod gMethods[] = { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 154 | NATIVE_METHOD(Constructor, getAnnotationNative, |
| 155 | "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
| 156 | NATIVE_METHOD(Constructor, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"), |
| 157 | NATIVE_METHOD(Constructor, getExceptionTypes, "!()[Ljava/lang/Class;"), |
| 158 | NATIVE_METHOD(Constructor, getParameterAnnotationsNative, |
| 159 | "!()[[Ljava/lang/annotation/Annotation;"), |
| 160 | NATIVE_METHOD(Constructor, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"), |
Przemyslaw Szczepaniak | dcf1b59 | 2015-10-12 16:34:14 +0100 | [diff] [blame] | 161 | NATIVE_METHOD(Constructor, newInstance0, "!([Ljava/lang/Object;)Ljava/lang/Object;"), |
| 162 | NATIVE_METHOD(Constructor, newInstanceFromSerialization, "!(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"), |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 165 | void register_java_lang_reflect_Constructor(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 166 | REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor"); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace art |