Showing the changes to java/pkg2/java/lang/reflect (18 of 18352 files). Show all
Unified · Split
Empty file added.
| @@ -0,0 +1,417 @@ | ||
| 1 | +implement Array_L; | |
| 2 | + | |
| 3 | +# javal v1.5 generated file: edit with care | |
| 4 | + | |
| 5 | +include "jni.m"; | |
| 6 | + jni : JNI; | |
| 7 | + ClassModule, | |
| 8 | + JString, | |
| 9 | + JArray, | |
| 10 | + JArrayI, | |
| 11 | + JArrayC, | |
| 12 | + JArrayB, | |
| 13 | + JArrayS, | |
| 14 | + JArrayJ, | |
| 15 | + JArrayF, | |
| 16 | + JArrayD, | |
| 17 | + JArrayZ, | |
| 18 | + JArrayJObject, | |
| 19 | + JArrayJClass, | |
| 20 | + JArrayJString, | |
| 21 | + JClass, | |
| 22 | + JThread, | |
| 23 | + JObject : import jni; | |
| 24 | + | |
| 25 | +#>> extra pre includes here | |
| 26 | + | |
| 27 | + ClassData, | |
| 28 | + Value : import jni; | |
| 29 | + | |
| 30 | + jldr : JavaClassLoader; | |
| 31 | + | |
| 32 | +#<< | |
| 33 | + | |
| 34 | +include "Array_L.m"; | |
| 35 | + | |
| 36 | +#>> extra post includes here | |
| 37 | + | |
| 38 | +include "reflect.m"; | |
| 39 | + refl : Reflect; | |
| 40 | + widen : import refl; | |
| 41 | + | |
| 42 | +array_class : ClassData; | |
| 43 | + | |
| 44 | +badindex := "java.lang.ArrayIndexOutOfBoundsException"; | |
| 45 | +illegalarg := "java.lang.IllegalArgumentException"; | |
| 46 | +nullptr := "java.lang.NullPointerException"; | |
| 47 | + | |
| 48 | + cast : Cast; | |
| 49 | + | |
| 50 | +#<< | |
| 51 | + | |
| 52 | +init( jni_p : JNI ) | |
| 53 | +{ | |
| 54 | + # save java native inteface mod instance | |
| 55 | + jni = jni_p; | |
| 56 | + #>>extra initialization here | |
| 57 | + array_class = jni->FindClass("inferno/vm/Array"); | |
| 58 | + | |
| 59 | + jldr = jni->jldr; | |
| 60 | + cast = jni->CastMod(); | |
| 61 | + refl = load Reflect Reflect->PATH; | |
| 62 | + if ( refl == nil ) | |
| 63 | + jni->InitError( jni->sys->sprint( "java.lang.reflect.Array: could not load %s: %r", Reflect->PATH ) ); | |
| 64 | + else | |
| 65 | + refl->init(jni); | |
| 66 | + | |
| 67 | + #<< | |
| 68 | +} | |
| 69 | + | |
| 70 | +getLength_rObject_I( p0 : JObject) : int | |
| 71 | +{#>> | |
| 72 | + # p0 - must be a Java array; if not, exceptions are thrown | |
| 73 | + # Returns the integer length of the array. | |
| 74 | + | |
| 75 | + if ( p0 == nil ) | |
| 76 | + jni->ThrowException(nullptr, ""); | |
| 77 | + if ( jni->IsArray(p0) == JNI->FALSE) | |
| 78 | + jni->ThrowException(illegalarg, "Not an array"); | |
| 79 | + return jni->GetArrayLength(cast->ToJArray(p0)); | |
| 80 | +}#<< | |
| 81 | + | |
| 82 | +get_rObject_I_rObject( p0 : JObject,p1 : int) : JObject | |
| 83 | +{#>> | |
| 84 | + # p0 - a Java object, which must be an array, else exception occurs. | |
| 85 | + # p1 - an index into the array, >= 0 and < length of array | |
| 86 | + # Returns the element at that index. If the array is of a primitive | |
| 87 | + # type, it is put in an appropriate wrapper class instance. | |
| 88 | + | |
| 89 | + checksOK(p0, p1); | |
| 90 | + | |
| 91 | + # | |
| 92 | + # Need to figure out what kind of an array this is, then get the | |
| 93 | + # requested element and convert it to an appropriate JObject. | |
| 94 | + # | |
| 95 | + (typ, elem) := GetArrayElement(p0, p1); | |
| 96 | + #(typ, elem) := jni->GetArrayElement(p0, p1); | |
| 97 | + | |
| 98 | + if ( typ == 0 ) # array of Objects. good. | |
| 99 | + return elem.Object(); | |
| 100 | + | |
| 101 | + if ( typ >= JNI->T_BOOLEAN && typ <= JNI->T_LONG ) | |
| 102 | + return refl->ValueToObject(elem); | |
| 103 | + | |
| 104 | + return nil; | |
| 105 | +}#<< | |
| 106 | + | |
| 107 | +getBoolean_rObject_I_Z( p0 : JObject,p1 : int) : int | |
| 108 | +{#>> | |
| 109 | + # p0 - a Java object, which must be an array, else exception occurs. | |
| 110 | + # p1 - an index into the array, >= 0 and < length of array | |
| 111 | + # Returns the element at that index as a boolean value. If the array | |
| 112 | + # is not an array of something that can be converted to a boolean by | |
| 113 | + # widening (and for boolean that's nothing), then getprim will throw | |
| 114 | + # an exception. | |
| 115 | + | |
| 116 | + return getprim(p0, p1, JNI->T_BOOLEAN, "boolean").Boolean(); | |
| 117 | +}#<< | |
| 118 | + | |
| 119 | +getByte_rObject_I_B( p0 : JObject,p1 : int) : int | |
| 120 | +{#>> | |
| 121 | + # See comments in getBoolean_rObject_I_Z, above | |
| 122 | + | |
| 123 | + return getprim(p0, p1, JNI->T_BYTE, "byte").Byte(); | |
| 124 | +}#<< | |
| 125 | + | |
| 126 | +getChar_rObject_I_C( p0 : JObject,p1 : int) : int | |
| 127 | +{#>> | |
| 128 | + # See comments in getBoolean_rObject_I_Z, above | |
| 129 | + | |
| 130 | + return getprim(p0, p1, JNI->T_CHAR, "char").Char(); | |
| 131 | +}#<< | |
| 132 | + | |
| 133 | +getShort_rObject_I_S( p0 : JObject,p1 : int) : int | |
| 134 | +{#>> | |
| 135 | + # See comments in getBoolean_rObject_I_Z, above | |
| 136 | + | |
| 137 | + return getprim(p0, p1, JNI->T_SHORT, "short").Short(); | |
| 138 | +}#<< | |
| 139 | + | |
| 140 | +getInt_rObject_I_I( p0 : JObject,p1 : int) : int | |
| 141 | +{#>> | |
| 142 | + # See comments in getBoolean_rObject_I_Z, above | |
| 143 | + | |
| 144 | + return getprim(p0, p1, JNI->T_INT, "int").Int(); | |
| 145 | +}#<< | |
| 146 | + | |
| 147 | +getLong_rObject_I_J( p0 : JObject,p1 : int) : big | |
| 148 | +{#>> | |
| 149 | + # See comments in getBoolean_rObject_I_Z, above | |
| 150 | + | |
| 151 | + return getprim(p0, p1, JNI->T_LONG, "long").Long(); | |
| 152 | +}#<< | |
| 153 | + | |
| 154 | +getFloat_rObject_I_F( p0 : JObject,p1 : int) : real | |
| 155 | +{#>> | |
| 156 | + # See comments in getBoolean_rObject_I_Z, above | |
| 157 | + | |
| 158 | + return getprim(p0, p1, JNI->T_FLOAT, "float").Float(); | |
| 159 | +}#<< | |
| 160 | + | |
| 161 | +getDouble_rObject_I_D( p0 : JObject,p1 : int) : real | |
| 162 | +{#>> | |
| 163 | + # See comments in getBoolean_rObject_I_Z, above | |
| 164 | + | |
| 165 | + return getprim(p0, p1, JNI->T_DOUBLE, "double").Double(); | |
| 166 | +}#<< | |
| 167 | + | |
| 168 | +set_rObject_I_rObject_V( p0 : JObject,p1 : int,p2 : JObject) | |
| 169 | +{#>> | |
| 170 | + # p0 - a Java array. If null or not an array, exception. | |
| 171 | + # p1 - index into the array. If < 0 or >= length of array, exception. | |
| 172 | + # p2 - a value to put into the array at the specified index. If p0 is | |
| 173 | + # an array of JObject, p2 must be an instance of the appropriate | |
| 174 | + # class. If p0 is an array of a primitive Java type, then p2 must | |
| 175 | + # be a wrapper of a primitive type that can be converted to the | |
| 176 | + # the array's type by widening. | |
| 177 | + | |
| 178 | + checksOK(p0, p1); | |
| 179 | + | |
| 180 | + objarray := cast->ToJArray(p0); | |
| 181 | + typ := objarray.primitive; | |
| 182 | + if ( typ == 0 ) { # an Object array | |
| 183 | + if ( jni->IsInstanceOf(p2, objarray.class) == JNI->TRUE ) { | |
| 184 | + cast->JArrayToObject(objarray).ary[p1] = p2; | |
| 185 | + return; | |
| 186 | + } | |
| 187 | + } | |
| 188 | + else if ( typ >= JNI->T_BOOLEAN && typ <= JNI->T_LONG ) { | |
| 189 | + (stype, val) := refl->GetVal(p2); | |
| 190 | + if ( stype != 0 && (val = widen(stype, typ, val)) != nil ) { | |
| 191 | + SetArrayElement(p0, p1, val); | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + } | |
| 195 | + jni->ThrowException(illegalarg, "Argument can't be cast to array element"); | |
| 196 | +}#<< | |
| 197 | + | |
| 198 | +setBoolean_rObject_I_Z_V( p0 : JObject,p1 : int,p2 : int) | |
| 199 | +{#>> | |
| 200 | + # p0[p1] = p2 | |
| 201 | + # where p2 is a boolean value, and it must be possible to convert it by | |
| 202 | + # widening to the type array p0. | |
| 203 | + | |
| 204 | + putprim(p0, p1, JNI->T_BOOLEAN, "boolean", ref Value.TBoolean(p2)); | |
| 205 | +}#<< | |
| 206 | + | |
| 207 | +setByte_rObject_I_B_V( p0 : JObject,p1 : int,p2 : int) | |
| 208 | +{#>> | |
| 209 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 210 | + | |
| 211 | + putprim(p0, p1, JNI->T_BYTE, "byte", ref Value.TByte(p2)); | |
| 212 | +}#<< | |
| 213 | + | |
| 214 | +setChar_rObject_I_C_V( p0 : JObject,p1 : int,p2 : int) | |
| 215 | +{#>> | |
| 216 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 217 | + | |
| 218 | + putprim(p0, p1, JNI->T_CHAR, "char", ref Value.TChar(p2)); | |
| 219 | +}#<< | |
| 220 | + | |
| 221 | +setShort_rObject_I_S_V( p0 : JObject,p1 : int,p2 : int) | |
| 222 | +{#>> | |
| 223 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 224 | + | |
| 225 | + putprim(p0, p1, JNI->T_SHORT, "short", ref Value.TShort(p2)); | |
| 226 | +}#<< | |
| 227 | + | |
| 228 | +setInt_rObject_I_I_V( p0 : JObject,p1 : int,p2 : int) | |
| 229 | +{#>> | |
| 230 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 231 | + | |
| 232 | + putprim(p0, p1, JNI->T_INT, "int", ref Value.TInt(p2)); | |
| 233 | +}#<< | |
| 234 | + | |
| 235 | +setLong_rObject_I_J_V( p0 : JObject,p1 : int,p2 : big) | |
| 236 | +{#>> | |
| 237 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 238 | + | |
| 239 | + putprim(p0, p1, JNI->T_LONG, "long", ref Value.TLong(p2)); | |
| 240 | +}#<< | |
| 241 | + | |
| 242 | +setFloat_rObject_I_F_V( p0 : JObject,p1 : int,p2 : real) | |
| 243 | +{#>> | |
| 244 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 245 | + | |
| 246 | + putprim(p0, p1, JNI->T_FLOAT, "float", ref Value.TFloat(p2)); | |
| 247 | +}#<< | |
| 248 | + | |
| 249 | +setDouble_rObject_I_D_V( p0 : JObject,p1 : int,p2 : real) | |
| 250 | +{#>> | |
| 251 | + # see comments for setBoolean_rObject_I_Z_V, above. | |
| 252 | + | |
| 253 | + putprim(p0, p1, JNI->T_DOUBLE, "double", ref Value.TDouble(p2)); | |
| 254 | +}#<< | |
| 255 | + | |
| 256 | +newArray(jclass: JClass, bounds: array of int): JObject | |
| 257 | +{ | |
| 258 | + # Create a Java array whose components belong to class jclass. | |
| 259 | + # jclass may be a class object representing a primitive type. | |
| 260 | + # bounds gives the length of each dimension. | |
| 261 | + | |
| 262 | + dims := len bounds; | |
| 263 | + cldata: ClassData; | |
| 264 | + | |
| 265 | + # See if the class represents a primitive type. | |
| 266 | + typ := jni->PrimitiveIndex(jclass, 1); | |
| 267 | + | |
| 268 | + # If jclass represents an array, then component type of array | |
| 269 | + # being created is component type of jclass. | |
| 270 | + s := jclass.aryname; | |
| 271 | + if(s != nil) { | |
| 272 | + # count [s in aryname | |
| 273 | + n := 1; | |
| 274 | + while(s[n] == '[') | |
| 275 | + n++; | |
| 276 | + dims += n; | |
| 277 | + typ = refl->SigToType(s[n:]); | |
| 278 | + if(typ == 0) | |
| 279 | + cldata = jni->FindClass(s[n+1:len s - 1]); | |
| 280 | + } else if(typ == 0) { # jclass is not array and not primitive | |
| 281 | + cldata = jclass.class; | |
| 282 | + } | |
| 283 | + | |
| 284 | + return cast->FromJArray(jldr->multianewarray(dims, cldata, typ, bounds)); | |
| 285 | +} | |
| 286 | + | |
| 287 | +newArray_rClass_I_rObject( p0 : JClass,p1 : int) : JObject | |
| 288 | +{#>> | |
| 289 | + if ( p0 == nil ) | |
| 290 | + jni->ThrowException(nullptr, ""); | |
| 291 | + | |
| 292 | + return newArray(p0, array [1] of { p1 }); | |
| 293 | +}#<< | |
| 294 | + | |
| 295 | +multiNewArray_rClass_aI_rObject( p0 : JClass,p1 : JArrayI) : JObject | |
| 296 | +{#>> | |
| 297 | + if ( p0 == nil || p1 == nil ) | |
| 298 | + jni->ThrowException(nullptr, ""); | |
| 299 | + | |
| 300 | + ln := len p1.ary; | |
| 301 | + | |
| 302 | + if ( ln < 1 || ln > 255 ) | |
| 303 | + jni->ThrowException(illegalarg, "dimensions out of range [1,255]"); | |
| 304 | + | |
| 305 | + return newArray(p0, p1.ary); | |
| 306 | +}#<< | |
| 307 | + | |
| 308 | +# An argument verification routine for use by native methods. The | |
| 309 | +# first argument must be non-null, and a Java array. (These conditions | |
| 310 | +# are actually check by calling getLength_rObject.) The second | |
| 311 | +# argument must be a valid index into that array. There is no return | |
| 312 | +# value: if the conditions above are not met, an exception is thrown; | |
| 313 | +# hence the routine does not return in this case. A successful return | |
| 314 | +# indicates the arguments are O.K. | |
| 315 | +# | |
| 316 | +checksOK(o : JObject, ix : int) | |
| 317 | +{ | |
| 318 | + if ( ix >= getLength_rObject_I(o) || ix < 0 ) | |
| 319 | + jni->ThrowException(badindex, "Illegal index"); | |
| 320 | + return; | |
| 321 | +} | |
| 322 | +# | |
| 323 | +# Gets element 'index' of Java array 'o' and returns it as a ref Value | |
| 324 | +# of the type corresponding to Java type index 'want'. String 's' | |
| 325 | +# is used only in creating the exception message, and should correspond | |
| 326 | +# to 'want'. o must be non-null and refer to a Java array, index must | |
| 327 | +# within range, and the array must be of a primitive type that can | |
| 328 | +# be made into the requested type by a widening conversion. If any of | |
| 329 | +# these conditions are not met, an exception is thrown. | |
| 330 | +# | |
| 331 | +getprim( o : JObject, index : int, want : int, s : string) : ref Value | |
| 332 | +{ | |
| 333 | + checksOK(o, index); | |
| 334 | + | |
| 335 | + (typ, elem) := GetArrayElement(o, index); | |
| 336 | + if ( (wider := widen(typ, want, elem)) == nil ) { | |
| 337 | + if ( s != nil ) | |
| 338 | + jni->ThrowException(illegalarg, "Can't cast array element to " + s); | |
| 339 | + else | |
| 340 | + jni->ThrowException(illegalarg, "Can't cast array element"); | |
| 341 | + } | |
| 342 | + return wider; | |
| 343 | +} | |
| 344 | +# | |
| 345 | +# Sets element 'index' of Java array 'o' to the ref Value 'val' | |
| 346 | +# of the type corresponding to Java type index 'have'. String 's' | |
| 347 | +# is used only in creating the exception message, and should correspond | |
| 348 | +# to 'have'. o must be non-null and refer to a Java array, index must | |
| 349 | +# within range, and the array must be of a primitive type that can | |
| 350 | +# be made from the provided type by a widening conversion. If any of | |
| 351 | +# these conditions are not met, an exception is thrown. | |
| 352 | +# | |
| 353 | +putprim( o : JObject, index : int, have : int, s : string, val : ref Value) | |
| 354 | +{ | |
| 355 | + checksOK(o, index); | |
| 356 | + | |
| 357 | + typ := cast->ToJArray(o).primitive; | |
| 358 | + if ( (val = widen(have, typ, val)) == nil ) { | |
| 359 | + if ( s != nil ) | |
| 360 | + jni->ThrowException(illegalarg, | |
| 361 | + "Can't put input value into " + s + " array"); | |
| 362 | + else | |
| 363 | + jni->ThrowException(illegalarg, | |
| 364 | + "Can't put input value into array"); | |
| 365 | + } | |
| 366 | + SetArrayElement(o, index, val); | |
| 367 | + #jni->SetArrayElement(o, index, val); | |
| 368 | +} | |
| 369 | +# | |
| 370 | +# Get the element at index from the Java array o and return it in | |
| 371 | +# in an appropriate member of the Value union. | |
| 372 | +# The arguments should have been verified before calling this function. | |
| 373 | +# | |
| 374 | +GetArrayElement( o : JObject, index : int ) : (int, ref Value) | |
| 375 | +{ | |
| 376 | + a := cast->ToJArray(o); | |
| 377 | + val : ref Value; | |
| 378 | + case a.primitive { | |
| 379 | + 0 => | |
| 380 | + val = ref Value.TObject(cast->JArrayToObject(a).ary[index]); | |
| 381 | + JNI->T_BOOLEAN => | |
| 382 | + val = ref Value.TBoolean(int cast->JArrayToByte(a).ary[index]); | |
| 383 | + JNI->T_BYTE => | |
| 384 | + val = ref Value.TByte(int cast->JArrayToByte(a).ary[index]); | |
| 385 | + JNI->T_CHAR => | |
| 386 | + val = ref Value.TChar(cast->JArrayToInt(a).ary[index]); | |
| 387 | + JNI->T_SHORT => | |
| 388 | + val = ref Value.TShort(cast->JArrayToInt(a).ary[index]); | |
| 389 | + JNI->T_INT => | |
| 390 | + val = ref Value.TInt(cast->JArrayToInt(a).ary[index]); | |
| 391 | + JNI->T_LONG => | |
| 392 | + val = ref Value.TLong(cast->JArrayToBig(a).ary[index]); | |
| 393 | + JNI->T_FLOAT => | |
| 394 | + val = ref Value.TFloat(cast->JArrayToReal(a).ary[index]); | |
| 395 | + JNI->T_DOUBLE => | |
| 396 | + val = ref Value.TDouble(cast->JArrayToReal(a).ary[index]); | |
| 397 | + } | |
| 398 | + return (a.primitive, val); | |
| 399 | +} | |
| 400 | +# | |
| 401 | +# Set the element at index from the Java array o to 'val', | |
| 402 | +# The arguments should have been verified before calling this function. | |
| 403 | +# | |
| 404 | +SetArrayElement( o : JObject, index : int, val : ref Value) | |
| 405 | +{ | |
| 406 | + a := cast->ToJArray(o); | |
| 407 | + pick sw := val { | |
| 408 | + TBoolean => cast->JArrayToByte(a).ary[index] = byte sw.jboolean; | |
| 409 | + TByte => cast->JArrayToByte(a).ary[index] = byte sw.jbyte; | |
| 410 | + TChar => cast->JArrayToInt(a).ary[index] = sw.jchar; | |
| 411 | + TShort => cast->JArrayToInt(a).ary[index] = sw.jshort; | |
| 412 | + TInt => cast->JArrayToInt(a).ary[index] = sw.jint; | |
| 413 | + TLong => cast->JArrayToBig(a).ary[index] = sw.jlong; | |
| 414 | + TFloat => cast->JArrayToReal(a).ary[index] = sw.jfloat; | |
| 415 | + TDouble => cast->JArrayToReal(a).ary[index] = sw.jdouble; | |
| 416 | + } | |
| 417 | +} | |
| @@ -0,0 +1,33 @@ | ||
| 1 | +# javal v1.5 generated file: edit with care | |
| 2 | + | |
| 3 | +Array_L : module | |
| 4 | +{ | |
| 5 | + Array_obj : adt | |
| 6 | + { | |
| 7 | + cl_mod : ClassModule; | |
| 8 | + }; | |
| 9 | + | |
| 10 | + init : fn( jni_p : JNI ); | |
| 11 | + getLength_rObject_I : fn( p0 : JObject) : int; | |
| 12 | + get_rObject_I_rObject : fn( p0 : JObject,p1 : int) : JObject; | |
| 13 | + getBoolean_rObject_I_Z : fn( p0 : JObject,p1 : int) : int; | |
| 14 | + getByte_rObject_I_B : fn( p0 : JObject,p1 : int) : int; | |
| 15 | + getChar_rObject_I_C : fn( p0 : JObject,p1 : int) : int; | |
| 16 | + getShort_rObject_I_S : fn( p0 : JObject,p1 : int) : int; | |
| 17 | + getInt_rObject_I_I : fn( p0 : JObject,p1 : int) : int; | |
| 18 | + getLong_rObject_I_J : fn( p0 : JObject,p1 : int) : big; | |
| 19 | + getFloat_rObject_I_F : fn( p0 : JObject,p1 : int) : real; | |
| 20 | + getDouble_rObject_I_D : fn( p0 : JObject,p1 : int) : real; | |
| 21 | + set_rObject_I_rObject_V : fn( p0 : JObject,p1 : int,p2 : JObject); | |
| 22 | + setBoolean_rObject_I_Z_V : fn( p0 : JObject,p1 : int,p2 : int); | |
| 23 | + setByte_rObject_I_B_V : fn( p0 : JObject,p1 : int,p2 : int); | |
| 24 | + setChar_rObject_I_C_V : fn( p0 : JObject,p1 : int,p2 : int); | |
| 25 | + setShort_rObject_I_S_V : fn( p0 : JObject,p1 : int,p2 : int); | |
| 26 | + setInt_rObject_I_I_V : fn( p0 : JObject,p1 : int,p2 : int); | |
| 27 | + setLong_rObject_I_J_V : fn( p0 : JObject,p1 : int,p2 : big); | |
| 28 | + setFloat_rObject_I_F_V : fn( p0 : JObject,p1 : int,p2 : real); | |
| 29 | + setDouble_rObject_I_D_V : fn( p0 : JObject,p1 : int,p2 : real); | |
| 30 | + newArray_rClass_I_rObject : fn( p0 : JClass,p1 : int) : JObject; | |
| 31 | + multiNewArray_rClass_aI_rObject : fn( p0 : JClass,p1 : JArrayI) : JObject; | |
| 32 | + | |
| 33 | +}; | |
Empty file added.
| @@ -0,0 +1,90 @@ | ||
| 1 | +implement Constructor_L; | |
| 2 | + | |
| 3 | +include "jni.m"; | |
| 4 | + jni : JNI; | |
| 5 | + ClassModule, | |
| 6 | + JString, | |
| 7 | + JArray, | |
| 8 | + JArrayI, | |
| 9 | + JArrayC, | |
| 10 | + JArrayB, | |
| 11 | + JArrayS, | |
| 12 | + JArrayJ, | |
| 13 | + JArrayF, | |
| 14 | + JArrayD, | |
| 15 | + JArrayZ, | |
| 16 | + JArrayJObject, | |
| 17 | + JArrayJClass, | |
| 18 | + JArrayJString, | |
| 19 | + JClass, | |
| 20 | + JObject : import jni; | |
| 21 | + | |
| 22 | +#>> extra pre includes here | |
| 23 | + | |
| 24 | + Value, | |
| 25 | + ClassData, | |
| 26 | + jldr : import jni; | |
| 27 | + | |
| 28 | + Object : import jldr; | |
| 29 | + | |
| 30 | +#<< | |
| 31 | + | |
| 32 | +include "Constructor_L.m"; | |
| 33 | + | |
| 34 | +#>> extra post includes here | |
| 35 | + | |
| 36 | +illegalarg := "java.lang.IllegalArgumentException"; | |
| 37 | +nullptr := "java.lang.NullPointerException"; | |
| 38 | + | |
| 39 | +include "reflect.m"; | |
| 40 | + refl : Reflect; | |
| 41 | + widen : import refl; | |
| 42 | + | |
| 43 | +#<< | |
| 44 | + | |
| 45 | +init( jni_p : JNI ) | |
| 46 | +{ | |
| 47 | + # save java native inteface mod instance | |
| 48 | + jni = jni_p; | |
| 49 | + #>>extra initialization here | |
| 50 | + refl = load Reflect Reflect->PATH; | |
| 51 | + if ( refl == nil ) | |
| 52 | + jni->InitError( jni->sys->sprint( "java.lang.reflect.Constructor: could not load %s: %r", Reflect->PATH ) ); | |
| 53 | + else | |
| 54 | + refl->init(jni); | |
| 55 | + #<< | |
| 56 | +} | |
| 57 | + | |
| 58 | +getModifiers_I( this : ref Constructor_obj) : int | |
| 59 | +{#>> | |
| 60 | + # | |
| 61 | + # Assume that lower part of slot has modifier bits. | |
| 62 | + # | |
| 63 | + return this.slot & 16rffff; | |
| 64 | +}#<< | |
| 65 | + | |
| 66 | +newInstance_aObject_rObject( this : ref Constructor_obj, p0 : JArrayJObject) : JObject | |
| 67 | +{#>> | |
| 68 | + # this - a Constructor object representing a constructor of some class | |
| 69 | + # p0 - a Java array of Objects, containing argument values for the | |
| 70 | + # the constructor | |
| 71 | + # Returns a new instance, constructed by the 'this' constructor. | |
| 72 | + # | |
| 73 | + # Check inputs and build the argument list. | |
| 74 | + # | |
| 75 | + args := refl->BuildArgList(this.parameterTypes, p0); | |
| 76 | + # | |
| 77 | + # Allocate a new object, then do the call. | |
| 78 | + # | |
| 79 | + newobj := jni->AllocObject(this.clazz.class); | |
| 80 | + | |
| 81 | + # Eventually may be able to do something like | |
| 82 | + # (result, ok) = jni->CallConstruct(newobj, this.slot, args); | |
| 83 | + | |
| 84 | + fld := this.clazz.class.initmethods[this.slot >> 16]; | |
| 85 | + (nil, ok) := jni->CallMethod(newobj, fld.field, fld.signature, args); | |
| 86 | + | |
| 87 | + if ( ok == JNI->OK ) | |
| 88 | + return newobj; | |
| 89 | + return nil; | |
| 90 | +}#<< | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +# generated file edit with care | |
| 2 | + | |
| 3 | +Constructor_L : module | |
| 4 | +{ | |
| 5 | + Constructor_obj : adt | |
| 6 | + { | |
| 7 | + cl_mod : ClassModule; | |
| 8 | + clazz : JClass; | |
| 9 | + slot : int; | |
| 10 | + parameterTypes : JArrayJClass; | |
| 11 | + exceptionTypes : JArrayJClass; | |
| 12 | + }; | |
| 13 | + | |
| 14 | + init : fn( jni_p : JNI ); | |
| 15 | + getModifiers_I : fn( this : ref Constructor_obj) : int; | |
| 16 | + newInstance_aObject_rObject : fn( this : ref Constructor_obj, p0 : JArrayJObject) : JObject; | |
| 17 | + | |
| 18 | +}; | |
Empty file added.
| @@ -0,0 +1,469 @@ | ||
| 1 | +implement Field_L; | |
| 2 | + | |
| 3 | +# javal v1.5 generated file: edit with care | |
| 4 | + | |
| 5 | +include "jni.m"; | |
| 6 | + jni : JNI; | |
| 7 | + ClassModule, | |
| 8 | + JString, | |
| 9 | + JArray, | |
| 10 | + JArrayI, | |
| 11 | + JArrayC, | |
| 12 | + JArrayB, | |
| 13 | + JArrayS, | |
| 14 | + JArrayJ, | |
| 15 | + JArrayF, | |
| 16 | + JArrayD, | |
| 17 | + JArrayZ, | |
| 18 | + JArrayJObject, | |
| 19 | + JArrayJClass, | |
| 20 | + JArrayJString, | |
| 21 | + JClass, | |
| 22 | + JThread, | |
| 23 | + JObject : import jni; | |
| 24 | + | |
| 25 | +#>> extra pre includes here | |
| 26 | + | |
| 27 | + Value, | |
| 28 | + ClassData : import jni; | |
| 29 | + jldr : JavaClassLoader; | |
| 30 | + Object, | |
| 31 | + Class : import jldr; | |
| 32 | + | |
| 33 | +#<< | |
| 34 | + | |
| 35 | +include "Field_L.m"; | |
| 36 | + | |
| 37 | +#>> extra post includes here | |
| 38 | + | |
| 39 | +include "reflect.m"; | |
| 40 | + refl : Reflect; | |
| 41 | + | |
| 42 | +STATIC, NONSTATIC : con iota; | |
| 43 | + | |
| 44 | +illegalacc, | |
| 45 | +illegalarg, | |
| 46 | +nullptr : string; | |
| 47 | + | |
| 48 | +#<< | |
| 49 | + | |
| 50 | +init( jni_p : JNI ) | |
| 51 | +{ | |
| 52 | + # save java native inteface mod instance | |
| 53 | + jni = jni_p; | |
| 54 | + #>>extra initialization here | |
| 55 | + jldr = jni->jldr; | |
| 56 | + | |
| 57 | + illegalacc = "java.lang.IllegalAccessException"; | |
| 58 | + illegalarg = "java.lang.IllegalArgumentException"; | |
| 59 | + nullptr = "java.lang.NullPointerException"; | |
| 60 | + | |
| 61 | + refl = load Reflect Reflect->PATH; | |
| 62 | + if ( refl == nil ) | |
| 63 | + jni->InitError( jni->sys->sprint( "java.lang.reflect.Field: could not load %s: %r", Reflect->PATH ) ); | |
| 64 | + else | |
| 65 | + refl->init(jni); | |
| 66 | + | |
| 67 | + #<< | |
| 68 | +} | |
| 69 | + | |
| 70 | +getModifiers_I( this : ref Field_obj) : int | |
| 71 | +{#>> | |
| 72 | + # | |
| 73 | + # Assume that lower part of slot points has modifier bits. | |
| 74 | + # | |
| 75 | + return this.slot & 16rffff; | |
| 76 | +}#<< | |
| 77 | + | |
| 78 | +get_rObject_rObject( this : ref Field_obj, p0 : JObject) : JObject | |
| 79 | +{#>> | |
| 80 | + val := getChecks(this, p0); | |
| 81 | + | |
| 82 | + typ := refl->ValType(val); | |
| 83 | + | |
| 84 | + if ( typ == 0 ) # array of Objects. good. | |
| 85 | + return val.Object(); | |
| 86 | + | |
| 87 | + if ( typ >= JNI->T_BOOLEAN && typ <= JNI->T_LONG ) | |
| 88 | + return refl->ValueToObject(val); | |
| 89 | + return nil; | |
| 90 | +}#<< | |
| 91 | + | |
| 92 | +getBoolean_rObject_Z( this : ref Field_obj, p0 : JObject) : int | |
| 93 | +{#>> | |
| 94 | + val := getChecks(this, p0); | |
| 95 | + | |
| 96 | + pick sw := val { | |
| 97 | + TBoolean => return sw.jboolean; | |
| 98 | + } | |
| 99 | + jni->ThrowException(illegalarg, "Can't convert to boolean"); | |
| 100 | + return JNI->FALSE; | |
| 101 | +}#<< | |
| 102 | + | |
| 103 | +getByte_rObject_B( this : ref Field_obj, p0 : JObject) : int | |
| 104 | +{#>> | |
| 105 | + val := getChecks(this, p0); | |
| 106 | + | |
| 107 | + pick sw := val { | |
| 108 | + TByte => return sw.jbyte; | |
| 109 | + } | |
| 110 | + jni->ThrowException(illegalarg, "Can't convert to byte"); | |
| 111 | + return JNI->FALSE; | |
| 112 | +}#<< | |
| 113 | + | |
| 114 | +getChar_rObject_C( this : ref Field_obj, p0 : JObject) : int | |
| 115 | +{#>> | |
| 116 | + val := getChecks(this, p0); | |
| 117 | + | |
| 118 | + pick sw := val { | |
| 119 | + TChar => return sw.jchar; | |
| 120 | + } | |
| 121 | + jni->ThrowException(illegalarg, "Can't convert to char"); | |
| 122 | + return 0; | |
| 123 | +}#<< | |
| 124 | + | |
| 125 | +getShort_rObject_S( this : ref Field_obj, p0 : JObject) : int | |
| 126 | +{#>> | |
| 127 | + val := getChecks(this, p0); | |
| 128 | + | |
| 129 | + pick sw := val { | |
| 130 | + TByte => return int sw.jbyte; | |
| 131 | + TShort => return sw.jshort; | |
| 132 | + } | |
| 133 | + jni->ThrowException(illegalarg, "Can't convert to short"); | |
| 134 | + return 0; | |
| 135 | +}#<< | |
| 136 | + | |
| 137 | +getInt_rObject_I( this : ref Field_obj, p0 : JObject) : int | |
| 138 | +{#>> | |
| 139 | + val := getChecks(this, p0); | |
| 140 | + | |
| 141 | + pick sw := val { | |
| 142 | + TByte => return int sw.jbyte; | |
| 143 | + TChar => return sw.jchar; | |
| 144 | + TShort => return sw.jshort; | |
| 145 | + TInt => return sw.jint; | |
| 146 | + } | |
| 147 | + jni->ThrowException(illegalarg, "Can't convert to int"); | |
| 148 | + return 0; | |
| 149 | +}#<< | |
| 150 | + | |
| 151 | +getLong_rObject_J( this : ref Field_obj, p0 : JObject) : big | |
| 152 | +{#>> | |
| 153 | + val := getChecks(this, p0); | |
| 154 | + | |
| 155 | + pick sw := val { | |
| 156 | + TByte => return big sw.jbyte; | |
| 157 | + TChar => return big sw.jchar; | |
| 158 | + TShort => return big sw.jshort; | |
| 159 | + TInt => return big sw.jint; | |
| 160 | + TLong => return sw.jlong; | |
| 161 | + } | |
| 162 | + jni->ThrowException(illegalarg, "Can't convert to long"); | |
| 163 | + return big 0; | |
| 164 | +}#<< | |
| 165 | + | |
| 166 | +getFloat_rObject_F( this : ref Field_obj, p0 : JObject) : real | |
| 167 | +{#>> | |
| 168 | + val := getChecks(this, p0); | |
| 169 | + | |
| 170 | + pick sw := val { | |
| 171 | + TByte => return real sw.jbyte; | |
| 172 | + TChar => return real sw.jchar; | |
| 173 | + TShort => return real sw.jshort; | |
| 174 | + TInt => return real sw.jint; | |
| 175 | + TLong => return real sw.jlong; | |
| 176 | + TFloat => return sw.jfloat; | |
| 177 | + } | |
| 178 | + jni->ThrowException(illegalarg, "Can't convert to float"); | |
| 179 | + return 0.0; | |
| 180 | +}#<< | |
| 181 | + | |
| 182 | +getDouble_rObject_D( this : ref Field_obj, p0 : JObject) : real | |
| 183 | +{#>> | |
| 184 | + val := getChecks(this, p0); | |
| 185 | + | |
| 186 | + pick sw := val { | |
| 187 | + TByte => return real sw.jbyte; | |
| 188 | + TChar => return real sw.jchar; | |
| 189 | + TShort => return real sw.jshort; | |
| 190 | + TInt => return real sw.jint; | |
| 191 | + TLong => return real sw.jlong; | |
| 192 | + TFloat => return sw.jfloat; | |
| 193 | + TDouble => return sw.jdouble; | |
| 194 | + } | |
| 195 | + jni->ThrowException(illegalarg, "Can't convert to double"); | |
| 196 | + return 0.0; | |
| 197 | +}#<< | |
| 198 | + | |
| 199 | +set_rObject_rObject_V( this : ref Field_obj, p0 : JObject,p1 : JObject) | |
| 200 | +{#>> | |
| 201 | + want := putChecks(this, p0); | |
| 202 | + clazz := jni->GetObjectClass(p1); | |
| 203 | + | |
| 204 | + if ( want == 0 ) { # field value is Object | |
| 205 | + p := ref Value.TObject; | |
| 206 | + if ( jni->IsAssignable(clazz, this.clazz) == JNI->TRUE ) { | |
| 207 | + p.jobj = p1; | |
| 208 | + SetField(this, p0, p); | |
| 209 | + return; | |
| 210 | + } | |
| 211 | + else { | |
| 212 | + jni->ThrowException(illegalarg, "Can't safely cast"); | |
| 213 | + return; | |
| 214 | + } | |
| 215 | + } | |
| 216 | + # The field is a primitive type. Then p1 better be a wrapper for that | |
| 217 | + # type. (Or is it okay if it's a wrapper for something that can be | |
| 218 | + # safely cast to the target type?) | |
| 219 | + # | |
| 220 | + # is p1 a Boolean object, or sub-classed from a Boolean object, | |
| 221 | + # or a String with value "true" or "false" ? | |
| 222 | + # | |
| 223 | + (typ, val) := refl->GetVal(p1); | |
| 224 | + if ( typ == 0 || (val = refl->widen(typ, want, val)) == nil ) { | |
| 225 | + jni->ThrowException(illegalarg, "Object can't be cast to needed type"); | |
| 226 | + return; | |
| 227 | + } | |
| 228 | + SetField(this, p0, val); | |
| 229 | + return; | |
| 230 | +}#<< | |
| 231 | + | |
| 232 | +setBoolean_rObject_Z_V( this : ref Field_obj, p0 : JObject,p1 : int) | |
| 233 | +{#>> | |
| 234 | + want := putChecks(this, p0); | |
| 235 | + | |
| 236 | +# | |
| 237 | +# Note that if the field's signature is "Ljava.lang.Boolean;", | |
| 238 | +# we could create an appropriate Object and assign it. | |
| 239 | +# But it isn't clear if that is what's wanted. | |
| 240 | +# A similar argument applies for the other set<Type> functions. | |
| 241 | +# | |
| 242 | + p : ref Value; | |
| 243 | + case want { | |
| 244 | + JNI->T_BOOLEAN => p = ref Value.TBoolean(p1); | |
| 245 | + * => | |
| 246 | + jni->ThrowException(illegalarg, "Can't convert from boolean"); | |
| 247 | + return; | |
| 248 | + } | |
| 249 | + SetField(this, p0, p); | |
| 250 | +}#<< | |
| 251 | + | |
| 252 | +setByte_rObject_B_V( this : ref Field_obj, p0 : JObject,p1 : int) | |
| 253 | +{#>> | |
| 254 | + want := putChecks(this, p0); | |
| 255 | + | |
| 256 | + p : ref Value; | |
| 257 | + case want { | |
| 258 | + JNI->T_BYTE => p = ref Value.TByte(p1); | |
| 259 | + JNI->T_SHORT => p = ref Value.TShort(int p1); | |
| 260 | + JNI->T_INT => p = ref Value.TInt(int p1); | |
| 261 | + JNI->T_LONG => p = ref Value.TLong(big p1); | |
| 262 | + JNI->T_FLOAT => p = ref Value.TFloat(real p1); | |
| 263 | + JNI->T_DOUBLE => p = ref Value.TDouble(real p1); | |
| 264 | + * => | |
| 265 | + jni->ThrowException(illegalarg, "Can't convert from byte"); | |
| 266 | + return; | |
| 267 | + } | |
| 268 | + SetField(this, p0, p); | |
| 269 | +}#<< | |
| 270 | + | |
| 271 | +setChar_rObject_C_V( this : ref Field_obj, p0 : JObject,p1 : int) | |
| 272 | +{#>> | |
| 273 | + want := putChecks(this, p0); | |
| 274 | + | |
| 275 | + p : ref Value; | |
| 276 | + case want { | |
| 277 | + JNI->T_CHAR => p = ref Value.TChar(p1); | |
| 278 | + * => | |
| 279 | + jni->ThrowException(illegalarg, "Can't convert from char"); | |
| 280 | + return; | |
| 281 | + } | |
| 282 | + SetField(this, p0, p); | |
| 283 | +}#<< | |
| 284 | + | |
| 285 | +setShort_rObject_S_V( this : ref Field_obj, p0 : JObject,p1 : int) | |
| 286 | +{#>> | |
| 287 | + want := putChecks(this, p0); | |
| 288 | + | |
| 289 | + p : ref Value; | |
| 290 | + case want { | |
| 291 | + JNI->T_SHORT => p = ref Value.TShort(p1); | |
| 292 | + JNI->T_INT => p = ref Value.TInt(p1); | |
| 293 | + JNI->T_LONG => p = ref Value.TLong(big p1); | |
| 294 | + JNI->T_FLOAT => p = ref Value.TFloat(real p1); | |
| 295 | + JNI->T_DOUBLE => p = ref Value.TDouble(real p1); | |
| 296 | + * => | |
| 297 | + jni->ThrowException(illegalarg, "Can't convert from short"); | |
| 298 | + return; | |
| 299 | + } | |
| 300 | + SetField(this, p0, p); | |
| 301 | +}#<< | |
| 302 | + | |
| 303 | +setInt_rObject_I_V( this : ref Field_obj, p0 : JObject,p1 : int) | |
| 304 | +{#>> | |
| 305 | + want := putChecks(this, p0); | |
| 306 | + | |
| 307 | + p : ref Value; | |
| 308 | + case want { | |
| 309 | + JNI->T_INT => p = ref Value.TInt(p1); | |
| 310 | + JNI->T_LONG => p = ref Value.TLong(big p1); | |
| 311 | + JNI->T_FLOAT => p = ref Value.TFloat(real p1); | |
| 312 | + JNI->T_DOUBLE => p = ref Value.TDouble(real p1); | |
| 313 | + * => | |
| 314 | + jni->ThrowException(illegalarg, "Can't convert from int"); | |
| 315 | + return; | |
| 316 | + } | |
| 317 | + SetField(this, p0, p); | |
| 318 | +}#<< | |
| 319 | + | |
| 320 | +setLong_rObject_J_V( this : ref Field_obj, p0 : JObject,p1 : big) | |
| 321 | +{#>> | |
| 322 | + want := putChecks(this, p0); | |
| 323 | + | |
| 324 | + p : ref Value; | |
| 325 | + case want { | |
| 326 | + JNI->T_LONG => p = ref Value.TLong(p1); | |
| 327 | + JNI->T_FLOAT => p = ref Value.TFloat(real p1); | |
| 328 | + JNI->T_DOUBLE => p = ref Value.TDouble(real p1); | |
| 329 | + * => | |
| 330 | + jni->ThrowException(illegalarg, "Can't convert from long"); | |
| 331 | + return; | |
| 332 | + } | |
| 333 | + SetField(this, p0, p); | |
| 334 | +}#<< | |
| 335 | + | |
| 336 | +setFloat_rObject_F_V( this : ref Field_obj, p0 : JObject,p1 : real) | |
| 337 | +{#>> | |
| 338 | + want := putChecks(this, p0); | |
| 339 | + | |
| 340 | + p : ref Value; | |
| 341 | + case want { | |
| 342 | + JNI->T_FLOAT => p = ref Value.TFloat(p1); | |
| 343 | + JNI->T_DOUBLE => p = ref Value.TDouble(p1); | |
| 344 | + * => | |
| 345 | + jni->ThrowException(illegalarg, "Can't convert from float"); | |
| 346 | + return; | |
| 347 | + } | |
| 348 | + SetField(this, p0, p); | |
| 349 | +}#<< | |
| 350 | + | |
| 351 | +setDouble_rObject_D_V( this : ref Field_obj, p0 : JObject,p1 : real) | |
| 352 | +{#>> | |
| 353 | + want := putChecks(this, p0); | |
| 354 | + | |
| 355 | + p : ref Value; | |
| 356 | + case want { | |
| 357 | + JNI->T_DOUBLE => p = ref Value.TDouble(p1); | |
| 358 | + * => | |
| 359 | + jni->ThrowException(illegalarg, "Can't convert from double"); | |
| 360 | + return; | |
| 361 | + } | |
| 362 | + SetField(this, p0, p); | |
| 363 | +}#<< | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | +getChecks( this : ref Field_obj, p0 : JObject) : ref Value | |
| 368 | +{ | |
| 369 | + if ( (this.slot & jldr->ACC_STATIC) == 0 && p0 == nil ) | |
| 370 | + jni->ThrowException(nullptr, "reading field of null Object"); | |
| 371 | + | |
| 372 | + # See if the target object is the same as the one declaring the field. | |
| 373 | + # this.clazz.class_data is ref to a classloader.Class adt | |
| 374 | + | |
| 375 | + cldata := this.clazz.class; | |
| 376 | + if ( (this.slot & jldr->ACC_STATIC) == 0 && jni->IsInstanceOf(p0, cldata) != JNI->TRUE ) { | |
| 377 | + jni->ThrowException(illegalarg, | |
| 378 | + "Object is not an instance of this class"); | |
| 379 | + return nil; | |
| 380 | + } | |
| 381 | + | |
| 382 | + if ( (this.slot & jldr->ACC_PUBLIC) == 0 ) | |
| 383 | + jni->ThrowException(illegalacc, "reading non-public field"); | |
| 384 | + | |
| 385 | + if ( this.slot & jldr->ACC_STATIC ) | |
| 386 | + return GetStat(cldata, this.slot); | |
| 387 | + else | |
| 388 | + return GetField(p0, this.slot); | |
| 389 | +} | |
| 390 | + | |
| 391 | +putChecks( this : ref Field_obj, p0 : JObject) : int | |
| 392 | +{ | |
| 393 | + if ( (this.slot & jldr->ACC_STATIC) == 0 && p0 == nil ) | |
| 394 | + jni->ThrowException(nullptr, "writing field of null Object"); | |
| 395 | + | |
| 396 | + # See if the target object is the same as the one declaring the field. | |
| 397 | + # this.clazz.class_data is ref to a classloader.Class adt | |
| 398 | + | |
| 399 | + cldata := this.clazz.class; | |
| 400 | + if ( (this.slot & jldr->ACC_STATIC) == 0 && jni->IsInstanceOf(p0, cldata) != JNI->TRUE ) { | |
| 401 | + jni->ThrowException(illegalarg, | |
| 402 | + "Object is not an instance of this class"); | |
| 403 | + return 0; | |
| 404 | + } | |
| 405 | + | |
| 406 | + if ( (this.slot & jldr->ACC_PUBLIC) == 0 || (this.slot & jldr->ACC_FINAL) != 0 ) | |
| 407 | + jni->ThrowException(illegalacc, "writing non-public or final field"); | |
| 408 | + | |
| 409 | + if ( this.slot & jldr->ACC_STATIC ) | |
| 410 | + return refl->SigToType(cldata.staticdata[this.slot >> 16].signature); | |
| 411 | + else { | |
| 412 | + flds := cldata.objectdata; | |
| 413 | + for ( i := (this.slot >> 16); i > 0; i--) | |
| 414 | + flds = tl flds; | |
| 415 | + return refl->SigToType((hd flds).signature); | |
| 416 | + } | |
| 417 | + return 0; | |
| 418 | +} | |
| 419 | + | |
| 420 | +checkMemberAccess(which : int) | |
| 421 | +{ | |
| 422 | + return; | |
| 423 | +} | |
| 424 | + | |
| 425 | +SetField(this : ref Field_obj, o : JObject, v : ref Value) | |
| 426 | +{ | |
| 427 | + cldata := this.clazz.class; | |
| 428 | + if ( (this.slot & jldr->ACC_STATIC) != 0 ) { | |
| 429 | + fld := cldata.staticdata[this.slot >> 16]; | |
| 430 | + jni->SetStaticField(cldata, fld.field, v); | |
| 431 | + } | |
| 432 | + else { | |
| 433 | + flds := cldata.objectdata; | |
| 434 | + for ( i := (this.slot >> 16); i > 0; i-- ) | |
| 435 | + flds = tl flds; | |
| 436 | + fld := hd flds; | |
| 437 | + jni->SetObjField(o, fld.field, v); | |
| 438 | + } | |
| 439 | +} | |
| 440 | + | |
| 441 | +widen(have, want : string, val : ref Value) : ref Value | |
| 442 | +{ | |
| 443 | + if ( have == want ) | |
| 444 | + return val; | |
| 445 | + havetype := refl->SigToType(have); | |
| 446 | + wanttype := refl->SigToType(want); | |
| 447 | + return refl->widen(havetype, wanttype, val); | |
| 448 | +} | |
| 449 | +# | |
| 450 | +# Eventually JNI may provide a funtions to do this more directly. | |
| 451 | +# | |
| 452 | +GetStat(cldata : ClassData, slot : int) : ref Value | |
| 453 | +{ | |
| 454 | + fld := cldata.staticdata[slot >> 16]; | |
| 455 | + return jni->GetStaticField(cldata, fld.field, fld.signature); | |
| 456 | +} | |
| 457 | +# | |
| 458 | +# Eventually JNI may provide a funtions to do this more directly. | |
| 459 | +# It would probably know what to do about privatemethods, if anything. | |
| 460 | +# | |
| 461 | +GetField(obj : JObject, slot : int) : ref Value | |
| 462 | +{ | |
| 463 | + flds := obj.class().objectdata; | |
| 464 | + for ( i := (slot >> 16); i > 0; i-- ) | |
| 465 | + flds = tl flds; | |
| 466 | + fld := hd flds; | |
| 467 | + return jni->GetObjField(obj, fld.field, fld.signature); | |
| 468 | +} | |
| 469 | + | |
| @@ -0,0 +1,35 @@ | ||
| 1 | +# javal v1.5 generated file: edit with care | |
| 2 | + | |
| 3 | +Field_L : module | |
| 4 | +{ | |
| 5 | + Field_obj : adt | |
| 6 | + { | |
| 7 | + cl_mod : ClassModule; | |
| 8 | + clazz : JClass; | |
| 9 | + slot : int; | |
| 10 | + name : JString; | |
| 11 | + type_j : JClass; | |
| 12 | + }; | |
| 13 | + | |
| 14 | + init : fn( jni_p : JNI ); | |
| 15 | + getModifiers_I : fn( this : ref Field_obj) : int; | |
| 16 | + get_rObject_rObject : fn( this : ref Field_obj, p0 : JObject) : JObject; | |
| 17 | + getBoolean_rObject_Z : fn( this : ref Field_obj, p0 : JObject) : int; | |
| 18 | + getByte_rObject_B : fn( this : ref Field_obj, p0 : JObject) : int; | |
| 19 | + getChar_rObject_C : fn( this : ref Field_obj, p0 : JObject) : int; | |
| 20 | + getShort_rObject_S : fn( this : ref Field_obj, p0 : JObject) : int; | |
| 21 | + getInt_rObject_I : fn( this : ref Field_obj, p0 : JObject) : int; | |
| 22 | + getLong_rObject_J : fn( this : ref Field_obj, p0 : JObject) : big; | |
| 23 | + getFloat_rObject_F : fn( this : ref Field_obj, p0 : JObject) : real; | |
| 24 | + getDouble_rObject_D : fn( this : ref Field_obj, p0 : JObject) : real; | |
| 25 | + set_rObject_rObject_V : fn( this : ref Field_obj, p0 : JObject,p1 : JObject); | |
| 26 | + setBoolean_rObject_Z_V : fn( this : ref Field_obj, p0 : JObject,p1 : int); | |
| 27 | + setByte_rObject_B_V : fn( this : ref Field_obj, p0 : JObject,p1 : int); | |
| 28 | + setChar_rObject_C_V : fn( this : ref Field_obj, p0 : JObject,p1 : int); | |
| 29 | + setShort_rObject_S_V : fn( this : ref Field_obj, p0 : JObject,p1 : int); | |
| 30 | + setInt_rObject_I_V : fn( this : ref Field_obj, p0 : JObject,p1 : int); | |
| 31 | + setLong_rObject_J_V : fn( this : ref Field_obj, p0 : JObject,p1 : big); | |
| 32 | + setFloat_rObject_F_V : fn( this : ref Field_obj, p0 : JObject,p1 : real); | |
| 33 | + setDouble_rObject_D_V : fn( this : ref Field_obj, p0 : JObject,p1 : real); | |
| 34 | + | |
| 35 | +}; | |
Empty file added.
Empty file added.
Empty file added.
| @@ -0,0 +1,117 @@ | ||
| 1 | +implement Method_L; | |
| 2 | + | |
| 3 | +include "jni.m"; | |
| 4 | + jni : JNI; | |
| 5 | + ClassModule, | |
| 6 | + JString, | |
| 7 | + JArray, | |
| 8 | + JArrayI, | |
| 9 | + JArrayC, | |
| 10 | + JArrayB, | |
| 11 | + JArrayS, | |
| 12 | + JArrayJ, | |
| 13 | + JArrayF, | |
| 14 | + JArrayD, | |
| 15 | + JArrayZ, | |
| 16 | + JArrayJObject, | |
| 17 | + JArrayJClass, | |
| 18 | + JArrayJString, | |
| 19 | + JClass, | |
| 20 | + JObject : import jni; | |
| 21 | + | |
| 22 | +#>> extra pre includes here | |
| 23 | + | |
| 24 | + Value, | |
| 25 | + ClassData, | |
| 26 | + jldr : import jni; | |
| 27 | + | |
| 28 | + Object : import jldr; | |
| 29 | + | |
| 30 | +#<< | |
| 31 | + | |
| 32 | +include "Method_L.m"; | |
| 33 | + | |
| 34 | +#>> extra post includes here | |
| 35 | + | |
| 36 | +illegalarg := "java.lang.IllegalArgumentException"; | |
| 37 | +nullptr := "java.lang.NullPointerException"; | |
| 38 | + | |
| 39 | +include "reflect.m"; | |
| 40 | + refl : Reflect; | |
| 41 | + widen : import refl; | |
| 42 | + | |
| 43 | +#<< | |
| 44 | + | |
| 45 | +init( jni_p : JNI ) | |
| 46 | +{ | |
| 47 | + # save java native inteface mod instance | |
| 48 | + jni = jni_p; | |
| 49 | + #>>extra initialization here | |
| 50 | + refl = load Reflect Reflect->PATH; | |
| 51 | + if ( refl == nil ) | |
| 52 | + jni->InitError( jni->sys->sprint( "java.lang.reflect.Method: could not load %s: %r", Reflect->PATH ) ); | |
| 53 | + else | |
| 54 | + refl->init(jni); | |
| 55 | + #<< | |
| 56 | +} | |
| 57 | + | |
| 58 | +getModifiers_I( this : ref Method_obj) : int | |
| 59 | +{#>> | |
| 60 | + # | |
| 61 | + # Assume that lower part of slot has modifier bits. | |
| 62 | + # | |
| 63 | + return this.slot & 16rffff; | |
| 64 | +}#<< | |
| 65 | + | |
| 66 | +invoke_rObject_aObject_rObject( this : ref Method_obj, p0 : JObject,p1 : JArrayJObject) : JObject | |
| 67 | +{#>> | |
| 68 | + # this - a Method object. The corresponding method should be invoked for | |
| 69 | + # p0 - an Object which should contain that method, with | |
| 70 | + # p1 - a Java array of Objects that are the arguments to the method. | |
| 71 | + # Can be null if the method has no arguments. | |
| 72 | + # Returns an object reflecting the value returned by the method call. | |
| 73 | + # | |
| 74 | + # First do some checking. If 'this' is a static method, p0 is ignored, | |
| 75 | + # otherwise it must an instance of the class in which the method is | |
| 76 | + # declared. | |
| 77 | + # | |
| 78 | + if ( (this.slot & JNI->ACC_STATIC) == 0 ) { # not static | |
| 79 | + if ( p0 == nil ) | |
| 80 | + jni->ThrowException(nullptr, "Must supply an instance"); | |
| 81 | + if ( jni->IsInstanceOf(p0, this.clazz.class) == JNI->FALSE ) | |
| 82 | + jni->ThrowException(illegalarg, "Method not in object"); | |
| 83 | + } | |
| 84 | + # | |
| 85 | + # Check inputs and build the argument list. | |
| 86 | + # | |
| 87 | + args := refl->BuildArgList(this.parameterTypes, p1); | |
| 88 | + # | |
| 89 | + # Do the call. | |
| 90 | + # | |
| 91 | + result : ref Value; | |
| 92 | + ok : int; | |
| 93 | + if ( this.slot & JNI->ACC_STATIC ) { | |
| 94 | + fld := this.clazz.class.staticmethods[this.slot >> 16]; | |
| 95 | + (result, ok) = jni->CallStatic(this.clazz.class, fld.field, | |
| 96 | + fld.signature, args); | |
| 97 | + } | |
| 98 | + else { | |
| 99 | + fld : ref JavaClassLoader->Field; | |
| 100 | + if ( (this.slot & JNI->ACC_PRIVATE) != 0 ) | |
| 101 | + fld = p0.class().privatemethods[this.slot >> 16]; | |
| 102 | + else | |
| 103 | + fld = p0.class().virtualmethods[this.slot >> 16].field; | |
| 104 | + (result, ok) = jni->CallMethod(p0, fld.field, fld.signature, args); | |
| 105 | + } | |
| 106 | + # | |
| 107 | + # If necessary, wrap the answer up as an Object. | |
| 108 | + # | |
| 109 | + if ( ok == JNI->OK && result != nil ) { | |
| 110 | + typ := refl->ValType(result); | |
| 111 | + if ( typ >= JNI->T_BOOLEAN && typ <= JNI->T_LONG ) | |
| 112 | + return refl->ValueToObject(result); | |
| 113 | + else | |
| 114 | + return result.Object(); | |
| 115 | + } | |
| 116 | + return nil; | |
| 117 | +}#<< | |
| @@ -0,0 +1,20 @@ | ||
| 1 | +# generated file edit with care | |
| 2 | + | |
| 3 | +Method_L : module | |
| 4 | +{ | |
| 5 | + Method_obj : adt | |
| 6 | + { | |
| 7 | + cl_mod : ClassModule; | |
| 8 | + clazz : JClass; | |
| 9 | + slot : int; | |
| 10 | + name : JString; | |
| 11 | + returnType : JClass; | |
| 12 | + parameterTypes : JArrayJClass; | |
| 13 | + exceptionTypes : JArrayJClass; | |
| 14 | + }; | |
| 15 | + | |
| 16 | + init : fn( jni_p : JNI ); | |
| 17 | + getModifiers_I : fn( this : ref Method_obj) : int; | |
| 18 | + invoke_rObject_aObject_rObject : fn( this : ref Method_obj, p0 : JObject,p1 : JArrayJObject) : JObject; | |
| 19 | + | |
| 20 | +}; | |
Empty file added.
| @@ -0,0 +1,46 @@ | ||
| 1 | +<../../../../../mkconfig | |
| 2 | + | |
| 3 | +TARG=\ | |
| 4 | + Array.dis\ | |
| 5 | + Constructor.dis\ | |
| 6 | + Field.dis\ | |
| 7 | +# for InvocationTargetException.dis\ | |
| 8 | + esaAWjdsByqq9cEPQZTRT.dis\ | |
| 9 | + Member.dis\ | |
| 10 | + Method.dis\ | |
| 11 | + Modifier.dis\ | |
| 12 | +# Limbo code\ | |
| 13 | + Array_L.dis\ | |
| 14 | + Constructor_L.dis\ | |
| 15 | + Field_L.dis\ | |
| 16 | + Method_L.dis\ | |
| 17 | + reflect.dis\ | |
| 18 | + | |
| 19 | +MODULES=\ | |
| 20 | + $ROOT/java/module/classloader.m\ | |
| 21 | + $ROOT/java/module/jni.m\ | |
| 22 | + $ROOT/java/module/cast.m\ | |
| 23 | + Array_L.m\ | |
| 24 | + Constructor_L.m\ | |
| 25 | + Field_L.m\ | |
| 26 | + Method_L.m\ | |
| 27 | + reflect.m\ | |
| 28 | + | |
| 29 | +SYSMODULES=\ | |
| 30 | + draw.m\ | |
| 31 | + hash.m\ | |
| 32 | + keyring.m\ | |
| 33 | + loader.m\ | |
| 34 | + math.m\ | |
| 35 | + string.m\ | |
| 36 | + sys.m\ | |
| 37 | + | |
| 38 | +DISBIN=$ROOT/java/dis/java/java/lang/reflect | |
| 39 | + | |
| 40 | +<$ROOT/mkfiles/mkjava | |
| 41 | + | |
| 42 | +reflect.dis: reflect.b | |
| 43 | + limbo $LIMBOINCL -gw reflect.b | |
| 44 | + | |
| 45 | +esaAWjdsByqq9cEPQZTRT.dis: InvocationTargetException.class | |
| 46 | + j2d InvocationTargetException.class | |
| @@ -0,0 +1,327 @@ | ||
| 1 | +implement Reflect; | |
| 2 | + | |
| 3 | +include "jni.m"; | |
| 4 | + jni : JNI; | |
| 5 | + cast : Cast; | |
| 6 | + | |
| 7 | + JObject, | |
| 8 | + JClass, | |
| 9 | + JArrayJClass, | |
| 10 | + JArrayJObject, | |
| 11 | + Value : import jni; | |
| 12 | + OK, | |
| 13 | + ERR, | |
| 14 | + TRUE, | |
| 15 | + FALSE, | |
| 16 | + #T_OBJECT, T_ARRAY, | |
| 17 | + T_BOOLEAN, | |
| 18 | + T_BYTE, | |
| 19 | + T_CHAR, | |
| 20 | + T_SHORT, | |
| 21 | + T_INT, | |
| 22 | + T_LONG, | |
| 23 | + T_FLOAT, | |
| 24 | + T_DOUBLE : import JNI; | |
| 25 | + | |
| 26 | + ClassData : import JNI; | |
| 27 | + | |
| 28 | +include "reflect.m"; | |
| 29 | + | |
| 30 | +wrapper_class := array[T_LONG-T_BOOLEAN+1] of ClassData; | |
| 31 | +wrapper_name := array[] of { "Boolean", | |
| 32 | + "Character", | |
| 33 | + "Float", | |
| 34 | + "Double", | |
| 35 | + "Byte", | |
| 36 | + "Short", | |
| 37 | + "Integer", | |
| 38 | + "Long" }; | |
| 39 | +type_string := "ZCFDBSIJ"; | |
| 40 | + | |
| 41 | +illegalarg := "java.lang.IllegalArgumentException"; | |
| 42 | +nullptr := "java.lang.NullPointerException"; | |
| 43 | + | |
| 44 | +init(jni_m : JNI) { | |
| 45 | + jni = jni_m; | |
| 46 | + for ( i := 0; i <= T_LONG - T_BOOLEAN; i++ ) | |
| 47 | + wrapper_class[i] = jni->FindClass("java/lang/" + wrapper_name[i]); | |
| 48 | + cast = jni->CastMod(); | |
| 49 | +} | |
| 50 | + | |
| 51 | +widen(have, want : int, val : ref Value) : ref Value | |
| 52 | +{ | |
| 53 | + if ( have == want ) | |
| 54 | + return val; | |
| 55 | + havetype := have; | |
| 56 | + wanttype := want; | |
| 57 | + valint : int; | |
| 58 | + valbig : big; | |
| 59 | + valreal : real; | |
| 60 | + if ( havetype == 0 || wanttype == 0 ) | |
| 61 | + return nil; | |
| 62 | + pick sw := val { | |
| 63 | + TByte => | |
| 64 | + valint = int sw.jbyte; | |
| 65 | + valbig = big valint; | |
| 66 | + valreal = real valint; | |
| 67 | + TShort => | |
| 68 | + valint = sw.jshort; | |
| 69 | + valbig = big valint; | |
| 70 | + valreal = real valint; | |
| 71 | + TChar => | |
| 72 | + valint = sw.jchar; | |
| 73 | + valbig = big valint; | |
| 74 | + valreal = real valint; | |
| 75 | + TInt => | |
| 76 | + valint = sw.jint; | |
| 77 | + valbig = big valint; | |
| 78 | + valreal = real valint; | |
| 79 | + TLong => | |
| 80 | + valbig = sw.jlong; | |
| 81 | + valreal = real valbig; | |
| 82 | + TFloat => | |
| 83 | + valreal = sw.jfloat; | |
| 84 | + } | |
| 85 | + case ( wanttype ) { | |
| 86 | + T_BOOLEAN or T_BYTE or T_CHAR => | |
| 87 | + return nil; | |
| 88 | + T_SHORT => | |
| 89 | + case ( havetype ) { | |
| 90 | + T_BYTE => | |
| 91 | + sw := ref Value.TShort; | |
| 92 | + sw.jshort = valint; | |
| 93 | + val = sw; | |
| 94 | + * => | |
| 95 | + return nil; | |
| 96 | + } | |
| 97 | + T_INT => | |
| 98 | + case ( havetype ) { | |
| 99 | + T_BYTE or T_SHORT or T_CHAR => | |
| 100 | + sw := ref Value.TInt; | |
| 101 | + sw.jint = valint; | |
| 102 | + val = sw; | |
| 103 | + * => | |
| 104 | + return nil; | |
| 105 | + } | |
| 106 | + T_LONG => | |
| 107 | + case ( havetype ) { | |
| 108 | + T_BYTE or T_SHORT or T_CHAR or T_INT => | |
| 109 | + sw := ref Value.TLong; | |
| 110 | + sw.jlong = valbig; | |
| 111 | + val = sw; | |
| 112 | + * => | |
| 113 | + return nil; | |
| 114 | + } | |
| 115 | + T_FLOAT => | |
| 116 | + case ( havetype ) { | |
| 117 | + T_BYTE or T_SHORT or T_CHAR or T_INT or T_LONG => | |
| 118 | + sw := ref Value.TFloat; | |
| 119 | + sw.jfloat = valreal; | |
| 120 | + val = sw; | |
| 121 | + * => | |
| 122 | + return nil; | |
| 123 | + } | |
| 124 | + T_DOUBLE => | |
| 125 | + case ( havetype ) { | |
| 126 | + T_BYTE or T_SHORT or T_CHAR or T_INT or T_LONG or T_FLOAT => | |
| 127 | + sw := ref Value.TDouble; | |
| 128 | + sw.jdouble = valreal; | |
| 129 | + val = sw; | |
| 130 | + * => | |
| 131 | + return nil; | |
| 132 | + } | |
| 133 | + } | |
| 134 | + return val; | |
| 135 | +} | |
| 136 | +BuildArgList(parms : JArrayJClass, arg : JArrayJObject) : array of ref Value | |
| 137 | +{ | |
| 138 | + # | |
| 139 | + # First do some checking. | |
| 140 | + # | |
| 141 | + l := 0; | |
| 142 | + formal: array of JClass; | |
| 143 | + actual: array of JObject; | |
| 144 | + args: array of ref Value; | |
| 145 | + if ( parms != nil ) { | |
| 146 | + if ( arg == nil ) | |
| 147 | + jni->ThrowException(nullptr, "constructor requires parameters"); | |
| 148 | + if ( len parms.ary != len arg.ary ) | |
| 149 | + jni->ThrowException(illegalarg, "Wrong number of args"); | |
| 150 | + l = len parms.ary; | |
| 151 | + formal = parms.ary; # array of JClass | |
| 152 | + actual = arg.ary; # array of JObject | |
| 153 | + args = array[l] of ref Value; | |
| 154 | + } | |
| 155 | + else # There are no formal parameters | |
| 156 | + if ( arg != nil && len arg.ary != 0 ) # But there are actual arguments | |
| 157 | + jni->ThrowException(illegalarg, "Wrong number of args"); | |
| 158 | + | |
| 159 | + # | |
| 160 | + # March through the arguments, checking for type as we go. | |
| 161 | + # | |
| 162 | + for ( i := 0; i < l; i++ ) { | |
| 163 | + # | |
| 164 | + # Is the formal parameter a primitive? | |
| 165 | + # | |
| 166 | + if ( (want := jni->PrimitiveIndex(formal[i], 1)) > 0 ) { | |
| 167 | + # | |
| 168 | + # Actual parameter should be (derived from) a wrapper, and | |
| 169 | + # be able to be widened to the formal parameter type. | |
| 170 | + # | |
| 171 | + (typ, parmval) := GetVal(actual[i]); | |
| 172 | + if ( (val := widen(want, typ, parmval)) != nil ) | |
| 173 | + args[i] = val; | |
| 174 | + else | |
| 175 | + jni->ThrowException(illegalarg, "Argument " + string (i+1) + | |
| 176 | + ", can't cast"); | |
| 177 | + } | |
| 178 | + else if ( jni->IsInstanceOf(actual[i], formal[i].class) == JNI->TRUE ) { | |
| 179 | + val := ref Value.TObject; | |
| 180 | + val.jobj = actual[i]; | |
| 181 | + args[i] = val; | |
| 182 | + } | |
| 183 | + else | |
| 184 | + jni->ThrowException(illegalarg, "Argument " + string (i+1) + | |
| 185 | + " wrong type"); | |
| 186 | + } | |
| 187 | + return args; | |
| 188 | +} | |
| 189 | +# | |
| 190 | +# Return a JClass corresponding to the Java type t. | |
| 191 | +# | |
| 192 | +Class(t : int) : JClass | |
| 193 | +{ | |
| 194 | + if ( t < T_BOOLEAN || t > T_LONG ) | |
| 195 | + return nil; | |
| 196 | + return jni->GetClassObject(wrapper_class[t - T_BOOLEAN]); | |
| 197 | +} | |
| 198 | +# | |
| 199 | +# Return a Class Object corresponding to the Java type t. | |
| 200 | +# | |
| 201 | +ClassObject(t : int) : JObject | |
| 202 | +{ | |
| 203 | + return cast->FromJClass(Class(t)); | |
| 204 | +} | |
| 205 | +# | |
| 206 | +# Given the java type, get the Class object representing this | |
| 207 | +# type, and return as a Value. | |
| 208 | +# | |
| 209 | +GetPrimitiveClass(typ : int) : ref Value | |
| 210 | +{ | |
| 211 | + return jni->GetStaticField(wrapper_class[typ - T_BOOLEAN], "TYPE", | |
| 212 | + "Ljava/lang/Class;"); | |
| 213 | +} | |
| 214 | +# | |
| 215 | +# If the Object o is a wrapper for a Java primitive type, return the | |
| 216 | +# primitive value (in a Value adt) and the type index. | |
| 217 | +# Otherwise, return 0 and a nil Value. | |
| 218 | +# | |
| 219 | +GetVal(o : JObject) : (int, ref Value) | |
| 220 | +{ | |
| 221 | + j := T_BOOLEAN; | |
| 222 | + for ( i := j; i <= T_LONG; i++ ) { | |
| 223 | + if ( jni->IsInstanceOf(o, wrapper_class[i-j]) == TRUE ) { | |
| 224 | + val := jni->GetObjField(o, "value", type_string[i-j:i-j+1]); | |
| 225 | + return (i, val); | |
| 226 | + } | |
| 227 | + } | |
| 228 | + return (0, nil); | |
| 229 | +} | |
| 230 | +# | |
| 231 | +# Convert a signature to a java type index. | |
| 232 | +# | |
| 233 | +SigToType(sig : string) : int | |
| 234 | +{ | |
| 235 | + index : int; | |
| 236 | + case sig[0] { | |
| 237 | + 'Z' => | |
| 238 | + index = T_BOOLEAN; | |
| 239 | + 'B' => | |
| 240 | + index = T_BYTE; | |
| 241 | + 'C' => | |
| 242 | + index = T_CHAR; | |
| 243 | + 'S' => | |
| 244 | + index = T_SHORT; | |
| 245 | + 'I' => | |
| 246 | + index = T_INT; | |
| 247 | + 'J' => | |
| 248 | + index = T_LONG; | |
| 249 | + 'F' => | |
| 250 | + index = T_FLOAT; | |
| 251 | + 'D' => | |
| 252 | + index = T_DOUBLE; | |
| 253 | + * => | |
| 254 | + index = 0; | |
| 255 | + } | |
| 256 | + return index; | |
| 257 | +} | |
| 258 | +# | |
| 259 | +# Convert a java type index to a signature. | |
| 260 | +# | |
| 261 | +TypeToSig(typ : int) : string | |
| 262 | +{ | |
| 263 | + if ( typ < T_BOOLEAN || typ > T_LONG ) | |
| 264 | + return nil; | |
| 265 | + return type_string[typ-T_BOOLEAN:typ-T_BOOLEAN+1]; | |
| 266 | +} | |
| 267 | +# | |
| 268 | +# Convert a Java type character to its corresponding type name. | |
| 269 | +# | |
| 270 | +TypeCharToName(c : int) : string | |
| 271 | +{ | |
| 272 | + tname : string; | |
| 273 | + case c { | |
| 274 | + 'Z' => | |
| 275 | + tname = "boolean"; | |
| 276 | + 'B' => | |
| 277 | + tname = "byte"; | |
| 278 | + 'C' => | |
| 279 | + tname = "char"; | |
| 280 | + 'S' => | |
| 281 | + tname = "short"; | |
| 282 | + 'I' => | |
| 283 | + tname = "int"; | |
| 284 | + 'J' => | |
| 285 | + tname = "long"; | |
| 286 | + 'F' => | |
| 287 | + tname = "float"; | |
| 288 | + 'D' => | |
| 289 | + tname = "double"; | |
| 290 | + * => | |
| 291 | + tname = "blah"; | |
| 292 | + } | |
| 293 | + return tname; | |
| 294 | +} | |
| 295 | +# | |
| 296 | +# Calculate the type index of the quantity in val. If it isn't a | |
| 297 | +# Java primitive type, return 0; | |
| 298 | +# | |
| 299 | +ValType(val : ref Value) : int | |
| 300 | +{ | |
| 301 | + pick sw := val { | |
| 302 | + TBoolean => return T_BOOLEAN; | |
| 303 | + TByte => return T_BYTE; | |
| 304 | + TChar => return T_CHAR; | |
| 305 | + TShort => return T_SHORT; | |
| 306 | + TInt => return T_INT; | |
| 307 | + TLong => return T_LONG; | |
| 308 | + TFloat => return T_FLOAT; | |
| 309 | + TDouble => return T_DOUBLE; | |
| 310 | + * => return 0; | |
| 311 | + } | |
| 312 | +} | |
| 313 | +# | |
| 314 | +# Create a wrapper Object for the primitive Java value corresponding | |
| 315 | +# to the argument. | |
| 316 | +# | |
| 317 | +ValueToObject(val : ref Value) : JObject | |
| 318 | +{ | |
| 319 | + typ := ValType(val); | |
| 320 | + j := T_BOOLEAN; | |
| 321 | + jobj := jni->AllocObject(wrapper_class[typ - j]); | |
| 322 | + sig := "(" + type_string[typ-j:typ-j+1] + ")V"; | |
| 323 | + (nil, ok) := jni->CallMethod(jobj, "<init>", sig, array[] of { val }); | |
| 324 | + if ( ok == OK ) | |
| 325 | + return jobj; | |
| 326 | + return nil; | |
| 327 | +} | |
| @@ -0,0 +1,19 @@ | ||
| 1 | +Reflect : module { | |
| 2 | + | |
| 3 | + PATH : con "/dis/java/java/lang/reflect/reflect.dis"; | |
| 4 | + | |
| 5 | + init : fn(jni_m : JNI); | |
| 6 | + widen : fn(havetype, wanttype : int, val : ref Value) : ref Value; | |
| 7 | + BuildArgList: fn(parms : JArrayJClass, arg : JArrayJObject) | |
| 8 | + : array of ref Value; | |
| 9 | + Class : fn(t : int) : JClass; | |
| 10 | + ClassObject : fn(t : int) : JObject; | |
| 11 | + GetPrimitiveClass : fn(typ : int) : ref Value; | |
| 12 | + GetVal : fn(o : JObject) : (int, ref Value); | |
| 13 | + SigToType : fn(sig : string) : int; | |
| 14 | + TypeToSig : fn(typ : int) : string; | |
| 15 | + TypeCharToName : fn(c : int) : string; | |
| 16 | + ValType : fn(val : ref Value) : int; | |
| 17 | + ValueToObject : fn(val : ref Value) : JObject; | |
| 18 | + | |
| 19 | +}; | |