blob: 04d2e5e324f2eb3e102d6bc37dc4dd2b3a91e040 [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
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070019#include "class_linker.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070020#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "mirror/art_method.h"
22#include "mirror/art_method-inl.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
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070032/*
33 * We get here through Constructor.newInstance(). The Constructor object
34 * would not be available if the constructor weren't public (per the
35 * definition of Class.getConstructor), so we can skip the method access
36 * check. We can also safely assume the constructor isn't associated
37 * with an interface, array, or primitive class. If this is coming from
38 * native, it is OK to avoid access checks since JNI does not enforce them.
39 */
40static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070041 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierfc58af42015-04-16 18:00:39 -070042 mirror::Method* m = soa.Decode<mirror::Method*>(javaMethod);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043 StackHandleScope<1> hs(soa.Self());
44 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
Ian Rogers62d6c772013-02-27 08:32:07 -080045 if (UNLIKELY(c->IsAbstract())) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070046 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", "Can't instantiate %s %s",
Ian Rogers62d6c772013-02-27 08:32:07 -080047 c->IsInterface() ? "interface" : "abstract class",
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048 PrettyDescriptor(c.Get()).c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080049 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070050 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070051 // Verify that we can access the class (only for debug since the above comment).
52 if (kIsDebugBuild && !c->IsPublic()) {
53 auto* caller = GetCallingClass(soa.Self(), 1);
54 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
55 // access checks anyways. TODO: Investigate if this the correct behavior.
56 if (caller != nullptr && !caller->CanAccess(c.Get())) {
57 soa.Self()->ThrowNewExceptionF(
58 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
59 PrettyClass(c.Get()).c_str(), PrettyClass(caller).c_str());
60 return nullptr;
61 }
62 }
Ian Rogers7b078e82014-09-10 14:44:24 -070063 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), c, true, true)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080065 return nullptr;
Brian Carlstrom5d40f182011-09-26 22:29:18 -070066 }
Mathieu Chartierf4b97622014-03-10 13:26:27 -070067 bool movable = true;
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070068 if (!kMovingClasses && c->IsClassClass()) {
Mathieu Chartierf4b97622014-03-10 13:26:27 -070069 movable = false;
70 }
71 mirror::Object* receiver =
72 movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080073 if (receiver == nullptr) {
74 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070075 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070077 InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, 1);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070078 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
79 return javaReceiver;
80}
81
82static JNINativeMethod gMethods[] = {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070083 NATIVE_METHOD(Constructor, newInstance, "!([Ljava/lang/Object;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070084};
85
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070086void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070087 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070088}
89
90} // namespace art