• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "java_lang_reflect_Constructor.h"
18 
19 #include "art_method-inl.h"
20 #include "class_linker.h"
21 #include "class_linker-inl.h"
22 #include "jni_internal.h"
23 #include "mirror/class-inl.h"
24 #include "mirror/method.h"
25 #include "mirror/object-inl.h"
26 #include "reflection.h"
27 #include "scoped_fast_native_object_access.h"
28 #include "well_known_classes.h"
29 
30 namespace art {
31 
Constructor_getAnnotationNative(JNIEnv * env,jobject javaMethod,jclass annotationType)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   if (method->IsProxyMethod()) {
38     return nullptr;
39   } else {
40     Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType)));
41     return soa.AddLocalReference<jobject>(
42         method->GetDexFile()->GetAnnotationForMethod(method, klass));
43   }
44 }
45 
Constructor_getDeclaredAnnotations(JNIEnv * env,jobject javaMethod)46 static jobjectArray Constructor_getDeclaredAnnotations(JNIEnv* env, jobject javaMethod) {
47   ScopedFastNativeObjectAccess soa(env);
48   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
49   if (method->IsProxyMethod()) {
50     mirror::Class* class_class = mirror::Class::GetJavaLangClass();
51     mirror::Class* class_array_class =
52         Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
53     if (class_array_class == nullptr) {
54       return nullptr;
55     }
56     mirror::ObjectArray<mirror::Class>* empty_array =
57         mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
58     return soa.AddLocalReference<jobjectArray>(empty_array);
59   } else {
60     return soa.AddLocalReference<jobjectArray>(
61         method->GetDexFile()->GetAnnotationsForMethod(method));
62   }
63 }
64 
Constructor_getExceptionTypes(JNIEnv * env,jobject javaMethod)65 static jobjectArray Constructor_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
66   ScopedFastNativeObjectAccess soa(env);
67   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod)
68       ->GetInterfaceMethodIfProxy(sizeof(void*));
69   mirror::ObjectArray<mirror::Class>* result_array =
70       method->GetDexFile()->GetExceptionTypesForMethod(method);
71   if (result_array == nullptr) {
72     // Return an empty array instead of a null pointer.
73     mirror::Class* class_class = mirror::Class::GetJavaLangClass();
74     mirror::Class* class_array_class =
75         Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
76     if (class_array_class == nullptr) {
77       return nullptr;
78     }
79     mirror::ObjectArray<mirror::Class>* empty_array =
80         mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
81     return soa.AddLocalReference<jobjectArray>(empty_array);
82   } else {
83     return soa.AddLocalReference<jobjectArray>(result_array);
84   }
85 }
86 
Constructor_getParameterAnnotationsNative(JNIEnv * env,jobject javaMethod)87 static jobjectArray Constructor_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
88   ScopedFastNativeObjectAccess soa(env);
89   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
90   if (method->IsProxyMethod()) {
91     return nullptr;
92   } else {
93     return soa.AddLocalReference<jobjectArray>(
94         method->GetDexFile()->GetParameterAnnotations(method));
95   }
96 }
97 
Constructor_isAnnotationPresentNative(JNIEnv * env,jobject javaMethod,jclass annotationType)98 static jboolean Constructor_isAnnotationPresentNative(JNIEnv* env, jobject javaMethod,
99                                                       jclass annotationType) {
100   ScopedFastNativeObjectAccess soa(env);
101   StackHandleScope<1> hs(soa.Self());
102   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
103   if (method->IsProxyMethod()) {
104     // Proxies have no annotations.
105     return false;
106   }
107   Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType)));
108   return method->GetDexFile()->IsMethodAnnotationPresent(method, klass);
109 }
110 
111 /*
112  * We can also safely assume the constructor isn't associated
113  * with an interface, array, or primitive class. If this is coming from
114  * native, it is OK to avoid access checks since JNI does not enforce them.
115  */
Constructor_newInstance0(JNIEnv * env,jobject javaMethod,jobjectArray javaArgs)116 static jobject Constructor_newInstance0(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
117   ScopedFastNativeObjectAccess soa(env);
118   mirror::Constructor* m = soa.Decode<mirror::Constructor*>(javaMethod);
119   StackHandleScope<1> hs(soa.Self());
120   Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
121   if (UNLIKELY(c->IsAbstract())) {
122     soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
123                                    c->IsInterface() ? "interface" : "abstract class",
124                                    PrettyDescriptor(c.Get()).c_str());
125     return nullptr;
126   }
127   // Verify that we can access the class.
128   if (!m->IsAccessible() && !c->IsPublic()) {
129     // Go 2 frames back, this method is always called from newInstance0, which is called from
130     // Constructor.newInstance(Object... args).
131     auto* caller = GetCallingClass(soa.Self(), 2);
132     // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
133     // access checks anyways. TODO: Investigate if this the correct behavior.
134     if (caller != nullptr && !caller->CanAccess(c.Get())) {
135       if (PrettyDescriptor(c.Get()) == "dalvik.system.DexPathList$Element") {
136         // b/20699073.
137         LOG(WARNING) << "The dalvik.system.DexPathList$Element constructor is not accessible by "
138                         "default. This is a temporary workaround for backwards compatibility "
139                         "with class-loader hacks. Please update your application.";
140       } else {
141         soa.Self()->ThrowNewExceptionF(
142             "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
143             PrettyClass(c.Get()).c_str(), PrettyClass(caller).c_str());
144         return nullptr;
145       }
146     }
147   }
148   if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
149     DCHECK(soa.Self()->IsExceptionPending());
150     return nullptr;
151   }
152   bool movable = true;
153   if (!kMovingClasses && c->IsClassClass()) {
154     movable = false;
155   }
156 
157   // String constructor is replaced by a StringFactory method in InvokeMethod.
158   if (c->IsStringClass()) {
159     return InvokeMethod(soa, javaMethod, nullptr, javaArgs, 2);
160   }
161 
162   mirror::Object* receiver =
163       movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
164   if (receiver == nullptr) {
165     return nullptr;
166   }
167   jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
168   InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 2);
169   // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
170   return javaReceiver;
171 }
172 
Constructor_newInstanceFromSerialization(JNIEnv * env,jclass unused ATTRIBUTE_UNUSED,jclass ctorClass,jclass allocClass)173 static jobject Constructor_newInstanceFromSerialization(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED,
174                                                         jclass ctorClass, jclass allocClass) {
175     jmethodID ctor = env->GetMethodID(ctorClass, "<init>", "()V");
176     DCHECK(ctor != NULL);
177     return env->NewObject(allocClass, ctor);
178 }
179 
180 static JNINativeMethod gMethods[] = {
181   NATIVE_METHOD(Constructor, getAnnotationNative,
182                 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
183   NATIVE_METHOD(Constructor, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"),
184   NATIVE_METHOD(Constructor, getExceptionTypes, "!()[Ljava/lang/Class;"),
185   NATIVE_METHOD(Constructor, getParameterAnnotationsNative,
186                 "!()[[Ljava/lang/annotation/Annotation;"),
187   NATIVE_METHOD(Constructor, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
188   NATIVE_METHOD(Constructor, newInstance0, "!([Ljava/lang/Object;)Ljava/lang/Object;"),
189   NATIVE_METHOD(Constructor, newInstanceFromSerialization, "!(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;"),
190 };
191 
register_java_lang_reflect_Constructor(JNIEnv * env)192 void register_java_lang_reflect_Constructor(JNIEnv* env) {
193   REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
194 }
195 
196 }  // namespace art
197