blob: f730543c6f4d4dc3c518a6a5be569cdfd6604ceb [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"
20#include "reflection.h"
21
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
28/*
29 * We get here through Constructor.newInstance(). The Constructor object
30 * would not be available if the constructor weren't public (per the
31 * definition of Class.getConstructor), so we can skip the method access
32 * check. We can also safely assume the constructor isn't associated
33 * with an interface, array, or primitive class.
34 */
35jobject Constructor_constructNative(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs, jclass javaDeclaringClass, jobjectArray javaParams, jint, jboolean) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070036 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070037 Class* c = Decode<Class*>(env, javaDeclaringClass);
38 if (c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070039 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070040 "Can't instantiate abstract class %s", PrettyDescriptor(c->GetDescriptor()).c_str());
41 return NULL;
42 }
43
Brian Carlstrom5d40f182011-09-26 22:29:18 -070044 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
45 DCHECK(Thread::Current()->IsExceptionPending());
46 return NULL;
47 }
48
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070049 Object* receiver = c->AllocObject();
50 if (receiver == NULL) {
51 return NULL;
52 }
53
54 jobject javaReceiver = AddLocalReference<jobject>(env, receiver);
55 InvokeMethod(env, javaMethod, javaReceiver, javaArgs, javaParams);
56
57 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
58 return javaReceiver;
59}
60
61static JNINativeMethod gMethods[] = {
62 NATIVE_METHOD(Constructor, constructNative, "([Ljava/lang/Object;Ljava/lang/Class;[Ljava/lang/Class;IZ)Ljava/lang/Object;"),
63};
64
65} // namespace
66
67void register_java_lang_reflect_Constructor(JNIEnv* env) {
68 jniRegisterNativeMethods(env, "java/lang/reflect/Constructor", gMethods, NELEM(gMethods));
69}
70
71} // namespace art