Home | History | Annotate | Download | only in native
      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 getConstructorModifiers(Class declaringClass, int slot)
     26  */
     27 static void Dalvik_java_lang_reflect_Constructor_getConstructorModifiers(
     28     const u4* args, JValue* pResult)
     29 {
     30     // ignore thisPtr in args[0]
     31     ClassObject* declaringClass = (ClassObject*) args[1];
     32     int slot = args[2];
     33     Method* meth;
     34 
     35     meth = dvmSlotToMethod(declaringClass, slot);
     36     RETURN_INT(dvmFixMethodFlags(meth->accessFlags));
     37 }
     38 
     39 /*
     40  * public int constructNative(Object[] args, Class declaringClass,
     41  *     Class[] parameterTypes, int slot, boolean noAccessCheck)
     42  *
     43  * We get here through Constructor.newInstance().  The Constructor object
     44  * would not be available if the constructor weren't public (per the
     45  * definition of Class.getConstructor), so we can skip the method access
     46  * check.  We can also safely assume the constructor isn't associated
     47  * with an interface, array, or primitive class.
     48  */
     49 static void Dalvik_java_lang_reflect_Constructor_constructNative(
     50     const u4* args, JValue* pResult)
     51 {
     52     // ignore thisPtr in args[0]
     53     ArrayObject* argList = (ArrayObject*) args[1];
     54     ClassObject* declaringClass = (ClassObject*) args[2];
     55     ArrayObject* params = (ArrayObject*) args[3];
     56     int slot = args[4];
     57     bool noAccessCheck = (args[5] != 0);
     58     Object* newObj;
     59     Method* meth;
     60 
     61     if (dvmIsAbstractClass(declaringClass)) {
     62         dvmThrowExceptionWithClassMessage("Ljava/lang/InstantiationException;",
     63             declaringClass->descriptor);
     64         RETURN_VOID();
     65     }
     66 
     67     /* initialize the class if it hasn't been already */
     68     if (!dvmIsClassInitialized(declaringClass)) {
     69         if (!dvmInitClass(declaringClass)) {
     70             LOGW("Class init failed in Constructor.constructNative (%s)\n",
     71                 declaringClass->descriptor);
     72             assert(dvmCheckException(dvmThreadSelf()));
     73             RETURN_VOID();
     74         }
     75     }
     76 
     77     newObj = dvmAllocObject(declaringClass, ALLOC_DEFAULT);
     78     if (newObj == NULL)
     79         RETURN_PTR(NULL);
     80 
     81     meth = dvmSlotToMethod(declaringClass, slot);
     82     assert(meth != NULL);
     83 
     84     (void) dvmInvokeMethod(newObj, meth, argList, params, NULL, noAccessCheck);
     85     dvmReleaseTrackedAlloc(newObj, NULL);
     86     RETURN_PTR(newObj);
     87 }
     88 
     89 /*
     90  * public Annotation[] getDeclaredAnnotations(Class declaringClass, int slot)
     91  *
     92  * Return the annotations declared for this constructor.
     93  */
     94 static void Dalvik_java_lang_reflect_Constructor_getDeclaredAnnotations(
     95     const u4* args, JValue* pResult)
     96 {
     97     // ignore thisPtr in args[0]
     98     ClassObject* declaringClass = (ClassObject*) args[1];
     99     int slot = args[2];
    100     Method* meth;
    101 
    102     meth = dvmSlotToMethod(declaringClass, slot);
    103     assert(meth != NULL);
    104 
    105     ArrayObject* annos = dvmGetMethodAnnotations(meth);
    106     dvmReleaseTrackedAlloc((Object*)annos, NULL);
    107     RETURN_PTR(annos);
    108 }
    109 
    110 /*
    111  * public Annotation[][] getParameterAnnotations(Class declaringClass, int slot)
    112  *
    113  * Return the annotations declared for this constructor's parameters.
    114  */
    115 static void Dalvik_java_lang_reflect_Constructor_getParameterAnnotations(
    116     const u4* args, JValue* pResult)
    117 {
    118     // ignore thisPtr in args[0]
    119     ClassObject* declaringClass = (ClassObject*) args[1];
    120     int slot = args[2];
    121     Method* meth;
    122 
    123     meth = dvmSlotToMethod(declaringClass, slot);
    124     assert(meth != NULL);
    125 
    126     ArrayObject* annos = dvmGetParameterAnnotations(meth);
    127     dvmReleaseTrackedAlloc((Object*)annos, NULL);
    128     RETURN_PTR(annos);
    129 }
    130 
    131 /*
    132  * private Object[] getSignatureAnnotation()
    133  *
    134  * Returns the signature annotation.
    135  */
    136 static void Dalvik_java_lang_reflect_Constructor_getSignatureAnnotation(
    137     const u4* args, JValue* pResult)
    138 {
    139     // ignore thisPtr in args[0]
    140     ClassObject* declaringClass = (ClassObject*) args[1];
    141     int slot = args[2];
    142     Method* meth;
    143 
    144     meth = dvmSlotToMethod(declaringClass, slot);
    145     assert(meth != NULL);
    146 
    147     ArrayObject* arr = dvmGetMethodSignatureAnnotation(meth);
    148     dvmReleaseTrackedAlloc((Object*) arr, NULL);
    149     RETURN_PTR(arr);
    150 }
    151 
    152 const DalvikNativeMethod dvm_java_lang_reflect_Constructor[] = {
    153     { "constructNative",    "([Ljava/lang/Object;Ljava/lang/Class;[Ljava/lang/Class;IZ)Ljava/lang/Object;",
    154         Dalvik_java_lang_reflect_Constructor_constructNative },
    155     { "getConstructorModifiers", "(Ljava/lang/Class;I)I",
    156         Dalvik_java_lang_reflect_Constructor_getConstructorModifiers },
    157     { "getDeclaredAnnotations", "(Ljava/lang/Class;I)[Ljava/lang/annotation/Annotation;",
    158         Dalvik_java_lang_reflect_Constructor_getDeclaredAnnotations },
    159     { "getParameterAnnotations", "(Ljava/lang/Class;I)[[Ljava/lang/annotation/Annotation;",
    160         Dalvik_java_lang_reflect_Constructor_getParameterAnnotations },
    161     { "getSignatureAnnotation",  "(Ljava/lang/Class;I)[Ljava/lang/Object;",
    162         Dalvik_java_lang_reflect_Constructor_getSignatureAnnotation },
    163     { NULL, NULL, NULL },
    164 };
    165