blob: d28ebd0e78da42b744843eaca64bbe2b1b7cbb82 [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
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070017#include "class_linker.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070018#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "mirror/art_method.h"
20#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/object-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object_utils.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070024#include "reflection.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070025#include "scoped_fast_native_object_access.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "well_known_classes.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070027
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070028namespace art {
29
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070030/*
31 * We get here through Constructor.newInstance(). The Constructor object
32 * would not be available if the constructor weren't public (per the
33 * definition of Class.getConstructor), so we can skip the method access
34 * check. We can also safely assume the constructor isn't associated
35 * with an interface, array, or primitive class.
36 */
Jeff Hao11d5d8f2014-03-26 15:08:20 -070037static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs,
38 jboolean accessible) {
Ian Rogers53b8b092014-03-13 23:45:53 -070039 ScopedFastNativeObjectAccess soa(env);
Ian Rogers62f05122014-03-21 11:21:29 -070040 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080041 SirtRef<mirror::Class> c(soa.Self(), m->GetDeclaringClass());
Ian Rogers62d6c772013-02-27 08:32:07 -080042 if (UNLIKELY(c->IsAbstract())) {
43 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
44 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/InstantiationException;",
45 "Can't instantiate %s %s",
46 c->IsInterface() ? "interface" : "abstract class",
Mathieu Chartierc528dba2013-11-26 12:00:11 -080047 PrettyDescriptor(c.get()).c_str());
48 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070049 }
50
Ian Rogers0045a292012-03-31 21:08:41 -070051 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080053 return nullptr;
Brian Carlstrom5d40f182011-09-26 22:29:18 -070054 }
55
Mathieu Chartierf4b97622014-03-10 13:26:27 -070056 bool movable = true;
57 if (!kMovingMethods && c->IsArtMethodClass()) {
58 movable = false;
59 } else if (!kMovingFields && c->IsArtFieldClass()) {
60 movable = false;
61 } else if (!kMovingClasses && c->IsClassClass()) {
62 movable = false;
63 }
64 mirror::Object* receiver =
65 movable ? c->AllocObject(soa.Self()) : c->AllocNonMovableObject(soa.Self());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080066 if (receiver == nullptr) {
67 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070068 }
69
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 jobject javaReceiver = soa.AddLocalReference<jobject>(receiver);
Jeff Hao11d5d8f2014-03-26 15:08:20 -070071 InvokeMethod(soa, javaMethod, javaReceiver, javaArgs, accessible);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070072
73 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
74 return javaReceiver;
75}
76
77static JNINativeMethod gMethods[] = {
Jeff Hao11d5d8f2014-03-26 15:08:20 -070078 NATIVE_METHOD(Constructor, newInstance, "!([Ljava/lang/Object;Z)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070079};
80
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070081void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070082 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070083}
84
85} // namespace art