blob: c908bb702cde4d951f4e424632aa0e347b209dd1 [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
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070027/*
28 * We get here through Constructor.newInstance(). The Constructor object
29 * would not be available if the constructor weren't public (per the
30 * definition of Class.getConstructor), so we can skip the method access
31 * check. We can also safely assume the constructor isn't associated
32 * with an interface, array, or primitive class.
33 */
Elliott Hughes0512f022012-03-15 22:10:52 -070034static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070035 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036 Method* m = Decode<Object*>(env, javaMethod)->AsMethod();
37 Class* c = m->GetDeclaringClass();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070038 if (c->IsAbstract()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070039 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040 "Can't instantiate abstract class %s", PrettyDescriptor(c).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070041 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);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080055 InvokeMethod(env, javaMethod, javaReceiver, javaArgs);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070056
57 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
58 return javaReceiver;
59}
60
61static JNINativeMethod gMethods[] = {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080062 NATIVE_METHOD(Constructor, newInstance, "([Ljava/lang/Object;)Ljava/lang/Object;"),
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070063};
64
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070065void register_java_lang_reflect_Constructor(JNIEnv* env) {
66 jniRegisterNativeMethods(env, "java/lang/reflect/Constructor", gMethods, NELEM(gMethods));
67}
68
69} // namespace art