java_lang_reflect_Constructor.cpp revision 60fc806b679a3655c228b4093058c59941a49cfe
1/*
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/*
18 * java.lang.reflect.Constructor
19 */
20#include "Dalvik.h"
21#include "native/InternalNativePriv.h"
22
23
24/*
25 * public int constructNative(Object[] args, Class declaringClass,
26 *     Class[] parameterTypes, int slot, boolean noAccessCheck)
27 *
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 */
34static void Dalvik_java_lang_reflect_Constructor_constructNative(
35    const u4* args, JValue* pResult)
36{
37    // ignore thisPtr in args[0]
38    ArrayObject* argList = (ArrayObject*) args[1];
39    ClassObject* declaringClass = (ClassObject*) args[2];
40    ArrayObject* params = (ArrayObject*) args[3];
41    int slot = args[4];
42    bool noAccessCheck = (args[5] != 0);
43    Object* newObj;
44    Method* meth;
45
46    if (dvmIsAbstractClass(declaringClass)) {
47        dvmThrowInstantiationException(declaringClass, NULL);
48        RETURN_VOID();
49    }
50
51    /* initialize the class if it hasn't been already */
52    if (!dvmIsClassInitialized(declaringClass)) {
53        if (!dvmInitClass(declaringClass)) {
54            LOGW("Class init failed in Constructor.constructNative (%s)",
55                declaringClass->descriptor);
56            assert(dvmCheckException(dvmThreadSelf()));
57            RETURN_VOID();
58        }
59    }
60
61    newObj = dvmAllocObject(declaringClass, ALLOC_DEFAULT);
62    if (newObj == NULL)
63        RETURN_PTR(NULL);
64
65    meth = dvmSlotToMethod(declaringClass, slot);
66    assert(meth != NULL);
67
68    (void) dvmInvokeMethod(newObj, meth, argList, params, NULL, noAccessCheck);
69    dvmReleaseTrackedAlloc(newObj, NULL);
70    RETURN_PTR(newObj);
71}
72
73const DalvikNativeMethod dvm_java_lang_reflect_Constructor[] = {
74    { "constructNative",    "([Ljava/lang/Object;Ljava/lang/Class;[Ljava/lang/Class;IZ)Ljava/lang/Object;",
75        Dalvik_java_lang_reflect_Constructor_constructNative },
76    { NULL, NULL, NULL },
77};
78