blob: 1094d06334e403207072166737734d71bae59ccf [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"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070019#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "object_utils.h"
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070021#include "reflection.h"
22
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070023namespace art {
24
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070025/*
26 * We get here through Constructor.newInstance(). The Constructor object
27 * would not be available if the constructor weren't public (per the
28 * definition of Class.getConstructor), so we can skip the method access
29 * check. We can also safely assume the constructor isn't associated
30 * with an interface, array, or primitive class.
31 */
Elliott Hughes0512f022012-03-15 22:10:52 -070032static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Elliott Hughes34e06962012-04-09 13:55:55 -070033 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080034 Method* m = Decode<Object*>(env, javaMethod)->AsMethod();
35 Class* c = m->GetDeclaringClass();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070036 if (c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070037 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080038 "Can't instantiate abstract class %s", PrettyDescriptor(c).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070039 return NULL;
40 }
41
Ian Rogers0045a292012-03-31 21:08:41 -070042 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -070043 DCHECK(Thread::Current()->IsExceptionPending());
44 return NULL;
45 }
46
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070047 Object* receiver = c->AllocObject();
48 if (receiver == NULL) {
49 return NULL;
50 }
51
52 jobject javaReceiver = AddLocalReference<jobject>(env, receiver);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080053 InvokeMethod(env, javaMethod, javaReceiver, javaArgs);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070054
55 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
56 return javaReceiver;
57}
58
59static JNINativeMethod gMethods[] = {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080060 NATIVE_METHOD(Constructor, newInstance, "([Ljava/lang/Object;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070061};
62
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070063void register_java_lang_reflect_Constructor(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070064 REGISTER_NATIVE_METHODS("java/lang/reflect/Constructor");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070065}
66
67} // namespace art