blob: 45b948408eb1181b30f81050654ce849afe82854 [file] [log] [blame]
Elliott Hughes2a20cfd2011-09-23 19:30:41 -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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_reflect_Constructor.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070020#include "class_linker.h"
Jeff Hao13e748b2015-08-25 20:44:19 +000021#include "class_linker-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070022#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070024#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/object-inl.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070026#include "reflection.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070027#include "scoped_fast_native_object_access.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "well_known_classes.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070029
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070030namespace art {
31
Jeff Hao13e748b2015-08-25 20:44:19 +000032static 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
42static 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
48static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
49 ScopedFastNativeObjectAccess soa(env);
50 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Jeff Hao2a5892f2015-08-31 15:00:40 -070051 mirror::ObjectArray<mirror::Class>* result_array =
Jeff Hao13e748b2015-08-25 20:44:19 +000052 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 Hao2a5892f2015-08-31 15:00:40 -070058 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 Hao13e748b2015-08-25 20:44:19 +000063 return soa.AddLocalReference<jobjectArray>(empty_array);
64 } else {
65 return soa.AddLocalReference<jobjectArray>(result_array);
66 }
67}
68
69static 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
75static 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 Chartierf36cb5f2015-04-24 16:55:16 -070084/*
Andreas Gampe8208bdd2015-04-27 17:26:37 -070085 * We can also safely assume the constructor isn't associated
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070086 * 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 */
89static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070090 ScopedFastNativeObjectAccess soa(env);
Sebastien Hertz2d2f2a92015-04-28 15:00:41 +020091 mirror::Constructor* m = soa.Decode<mirror::Constructor*>(javaMethod);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070092 StackHandleScope<1> hs(soa.Self());
93 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
Ian Rogers62d6c772013-02-27 08:32:07 -080094 if (UNLIKELY(c->IsAbstract())) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070095 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
Ian Rogers62d6c772013-02-27 08:32:07 -080096 c->IsInterface() ? "interface" : "abstract class",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070097 PrettyDescriptor(c.Get()).c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080098 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070099 }
Andreas Gampe8208bdd2015-04-27 17:26:37 -0700100 // Verify that we can access the class.
Sebastien Hertz2d2f2a92015-04-28 15:00:41 +0200101 if (!m->IsAccessible() && !c->IsPublic()) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700102 auto* caller = GetCallingClass(soa.Self(), 1);
103 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
104 // access checks anyways. TODO: Investigate if this the correct behavior.
105 if (caller != nullptr && !caller->CanAccess(c.Get())) {
Andreas Gampe61d7ca82015-04-29 19:56:36 -0700106 if (PrettyDescriptor(c.Get()) == "dalvik.system.DexPathList$Element") {
107 // b/20699073.
108 LOG(WARNING) << "The dalvik.system.DexPathList$Element constructor is not accessible by "
109 "default. This is a temporary workaround for backwards compatibility "
110 "with class-loader hacks. Please update your application.";
111 } else {
112 soa.Self()->ThrowNewExceptionF(
113 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
114 PrettyClass(c.Get()).c_str(), PrettyClass(caller).c_str());
115 return nullptr;
116 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700117 }
118 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700119 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800121 return nullptr;
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700122 }
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700123 bool movable = true;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700124 if (!kMovingClasses && c->IsClassClass()) {
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700125 movable = false;
126 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800127
128 // String constructor is replaced by a StringFactory method in InvokeMethod.
129 if (c->IsStringClass()) {
130 return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 1);
131 }
132
Mathieu Chartierf4b97622014-03-10 13:26:27 -0700133 mirror::Object* receiver =
134 movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800135 if (receiver == nullptr) {
136 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700137 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700139 InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 1);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700140 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
141 return javaReceiver;
142}
143
144static JNINativeMethod gMethods[] = {
Jeff Hao13e748b2015-08-25 20:44:19 +0000145 NATIVE_METHOD(Constructor, getAnnotationNative,
146 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
147 NATIVE_METHOD(Constructor, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"),
148 NATIVE_METHOD(Constructor, getExceptionTypes, "!()[Ljava/lang/Class;"),
149 NATIVE_METHOD(Constructor, getParameterAnnotationsNative,
150 "!()[[Ljava/lang/annotation/Annotation;"),
151 NATIVE_METHOD(Constructor, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700152 NATIVE_METHOD(Constructor, newInstance, "!([Ljava/lang/Object;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700153};
154
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700155void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700156 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700157}
158
159} // namespace art