LCOV - code coverage report
Current view: top level - native/vm/gnuclasspath - sun_reflect_ConstantPool.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 115 27.0 %
Date: 2017-07-14 10:03:36 Functions: 6 15 40.0 %

          Line data    Source code
       1             : /* src/native/vm/gnuclasspath/sun_reflect_ConstantPool.cpp
       2             : 
       3             :    Copyright (C) 2007-2013
       4             :    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
       5             : 
       6             :    This file is part of CACAO.
       7             : 
       8             :    This program is free software; you can redistribute it and/or
       9             :    modify it under the terms of the GNU General Public License as
      10             :    published by the Free Software Foundation; either version 2, or (at
      11             :    your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful, but
      14             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :    General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program; if not, write to the Free Software
      20             :    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
      21             :    02110-1301, USA.
      22             : 
      23             : */
      24             : 
      25             : /*******************************************************************************
      26             : 
      27             :    XXX: The Methods in this file are very redundant to thouse in
      28             :         src/native/vm/sun/jvm.c Unless someone has a good idea how to cover
      29             :         such redundancy I leave it how it is.
      30             : 
      31             :   The ConstantPool class represents an interface to the constant pool of a
      32             :   class and is used by the annotations parser (sun.reflect.annotation.
      33             :   AnnotationParser) to get the values of the constants refered by the
      34             :   annotations.
      35             : 
      36             : *******************************************************************************/
      37             : 
      38             : #include "config.h"
      39             : 
      40             : #include <assert.h>
      41             : #include <stdint.h>
      42             : 
      43             : #include "mm/memory.hpp"
      44             : 
      45             : #include "native/jni.hpp"
      46             : #include "native/llni.hpp"
      47             : #include "native/native.hpp"
      48             : 
      49             : // FIXME
      50             : //#include "native/include/sun_reflect_ConstantPool.h"
      51             : 
      52             : #include "native/vm/reflection.hpp"
      53             : 
      54             : #include "toolbox/logging.hpp"
      55             : 
      56             : #include "vm/class.hpp"
      57             : #include "vm/exceptions.hpp"
      58             : #include "vm/javaobjects.hpp"
      59             : #include "vm/resolve.hpp"
      60             : #include "vm/string.hpp"
      61             : #include "vm/utf8.hpp"
      62             : #include "vm/vm.hpp"
      63             : 
      64             : 
      65             : // Native functions are exported as C functions.
      66             : extern "C" {
      67             : 
      68             : /*
      69             :  * Class:     sun/reflect/ConstantPool
      70             :  * Method:    getSize0
      71             :  * Signature: (Ljava/lang/Object;)I
      72             :  */
      73           0 : JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getSize0(JNIEnv *env, jobject _this, jobject jcpool)
      74             : {
      75           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
      76           0 :         return cls->cpcount;
      77             : }
      78             : 
      79             : 
      80             : /*
      81             :  * Class:     sun/reflect/ConstantPool
      82             :  * Method:    getClassAt0
      83             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
      84             :  */
      85           0 : JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
      86             : {
      87             :         constant_classref *ref;
      88           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
      89             : 
      90             :         ref = (constant_classref*)class_getconstant(
      91           0 :                 cls, index, CONSTANT_Class);
      92             : 
      93           0 :         if (ref == NULL) {
      94           0 :                 exceptions_throw_illegalargumentexception();
      95           0 :                 return NULL;
      96             :         }
      97             : 
      98           0 :         return (jclass) LLNI_classinfo_wrap(resolve_classref_eager(ref));
      99             : }
     100             : 
     101             : 
     102             : /*
     103             :  * Class:     sun/reflect/ConstantPool
     104             :  * Method:    getClassAtIfLoaded0
     105             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/Class;
     106             :  */
     107           0 : JNIEXPORT jclass JNICALL Java_sun_reflect_ConstantPool_getClassAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     108             : {
     109             :         constant_classref *ref;
     110           0 :         classinfo *c = NULL;
     111           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     112             : 
     113             :         ref = (constant_classref*)class_getconstant(
     114           0 :                 cls, index, CONSTANT_Class);
     115             : 
     116           0 :         if (ref == NULL) {
     117           0 :                 exceptions_throw_illegalargumentexception();
     118           0 :                 return NULL;
     119             :         }
     120             :         
     121           0 :         if (!resolve_classref(NULL,ref,resolveLazy,true,true,&c)) {
     122           0 :                 return NULL;
     123             :         }
     124             : 
     125           0 :         if (c == NULL || !(c->state & CLASS_LOADED)) {
     126           0 :                 return NULL;
     127             :         }
     128             :         
     129           0 :         return (jclass) LLNI_classinfo_wrap(c);
     130             : }
     131             : 
     132             : 
     133             : /*
     134             :  * Class:     sun/reflect/ConstantPool
     135             :  * Method:    getMethodAt0
     136             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
     137             :  */
     138           0 : JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     139             : {
     140             :         constant_FMIref *ref;
     141           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     142             :         
     143             :         ref = (constant_FMIref*)class_getconstant(
     144           0 :                 cls, index, CONSTANT_Methodref);
     145             :         
     146           0 :         if (ref == NULL) {
     147           0 :                 exceptions_throw_illegalargumentexception();
     148           0 :                 return NULL;
     149             :         }
     150             : 
     151             :         /* XXX: is that right? or do I have to use resolve_method_*? */
     152           0 :         java_lang_reflect_Method jlrm(ref->p.method);
     153             : 
     154           0 :         return (jobject) jlrm.get_handle();
     155             : }
     156             : 
     157             : 
     158             : /*
     159             :  * Class:     sun/reflect/ConstantPool
     160             :  * Method:    getMethodAtIfLoaded0
     161             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Member;
     162             :  */
     163           0 : JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     164             : {
     165             :         constant_FMIref *ref;
     166           0 :         classinfo *c = NULL;
     167           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     168             : 
     169             :         ref = (constant_FMIref*)class_getconstant(
     170           0 :                 cls, index, CONSTANT_Methodref);
     171             : 
     172           0 :         if (ref == NULL) {
     173           0 :                 exceptions_throw_illegalargumentexception();
     174           0 :                 return NULL;
     175             :         }
     176             : 
     177           0 :         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
     178           0 :                 return NULL;
     179             :         }
     180             : 
     181           0 :         if (c == NULL || !(c->state & CLASS_LOADED)) {
     182           0 :                 return NULL;
     183             :         }
     184             : 
     185           0 :         java_lang_reflect_Method jlrm(ref->p.method);
     186             : 
     187           0 :         return (jobject) jlrm.get_handle();
     188             : }
     189             : 
     190             : 
     191             : /*
     192             :  * Class:     sun/reflect/ConstantPool
     193             :  * Method:    getFieldAt0
     194             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
     195             :  */
     196           0 : JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     197             : {
     198             :         constant_FMIref *ref;
     199           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     200             : 
     201           0 :         ref = (constant_FMIref*) class_getconstant(cls, index, CONSTANT_Fieldref);
     202             : 
     203           0 :         if (ref == NULL) {
     204           0 :                 exceptions_throw_illegalargumentexception();
     205           0 :                 return NULL;
     206             :         }
     207             : 
     208             :         // Create a new java.lang.reflect.Field Java object.
     209           0 :         java_lang_reflect_Field jlrf(ref->p.field);
     210             : 
     211           0 :         return (jobject) jlrf.get_handle();
     212             : }
     213             : 
     214             : 
     215             : /*
     216             :  * Class:     sun/reflect/ConstantPool
     217             :  * Method:    getFieldAtIfLoaded0
     218             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/reflect/Field;
     219             :  */
     220           0 : JNIEXPORT jobject JNICALL Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     221             : {
     222             :         constant_FMIref *ref;
     223             :         classinfo *c;
     224           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     225             : 
     226           0 :         ref = (constant_FMIref*) class_getconstant(cls, index, CONSTANT_Fieldref);
     227             : 
     228           0 :         if (ref == NULL) {
     229           0 :                 exceptions_throw_illegalargumentexception();
     230           0 :                 return NULL;
     231             :         }
     232             : 
     233           0 :         if (!resolve_classref(NULL,ref->p.classref,resolveLazy,true,true,&c)) {
     234           0 :                 return NULL;
     235             :         }
     236             : 
     237           0 :         if (c == NULL || !(c->state & CLASS_LOADED)) {
     238           0 :                 return NULL;
     239             :         }
     240             : 
     241             :         // Create a new java.lang.reflect.Field Java object.
     242           0 :         java_lang_reflect_Field jlrf(ref->p.field);
     243             : 
     244           0 :         return (jobject) jlrf.get_handle();
     245             : }
     246             : 
     247             : 
     248             : /*
     249             :  * Class:     sun/reflect/ConstantPool
     250             :  * Method:    getMemberRefInfoAt0
     251             :  * Signature: (Ljava/lang/Object;I)[Ljava/lang/String;
     252             :  */
     253           0 : JNIEXPORT jobjectArray JNICALL Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     254             : {
     255           0 :         log_println("Java_sun_reflect_ConstantPool_getMemberRefInfoAt0(env=%p, jcpool=%p, index=%d): IMPLEMENT ME!", env, jcpool, index);
     256           0 :         return NULL;
     257             : }
     258             : 
     259             : 
     260             : /*
     261             :  * Class:     sun/reflect/ConstantPool
     262             :  * Method:    getIntAt0
     263             :  * Signature: (Ljava/lang/Object;I)I
     264             :  */
     265         147 : JNIEXPORT jint JNICALL Java_sun_reflect_ConstantPool_getIntAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     266             : {
     267         147 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     268             : 
     269         147 :         int32_t *ref = (int32_t*) class_getconstant(cls, index, CONSTANT_Integer);
     270             : 
     271         147 :         if (ref == NULL) {
     272           0 :                 exceptions_throw_illegalargumentexception();
     273           0 :                 return 0;
     274             :         }
     275             : 
     276         147 :         return *ref;
     277             : }
     278             : 
     279             : 
     280             : /*
     281             :  * Class:     sun/reflect/ConstantPool
     282             :  * Method:    getLongAt0
     283             :  * Signature: (Ljava/lang/Object;I)J
     284             :  */
     285          23 : JNIEXPORT jlong JNICALL Java_sun_reflect_ConstantPool_getLongAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     286             : {
     287          23 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     288             : 
     289          23 :         int64_t *ref = (int64_t*) class_getconstant(cls, index, CONSTANT_Long);
     290             : 
     291          23 :         if (ref == NULL) {
     292           0 :                 exceptions_throw_illegalargumentexception();
     293           0 :                 return 0;
     294             :         }
     295             : 
     296          23 :         return *ref;
     297             : }
     298             : 
     299             : 
     300             : /*
     301             :  * Class:     sun/reflect/ConstantPool
     302             :  * Method:    getFloatAt0
     303             :  * Signature: (Ljava/lang/Object;I)F
     304             :  */
     305          26 : JNIEXPORT float JNICALL Java_sun_reflect_ConstantPool_getFloatAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     306             : {
     307          26 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     308             : 
     309          26 :         float *ref = (float*) class_getconstant( cls, index, CONSTANT_Float);
     310             : 
     311          26 :         if (ref == NULL) {
     312           0 :                 exceptions_throw_illegalargumentexception();
     313           0 :                 return 0;
     314             :         }
     315             : 
     316          26 :         return *ref;
     317             : }
     318             : 
     319             : 
     320             : /*
     321             :  * Class:     sun/reflect/ConstantPool
     322             :  * Method:    getDoubleAt0
     323             :  * Signature: (Ljava/lang/Object;I)D
     324             :  */
     325           9 : JNIEXPORT double JNICALL Java_sun_reflect_ConstantPool_getDoubleAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     326             : {
     327           9 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     328             : 
     329           9 :         double *ref = (double*) class_getconstant(cls, index, CONSTANT_Double);
     330             : 
     331           9 :         if (ref == NULL) {
     332           0 :                 exceptions_throw_illegalargumentexception();
     333           0 :                 return 0;
     334             :         }
     335             : 
     336           9 :         return *ref;
     337             : }
     338             : 
     339             : 
     340             : /*
     341             :  * Class:     sun/reflect/ConstantPool
     342             :  * Method:    getStringAt0
     343             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
     344             :  */
     345           0 : JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getStringAt0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     346             : {
     347           0 :         Utf8String ref;
     348           0 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     349             :         
     350           0 :         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
     351             : 
     352           0 :         if (ref == NULL) {
     353           0 :                 exceptions_throw_illegalargumentexception();
     354           0 :                 return NULL;
     355             :         }
     356             : 
     357             :         /* XXX: I hope literalstring_new is the right Function. */
     358           0 :         return (jstring) JavaString::literal(ref);
     359             : }
     360             : 
     361             : 
     362             : /*
     363             :  * Class:     sun/reflect/ConstantPool
     364             :  * Method:    getUTF8At0
     365             :  * Signature: (Ljava/lang/Object;I)Ljava/lang/String;
     366             :  */
     367        1346 : JNIEXPORT jstring JNICALL Java_sun_reflect_ConstantPool_getUTF8At0(JNIEnv *env, jobject _this, jobject jcpool, jint index)
     368             : {
     369        1346 :         Utf8String ref;
     370        1346 :         classinfo *cls = LLNI_classinfo_unwrap(jcpool);
     371             : 
     372        1346 :         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
     373             : 
     374        1346 :         if (ref == NULL) {
     375           0 :                 exceptions_throw_illegalargumentexception();
     376           0 :                 return NULL;
     377             :         }
     378             : 
     379             :         /* XXX: I hope literalstring_new is the right Function. */
     380        1346 :         return (jstring) JavaString::literal(ref);
     381             : }
     382             : 
     383             : } // extern "C"
     384             : 
     385             : 
     386             : /* native methods implemented by this file ************************************/
     387             : 
     388             : static JNINativeMethod methods[] = {
     389             :         { (char*) "getSize0",             (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getSize0             },
     390             :         { (char*) "getClassAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAt0          },
     391             :         { (char*) "getClassAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/Class;",          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getClassAtIfLoaded0  },
     392             :         { (char*) "getMethodAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAt0         },
     393             :         { (char*) "getMethodAtIfLoaded0", (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Member;", (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMethodAtIfLoaded0 },
     394             :         { (char*) "getFieldAt0",          (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAt0          },
     395             :         { (char*) "getFieldAtIfLoaded0",  (char*) "(Ljava/lang/Object;I)Ljava/lang/reflect/Field;",  (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFieldAtIfLoaded0  },
     396             :         { (char*) "getMemberRefInfoAt0",  (char*) "(Ljava/lang/Object;I)[Ljava/lang/String;",        (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getMemberRefInfoAt0  },
     397             :         { (char*) "getIntAt0",            (char*) "(Ljava/lang/Object;I)I",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getIntAt0            },
     398             :         { (char*) "getLongAt0",           (char*) "(Ljava/lang/Object;I)J",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getLongAt0           },
     399             :         { (char*) "getFloatAt0",          (char*) "(Ljava/lang/Object;I)F",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getFloatAt0          },
     400             :         { (char*) "getDoubleAt0",         (char*) "(Ljava/lang/Object;I)D",                          (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getDoubleAt0         },
     401             :         { (char*) "getStringAt0",         (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getStringAt0         },
     402             :         { (char*) "getUTF8At0",           (char*) "(Ljava/lang/Object;I)Ljava/lang/String;",         (void*) (uintptr_t) &Java_sun_reflect_ConstantPool_getUTF8At0           }, 
     403             : };
     404             : 
     405             : 
     406             : /* _Jv_sun_reflect_ConstantPool_init ******************************************
     407             : 
     408             :    Register native functions.
     409             : 
     410             : *******************************************************************************/
     411             : 
     412         163 : void _Jv_sun_reflect_ConstantPool_init(void)
     413             : {
     414         163 :         Utf8String u = Utf8String::from_utf8("sun/reflect/ConstantPool");
     415             : 
     416         163 :         NativeMethods& nm = VM::get_current()->get_nativemethods();
     417         163 :         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
     418         163 : }
     419             : 
     420             : 
     421             : /*
     422             :  * These are local overrides for various environment variables in Emacs.
     423             :  * Please do not remove this and leave it at the end of the file, where
     424             :  * Emacs will automagically detect them.
     425             :  * ---------------------------------------------------------------------
     426             :  * Local variables:
     427             :  * mode: c++
     428             :  * indent-tabs-mode: t
     429             :  * c-basic-offset: 4
     430             :  * tab-width: 4
     431             :  * End:
     432             :  * vim:noexpandtab:sw=4:ts=4:
     433             :  */

Generated by: LCOV version 1.11