OP_NEW_INSTANCE.c revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1HANDLE_OPCODE(OP_NEW_INSTANCE /*vAA, class@BBBB*/)
2    {
3        ClassObject* clazz;
4        Object* newObj;
5
6        EXPORT_PC();
7
8        vdst = INST_AA(inst);
9        ref = FETCH(1);
10        ILOGV("|new-instance v%d,class@0x%04x", vdst, ref);
11        clazz = dvmDexGetResolvedClass(methodClassDex, ref);
12        if (clazz == NULL) {
13            clazz = dvmResolveClass(curMethod->clazz, ref, false);
14            if (clazz == NULL)
15                GOTO_exceptionThrown();
16        }
17
18        if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz))
19            GOTO_exceptionThrown();
20
21        /*
22         * Note: the verifier can ensure that this never happens, allowing us
23         * to remove the check.  However, the spec requires we throw the
24         * exception at runtime, not verify time, so the verifier would
25         * need to replace the new-instance call with a magic "throw
26         * InstantiationError" instruction.
27         *
28         * Since this relies on the verifier, which is optional, we would
29         * also need a "new-instance-quick" instruction to identify instances
30         * that don't require the check.
31         */
32        if (dvmIsInterfaceClass(clazz) || dvmIsAbstractClass(clazz)) {
33            dvmThrowExceptionWithClassMessage("Ljava/lang/InstantiationError;",
34                clazz->descriptor);
35            GOTO_exceptionThrown();
36        }
37        newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
38        if (newObj == NULL)
39            GOTO_exceptionThrown();
40        SET_REGISTER(vdst, (u4) newObj);
41    }
42    FINISH(2);
43OP_END
44