blob: 4d424509acad1be257ecf65bb6484598b072d7f1 [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) {
36 Class* c = Decode<Class*>(env, javaDeclaringClass);
37 if (c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070038 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070039 "Can't instantiate abstract class %s", PrettyDescriptor(c->GetDescriptor()).c_str());
40 return NULL;
41 }
42
Brian Carlstrom5d40f182011-09-26 22:29:18 -070043 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true)) {
44 DCHECK(Thread::Current()->IsExceptionPending());
45 return NULL;
46 }
47
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070048 Object* receiver = c->AllocObject();
49 if (receiver == NULL) {
50 return NULL;
51 }
52
53 jobject javaReceiver = AddLocalReference<jobject>(env, receiver);
54 InvokeMethod(env, javaMethod, javaReceiver, javaArgs, javaParams);
55
56 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
57 return javaReceiver;
58}
59
60static JNINativeMethod gMethods[] = {
61 NATIVE_METHOD(Constructor, constructNative, "([Ljava/lang/Object;Ljava/lang/Class;[Ljava/lang/Class;IZ)Ljava/lang/Object;"),
62};
63
64} // namespace
65
66void register_java_lang_reflect_Constructor(JNIEnv* env) {
67 jniRegisterNativeMethods(env, "java/lang/reflect/Constructor", gMethods, NELEM(gMethods));
68}
69
70} // namespace art