blob: 36481f7675dd4d14b45dcb6fab902102fcb339f7 [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
17#include "jni_internal.h"
18#include "class_linker.h"
19#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
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24
25namespace art {
26
27namespace {
28
29/*
30 * We get here through Constructor.newInstance(). The Constructor object
31 * would not be available if the constructor weren't public (per the
32 * definition of Class.getConstructor), so we can skip the method access
33 * check. We can also safely assume the constructor isn't associated
34 * with an interface, array, or primitive class.
35 */
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070037 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080038 Method* m = Decode<Object*>(env, javaMethod)->AsMethod();
39 Class* c = m->GetDeclaringClass();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070040 if (c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070041 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042 "Can't instantiate abstract class %s", PrettyDescriptor(c).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070043 return NULL;
44 }
45
Brian Carlstrom5d40f182011-09-26 22:29:18 -070046 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
47 DCHECK(Thread::Current()->IsExceptionPending());
48 return NULL;
49 }
50
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070051 Object* receiver = c->AllocObject();
52 if (receiver == NULL) {
53 return NULL;
54 }
55
56 jobject javaReceiver = AddLocalReference<jobject>(env, receiver);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080057 InvokeMethod(env, javaMethod, javaReceiver, javaArgs);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070058
59 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
60 return javaReceiver;
61}
62
63static JNINativeMethod gMethods[] = {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080064 NATIVE_METHOD(Constructor, newInstance, "([Ljava/lang/Object;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070065};
66
67} // namespace
68
69void register_java_lang_reflect_Constructor(JNIEnv* env) {
70 jniRegisterNativeMethods(env, "java/lang/reflect/Constructor", gMethods, NELEM(gMethods));
71}
72
73} // namespace art