| 1 |
758 |
jeremybenn |
// natMethod.cc - Native code for Method class.
|
| 2 |
|
|
|
| 3 |
|
|
/* Copyright (C) 1998, 1999, 2000, 2001 , 2002, 2003, 2004, 2005, 2006 Free Software Foundation
|
| 4 |
|
|
|
| 5 |
|
|
This file is part of libgcj.
|
| 6 |
|
|
|
| 7 |
|
|
This software is copyrighted work licensed under the terms of the
|
| 8 |
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
| 9 |
|
|
details. */
|
| 10 |
|
|
|
| 11 |
|
|
#include <config.h>
|
| 12 |
|
|
|
| 13 |
|
|
#include <gcj/cni.h>
|
| 14 |
|
|
#include <jvm.h>
|
| 15 |
|
|
#include <jni.h>
|
| 16 |
|
|
#include <java-stack.h>
|
| 17 |
|
|
|
| 18 |
|
|
#include <java/lang/reflect/Method.h>
|
| 19 |
|
|
#include <java/lang/reflect/Constructor.h>
|
| 20 |
|
|
#include <java/lang/reflect/InvocationTargetException.h>
|
| 21 |
|
|
#include <java/lang/reflect/Modifier.h>
|
| 22 |
|
|
|
| 23 |
|
|
#include <java/lang/Void.h>
|
| 24 |
|
|
#include <java/lang/Byte.h>
|
| 25 |
|
|
#include <java/lang/Boolean.h>
|
| 26 |
|
|
#include <java/lang/Character.h>
|
| 27 |
|
|
#include <java/lang/Short.h>
|
| 28 |
|
|
#include <java/lang/Integer.h>
|
| 29 |
|
|
#include <java/lang/Long.h>
|
| 30 |
|
|
#include <java/lang/Float.h>
|
| 31 |
|
|
#include <java/lang/Double.h>
|
| 32 |
|
|
#include <java/lang/IllegalAccessException.h>
|
| 33 |
|
|
#include <java/lang/IllegalArgumentException.h>
|
| 34 |
|
|
#include <java/lang/IncompatibleClassChangeError.h>
|
| 35 |
|
|
#include <java/lang/NullPointerException.h>
|
| 36 |
|
|
#include <java/lang/ArrayIndexOutOfBoundsException.h>
|
| 37 |
|
|
#include <java/lang/VirtualMachineError.h>
|
| 38 |
|
|
#include <java/lang/Class.h>
|
| 39 |
|
|
#include <gcj/method.h>
|
| 40 |
|
|
#include <gnu/gcj/RawData.h>
|
| 41 |
|
|
#include <java/lang/NoClassDefFoundError.h>
|
| 42 |
|
|
|
| 43 |
|
|
#include <stdlib.h>
|
| 44 |
|
|
|
| 45 |
|
|
#if USE_LIBFFI
|
| 46 |
|
|
#include <ffi.h>
|
| 47 |
|
|
#else
|
| 48 |
|
|
#include <java/lang/UnsupportedOperationException.h>
|
| 49 |
|
|
#endif
|
| 50 |
|
|
|
| 51 |
|
|
typedef JArray< ::java::lang::annotation::Annotation * > * anno_a_t;
|
| 52 |
|
|
typedef JArray< JArray< ::java::lang::annotation::Annotation * > *> * anno_aa_t;
|
| 53 |
|
|
|
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
struct cpair
|
| 57 |
|
|
{
|
| 58 |
|
|
jclass prim;
|
| 59 |
|
|
jclass wrap;
|
| 60 |
|
|
};
|
| 61 |
|
|
|
| 62 |
|
|
// This is used to determine when a primitive widening conversion is
|
| 63 |
|
|
// allowed.
|
| 64 |
|
|
static cpair primitives[] =
|
| 65 |
|
|
{
|
| 66 |
|
|
#define BOOLEAN 0
|
| 67 |
|
|
{ JvPrimClass (boolean), &java::lang::Boolean::class$ },
|
| 68 |
|
|
{ JvPrimClass (byte), &java::lang::Byte::class$ },
|
| 69 |
|
|
#define SHORT 2
|
| 70 |
|
|
{ JvPrimClass (short), &java::lang::Short::class$ },
|
| 71 |
|
|
#define CHAR 3
|
| 72 |
|
|
{ JvPrimClass (char), &java::lang::Character::class$ },
|
| 73 |
|
|
{ JvPrimClass (int), &java::lang::Integer::class$ },
|
| 74 |
|
|
{ JvPrimClass (long), &java::lang::Long::class$ },
|
| 75 |
|
|
{ JvPrimClass (float), &java::lang::Float::class$ },
|
| 76 |
|
|
{ JvPrimClass (double), &java::lang::Double::class$ },
|
| 77 |
|
|
{ NULL, NULL }
|
| 78 |
|
|
};
|
| 79 |
|
|
|
| 80 |
|
|
static inline jboolean
|
| 81 |
|
|
can_widen (jclass from, jclass to)
|
| 82 |
|
|
{
|
| 83 |
|
|
int fromx = -1, tox = -1;
|
| 84 |
|
|
|
| 85 |
|
|
for (int i = 0; primitives[i].prim; ++i)
|
| 86 |
|
|
{
|
| 87 |
|
|
if (primitives[i].wrap == from)
|
| 88 |
|
|
fromx = i;
|
| 89 |
|
|
if (primitives[i].prim == to)
|
| 90 |
|
|
tox = i;
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 93 |
|
|
// Can't handle a miss.
|
| 94 |
|
|
if (fromx == -1 || tox == -1)
|
| 95 |
|
|
return false;
|
| 96 |
|
|
// Boolean arguments may not be widened.
|
| 97 |
|
|
if (fromx == BOOLEAN && tox != BOOLEAN)
|
| 98 |
|
|
return false;
|
| 99 |
|
|
// Nothing promotes to char.
|
| 100 |
|
|
if (tox == CHAR && fromx != CHAR)
|
| 101 |
|
|
return false;
|
| 102 |
|
|
|
| 103 |
|
|
return fromx <= tox;
|
| 104 |
|
|
}
|
| 105 |
|
|
|
| 106 |
|
|
#ifdef USE_LIBFFI
|
| 107 |
|
|
static inline ffi_type *
|
| 108 |
|
|
get_ffi_type (jclass klass)
|
| 109 |
|
|
{
|
| 110 |
|
|
// A special case.
|
| 111 |
|
|
if (klass == NULL)
|
| 112 |
|
|
return &ffi_type_pointer;
|
| 113 |
|
|
|
| 114 |
|
|
ffi_type *r;
|
| 115 |
|
|
if (klass == JvPrimClass (byte))
|
| 116 |
|
|
r = &ffi_type_sint8;
|
| 117 |
|
|
else if (klass == JvPrimClass (short))
|
| 118 |
|
|
r = &ffi_type_sint16;
|
| 119 |
|
|
else if (klass == JvPrimClass (int))
|
| 120 |
|
|
r = &ffi_type_sint32;
|
| 121 |
|
|
else if (klass == JvPrimClass (long))
|
| 122 |
|
|
r = &ffi_type_sint64;
|
| 123 |
|
|
else if (klass == JvPrimClass (float))
|
| 124 |
|
|
r = &ffi_type_float;
|
| 125 |
|
|
else if (klass == JvPrimClass (double))
|
| 126 |
|
|
r = &ffi_type_double;
|
| 127 |
|
|
else if (klass == JvPrimClass (boolean))
|
| 128 |
|
|
{
|
| 129 |
|
|
// On some platforms a bool is a byte, on others an int.
|
| 130 |
|
|
if (sizeof (jboolean) == sizeof (jbyte))
|
| 131 |
|
|
r = &ffi_type_sint8;
|
| 132 |
|
|
else
|
| 133 |
|
|
{
|
| 134 |
|
|
JvAssert (sizeof (jboolean) == sizeof (jint));
|
| 135 |
|
|
r = &ffi_type_sint32;
|
| 136 |
|
|
}
|
| 137 |
|
|
}
|
| 138 |
|
|
else if (klass == JvPrimClass (char))
|
| 139 |
|
|
r = &ffi_type_uint16;
|
| 140 |
|
|
else
|
| 141 |
|
|
{
|
| 142 |
|
|
JvAssert (! klass->isPrimitive());
|
| 143 |
|
|
r = &ffi_type_pointer;
|
| 144 |
|
|
}
|
| 145 |
|
|
|
| 146 |
|
|
return r;
|
| 147 |
|
|
}
|
| 148 |
|
|
#endif // USE_LIBFFI
|
| 149 |
|
|
|
| 150 |
|
|
jobject
|
| 151 |
|
|
java::lang::reflect::Method::invoke (jobject obj, jobjectArray args)
|
| 152 |
|
|
{
|
| 153 |
|
|
using namespace java::lang::reflect;
|
| 154 |
|
|
jclass iface = NULL;
|
| 155 |
|
|
|
| 156 |
|
|
if (parameter_types == NULL)
|
| 157 |
|
|
getType ();
|
| 158 |
|
|
|
| 159 |
|
|
jmethodID meth = _Jv_FromReflectedMethod (this);
|
| 160 |
|
|
|
| 161 |
|
|
if (Modifier::isStatic(meth->accflags))
|
| 162 |
|
|
{
|
| 163 |
|
|
// We have to initialize a static class. It is safe to do this
|
| 164 |
|
|
// here and not in _Jv_CallAnyMethodA because JNI initializes a
|
| 165 |
|
|
// class whenever a method lookup is done.
|
| 166 |
|
|
_Jv_InitClass (declaringClass);
|
| 167 |
|
|
}
|
| 168 |
|
|
else
|
| 169 |
|
|
{
|
| 170 |
|
|
jclass objClass = JV_CLASS (obj);
|
| 171 |
|
|
if (! _Jv_IsAssignableFrom (objClass, declaringClass))
|
| 172 |
|
|
throw new java::lang::IllegalArgumentException;
|
| 173 |
|
|
}
|
| 174 |
|
|
|
| 175 |
|
|
// Check accessibility, if required.
|
| 176 |
|
|
if (! this->isAccessible())
|
| 177 |
|
|
{
|
| 178 |
|
|
if (! (Modifier::isPublic (meth->accflags)))
|
| 179 |
|
|
{
|
| 180 |
|
|
Class *caller = _Jv_StackTrace::GetCallingClass (&Method::class$);
|
| 181 |
|
|
if (! _Jv_CheckAccess(caller, declaringClass, meth->accflags))
|
| 182 |
|
|
throw new IllegalAccessException;
|
| 183 |
|
|
}
|
| 184 |
|
|
else
|
| 185 |
|
|
// Method is public, check to see if class is accessible.
|
| 186 |
|
|
{
|
| 187 |
|
|
jint flags = (declaringClass->accflags
|
| 188 |
|
|
& (Modifier::PUBLIC
|
| 189 |
|
|
| Modifier::PROTECTED
|
| 190 |
|
|
| Modifier::PRIVATE));
|
| 191 |
|
|
if (flags == 0) // i.e. class is package private
|
| 192 |
|
|
{
|
| 193 |
|
|
Class *caller = _Jv_StackTrace::GetCallingClass (&Method::class$);
|
| 194 |
|
|
if (! _Jv_ClassNameSamePackage (caller->name,
|
| 195 |
|
|
declaringClass->name))
|
| 196 |
|
|
throw new IllegalAccessException;
|
| 197 |
|
|
}
|
| 198 |
|
|
}
|
| 199 |
|
|
}
|
| 200 |
|
|
|
| 201 |
|
|
if (declaringClass->isInterface())
|
| 202 |
|
|
iface = declaringClass;
|
| 203 |
|
|
|
| 204 |
|
|
return _Jv_CallAnyMethodA (obj, return_type, meth, false,
|
| 205 |
|
|
parameter_types, args, iface);
|
| 206 |
|
|
}
|
| 207 |
|
|
|
| 208 |
|
|
jint
|
| 209 |
|
|
java::lang::reflect::Method::getModifiersInternal ()
|
| 210 |
|
|
{
|
| 211 |
|
|
return _Jv_FromReflectedMethod (this)->accflags;
|
| 212 |
|
|
}
|
| 213 |
|
|
|
| 214 |
|
|
jstring
|
| 215 |
|
|
java::lang::reflect::Method::getSignature()
|
| 216 |
|
|
{
|
| 217 |
|
|
return declaringClass->getReflectionSignature (this);
|
| 218 |
|
|
}
|
| 219 |
|
|
|
| 220 |
|
|
jobject
|
| 221 |
|
|
java::lang::reflect::Method::getDefaultValue()
|
| 222 |
|
|
{
|
| 223 |
|
|
return declaringClass->getMethodDefaultValue(this);
|
| 224 |
|
|
}
|
| 225 |
|
|
|
| 226 |
|
|
anno_a_t
|
| 227 |
|
|
java::lang::reflect::Method::getDeclaredAnnotationsInternal()
|
| 228 |
|
|
{
|
| 229 |
|
|
return (anno_a_t) declaringClass->getDeclaredAnnotations(this, false);
|
| 230 |
|
|
}
|
| 231 |
|
|
|
| 232 |
|
|
anno_aa_t
|
| 233 |
|
|
java::lang::reflect::Method::getParameterAnnotationsInternal()
|
| 234 |
|
|
{
|
| 235 |
|
|
return (anno_aa_t) declaringClass->getDeclaredAnnotations(this, true);
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
jstring
|
| 239 |
|
|
java::lang::reflect::Method::getName ()
|
| 240 |
|
|
{
|
| 241 |
|
|
if (name == NULL)
|
| 242 |
|
|
name = _Jv_NewStringUtf8Const (_Jv_FromReflectedMethod (this)->name);
|
| 243 |
|
|
return name;
|
| 244 |
|
|
}
|
| 245 |
|
|
|
| 246 |
|
|
/* Internal method to set return_type and parameter_types fields. */
|
| 247 |
|
|
|
| 248 |
|
|
void
|
| 249 |
|
|
java::lang::reflect::Method::getType ()
|
| 250 |
|
|
{
|
| 251 |
|
|
_Jv_Method *method = _Jv_FromReflectedMethod (this);
|
| 252 |
|
|
_Jv_GetTypesFromSignature (method,
|
| 253 |
|
|
declaringClass,
|
| 254 |
|
|
¶meter_types,
|
| 255 |
|
|
&return_type);
|
| 256 |
|
|
|
| 257 |
|
|
int count = 0;
|
| 258 |
|
|
if (method->throws != NULL)
|
| 259 |
|
|
{
|
| 260 |
|
|
while (method->throws[count] != NULL)
|
| 261 |
|
|
++count;
|
| 262 |
|
|
}
|
| 263 |
|
|
|
| 264 |
|
|
exception_types
|
| 265 |
|
|
= (JArray<jclass> *) JvNewObjectArray (count, &java::lang::Class::class$,
|
| 266 |
|
|
NULL);
|
| 267 |
|
|
jclass *elts = elements (exception_types);
|
| 268 |
|
|
for (int i = 0; i < count; ++i)
|
| 269 |
|
|
elts[i] = _Jv_FindClass (method->throws[i],
|
| 270 |
|
|
declaringClass->getClassLoaderInternal ());
|
| 271 |
|
|
}
|
| 272 |
|
|
|
| 273 |
|
|
void
|
| 274 |
|
|
_Jv_GetTypesFromSignature (jmethodID method,
|
| 275 |
|
|
jclass declaringClass,
|
| 276 |
|
|
JArray<jclass> **arg_types_out,
|
| 277 |
|
|
jclass *return_type_out)
|
| 278 |
|
|
{
|
| 279 |
|
|
|
| 280 |
|
|
_Jv_Utf8Const* sig = method->signature;
|
| 281 |
|
|
java::lang::ClassLoader *loader = declaringClass->getClassLoaderInternal();
|
| 282 |
|
|
char *ptr = sig->chars();
|
| 283 |
|
|
int numArgs = 0;
|
| 284 |
|
|
/* First just count the number of parameters. */
|
| 285 |
|
|
// FIXME: should do some validation here, e.g., that there is only
|
| 286 |
|
|
// one return type.
|
| 287 |
|
|
for (; ; ptr++)
|
| 288 |
|
|
{
|
| 289 |
|
|
switch (*ptr)
|
| 290 |
|
|
{
|
| 291 |
|
|
case 0:
|
| 292 |
|
|
case ')':
|
| 293 |
|
|
case 'V':
|
| 294 |
|
|
break;
|
| 295 |
|
|
case '[':
|
| 296 |
|
|
case '(':
|
| 297 |
|
|
continue;
|
| 298 |
|
|
case 'B':
|
| 299 |
|
|
case 'C':
|
| 300 |
|
|
case 'D':
|
| 301 |
|
|
case 'F':
|
| 302 |
|
|
case 'S':
|
| 303 |
|
|
case 'I':
|
| 304 |
|
|
case 'J':
|
| 305 |
|
|
case 'Z':
|
| 306 |
|
|
numArgs++;
|
| 307 |
|
|
continue;
|
| 308 |
|
|
case 'L':
|
| 309 |
|
|
numArgs++;
|
| 310 |
|
|
do
|
| 311 |
|
|
ptr++;
|
| 312 |
|
|
while (*ptr != ';' && ptr[1] != '\0');
|
| 313 |
|
|
continue;
|
| 314 |
|
|
}
|
| 315 |
|
|
break;
|
| 316 |
|
|
}
|
| 317 |
|
|
|
| 318 |
|
|
JArray<jclass> *args = (JArray<jclass> *)
|
| 319 |
|
|
JvNewObjectArray (numArgs, &java::lang::Class::class$, NULL);
|
| 320 |
|
|
jclass* argPtr = elements (args);
|
| 321 |
|
|
for (ptr = sig->chars(); *ptr != '\0'; ptr++)
|
| 322 |
|
|
{
|
| 323 |
|
|
if (*ptr == '(')
|
| 324 |
|
|
continue;
|
| 325 |
|
|
if (*ptr == ')')
|
| 326 |
|
|
{
|
| 327 |
|
|
argPtr = return_type_out;
|
| 328 |
|
|
continue;
|
| 329 |
|
|
}
|
| 330 |
|
|
|
| 331 |
|
|
char *end_ptr;
|
| 332 |
|
|
jclass type = _Jv_FindClassFromSignature (ptr, loader, &end_ptr);
|
| 333 |
|
|
if (type == NULL)
|
| 334 |
|
|
// FIXME: This isn't ideal.
|
| 335 |
|
|
throw new java::lang::NoClassDefFoundError (sig->toString());
|
| 336 |
|
|
|
| 337 |
|
|
// ARGPTR can be NULL if we are processing the return value of a
|
| 338 |
|
|
// call from Constructor.
|
| 339 |
|
|
if (argPtr)
|
| 340 |
|
|
*argPtr++ = type;
|
| 341 |
|
|
|
| 342 |
|
|
ptr = end_ptr;
|
| 343 |
|
|
}
|
| 344 |
|
|
*arg_types_out = args;
|
| 345 |
|
|
}
|
| 346 |
|
|
|
| 347 |
|
|
// This is a very rough analog of the JNI CallNonvirtual<type>MethodA
|
| 348 |
|
|
// functions. It handles both Methods and Constructors, and it can
|
| 349 |
|
|
// handle any return type. In the Constructor case, the `obj'
|
| 350 |
|
|
// argument is unused and should be NULL; also, the `return_type' is
|
| 351 |
|
|
// the class that the constructor will construct. RESULT is a pointer
|
| 352 |
|
|
// to a `jvalue' (see jni.h); for a void method this should be NULL.
|
| 353 |
|
|
// This function returns an exception (if one was thrown), or NULL if
|
| 354 |
|
|
// the call went ok.
|
| 355 |
|
|
void
|
| 356 |
|
|
_Jv_CallAnyMethodA (jobject obj,
|
| 357 |
|
|
jclass return_type,
|
| 358 |
|
|
jmethodID meth,
|
| 359 |
|
|
jboolean is_constructor,
|
| 360 |
|
|
jboolean is_virtual_call,
|
| 361 |
|
|
JArray<jclass> *parameter_types,
|
| 362 |
|
|
const jvalue *args,
|
| 363 |
|
|
jvalue *result,
|
| 364 |
|
|
jboolean is_jni_call,
|
| 365 |
|
|
jclass iface)
|
| 366 |
|
|
{
|
| 367 |
|
|
using namespace java::lang::reflect;
|
| 368 |
|
|
|
| 369 |
|
|
#ifdef USE_LIBFFI
|
| 370 |
|
|
JvAssert (! is_constructor || ! obj);
|
| 371 |
|
|
JvAssert (! is_constructor || return_type);
|
| 372 |
|
|
|
| 373 |
|
|
// See whether call needs an object as the first argument. A
|
| 374 |
|
|
// constructor does need a `this' argument, but it is one we create.
|
| 375 |
|
|
jboolean needs_this = false;
|
| 376 |
|
|
if (is_constructor
|
| 377 |
|
|
|| ! Modifier::isStatic(meth->accflags))
|
| 378 |
|
|
needs_this = true;
|
| 379 |
|
|
|
| 380 |
|
|
int param_count = parameter_types->length;
|
| 381 |
|
|
if (needs_this)
|
| 382 |
|
|
++param_count;
|
| 383 |
|
|
|
| 384 |
|
|
ffi_type *rtype;
|
| 385 |
|
|
// A constructor itself always returns void.
|
| 386 |
|
|
if (is_constructor || return_type == JvPrimClass (void))
|
| 387 |
|
|
rtype = &ffi_type_void;
|
| 388 |
|
|
else
|
| 389 |
|
|
rtype = get_ffi_type (return_type);
|
| 390 |
|
|
ffi_type **argtypes = (ffi_type **) __builtin_alloca (param_count
|
| 391 |
|
|
* sizeof (ffi_type *));
|
| 392 |
|
|
|
| 393 |
|
|
jclass *paramelts = elements (parameter_types);
|
| 394 |
|
|
|
| 395 |
|
|
// Special case for the `this' argument of a constructor. Note that
|
| 396 |
|
|
// the JDK 1.2 docs specify that the new object must be allocated
|
| 397 |
|
|
// before argument conversions are done.
|
| 398 |
|
|
if (is_constructor)
|
| 399 |
|
|
obj = _Jv_AllocObject (return_type);
|
| 400 |
|
|
|
| 401 |
|
|
const int size_per_arg = sizeof(jvalue);
|
| 402 |
|
|
ffi_cif cif;
|
| 403 |
|
|
|
| 404 |
|
|
char *p = (char *) __builtin_alloca (param_count * size_per_arg);
|
| 405 |
|
|
// Overallocate to get correct alignment.
|
| 406 |
|
|
void **values = (void **)
|
| 407 |
|
|
__builtin_alloca (param_count * sizeof (void *));
|
| 408 |
|
|
|
| 409 |
|
|
int i = 0;
|
| 410 |
|
|
if (needs_this)
|
| 411 |
|
|
{
|
| 412 |
|
|
// The `NULL' type is `Object'.
|
| 413 |
|
|
argtypes[i] = get_ffi_type (NULL);
|
| 414 |
|
|
values[i] = p;
|
| 415 |
|
|
memcpy (p, &obj, sizeof (jobject));
|
| 416 |
|
|
p += size_per_arg;
|
| 417 |
|
|
++i;
|
| 418 |
|
|
}
|
| 419 |
|
|
|
| 420 |
|
|
for (int arg = 0; i < param_count; ++i, ++arg)
|
| 421 |
|
|
{
|
| 422 |
|
|
int tsize;
|
| 423 |
|
|
|
| 424 |
|
|
argtypes[i] = get_ffi_type (paramelts[arg]);
|
| 425 |
|
|
if (paramelts[arg]->isPrimitive())
|
| 426 |
|
|
tsize = paramelts[arg]->size();
|
| 427 |
|
|
else
|
| 428 |
|
|
tsize = sizeof (jobject);
|
| 429 |
|
|
|
| 430 |
|
|
// Copy appropriate bits from the jvalue into the ffi array.
|
| 431 |
|
|
// FIXME: we could do this copying all in one loop, above, by
|
| 432 |
|
|
// over-allocating a bit.
|
| 433 |
|
|
// How do we do this without breaking big-endian platforms?
|
| 434 |
|
|
values[i] = p;
|
| 435 |
|
|
memcpy (p, &args[arg], tsize);
|
| 436 |
|
|
p += size_per_arg;
|
| 437 |
|
|
}
|
| 438 |
|
|
|
| 439 |
|
|
ffi_abi cabi = FFI_DEFAULT_ABI;
|
| 440 |
|
|
#if defined (X86_WIN32) && !defined (__CYGWIN__)
|
| 441 |
|
|
if (needs_this)
|
| 442 |
|
|
cabi = FFI_THISCALL;
|
| 443 |
|
|
#endif
|
| 444 |
|
|
if (ffi_prep_cif (&cif, cabi, param_count,
|
| 445 |
|
|
rtype, argtypes) != FFI_OK)
|
| 446 |
|
|
throw new java::lang::VirtualMachineError(JvNewStringLatin1("internal error: ffi_prep_cif failed"));
|
| 447 |
|
|
|
| 448 |
|
|
using namespace java::lang;
|
| 449 |
|
|
using namespace java::lang::reflect;
|
| 450 |
|
|
|
| 451 |
|
|
union
|
| 452 |
|
|
{
|
| 453 |
|
|
ffi_arg i;
|
| 454 |
|
|
jobject o;
|
| 455 |
|
|
jlong l;
|
| 456 |
|
|
jfloat f;
|
| 457 |
|
|
jdouble d;
|
| 458 |
|
|
} ffi_result;
|
| 459 |
|
|
|
| 460 |
|
|
switch (rtype->type)
|
| 461 |
|
|
{
|
| 462 |
|
|
case FFI_TYPE_VOID:
|
| 463 |
|
|
break;
|
| 464 |
|
|
case FFI_TYPE_SINT8:
|
| 465 |
|
|
result->b = 0;
|
| 466 |
|
|
break;
|
| 467 |
|
|
case FFI_TYPE_SINT16:
|
| 468 |
|
|
result->s = 0;
|
| 469 |
|
|
break;
|
| 470 |
|
|
case FFI_TYPE_UINT16:
|
| 471 |
|
|
result->c = 0;
|
| 472 |
|
|
break;
|
| 473 |
|
|
case FFI_TYPE_SINT32:
|
| 474 |
|
|
result->i = 0;
|
| 475 |
|
|
break;
|
| 476 |
|
|
case FFI_TYPE_SINT64:
|
| 477 |
|
|
result->j = 0;
|
| 478 |
|
|
break;
|
| 479 |
|
|
case FFI_TYPE_FLOAT:
|
| 480 |
|
|
result->f = 0;
|
| 481 |
|
|
break;
|
| 482 |
|
|
case FFI_TYPE_DOUBLE:
|
| 483 |
|
|
result->d = 0;
|
| 484 |
|
|
break;
|
| 485 |
|
|
case FFI_TYPE_POINTER:
|
| 486 |
|
|
result->l = 0;
|
| 487 |
|
|
break;
|
| 488 |
|
|
default:
|
| 489 |
|
|
JvFail ("Unknown ffi_call return type");
|
| 490 |
|
|
break;
|
| 491 |
|
|
}
|
| 492 |
|
|
|
| 493 |
|
|
void *ncode;
|
| 494 |
|
|
|
| 495 |
|
|
// FIXME: If a vtable index is -1 at this point it is invalid, so we
|
| 496 |
|
|
// have to use the ncode.
|
| 497 |
|
|
//
|
| 498 |
|
|
// This can happen because methods in final classes don't have
|
| 499 |
|
|
// vtable entries, but _Jv_isVirtualMethod() doesn't know that. We
|
| 500 |
|
|
// could solve this problem by allocating a vtable index for methods
|
| 501 |
|
|
// in final classes.
|
| 502 |
|
|
if (is_virtual_call
|
| 503 |
|
|
&& ! Modifier::isFinal (meth->accflags)
|
| 504 |
|
|
&& (_Jv_ushort)-1 != meth->index)
|
| 505 |
|
|
{
|
| 506 |
|
|
_Jv_VTable *vtable = *(_Jv_VTable **) obj;
|
| 507 |
|
|
if (iface == NULL)
|
| 508 |
|
|
{
|
| 509 |
|
|
if (is_jni_call && Modifier::isAbstract (meth->accflags))
|
| 510 |
|
|
{
|
| 511 |
|
|
// With JNI we don't know if this is an interface call
|
| 512 |
|
|
// or a call to an abstract method. Look up the method
|
| 513 |
|
|
// by name, the slow way.
|
| 514 |
|
|
_Jv_Method *concrete_meth
|
| 515 |
|
|
= _Jv_LookupDeclaredMethod (vtable->clas,
|
| 516 |
|
|
meth->name,
|
| 517 |
|
|
meth->signature,
|
| 518 |
|
|
NULL);
|
| 519 |
|
|
if (concrete_meth == NULL
|
| 520 |
|
|
|| concrete_meth->ncode == NULL
|
| 521 |
|
|
|| Modifier::isAbstract(concrete_meth->accflags))
|
| 522 |
|
|
throw new java::lang::IncompatibleClassChangeError
|
| 523 |
|
|
(_Jv_GetMethodString (vtable->clas, meth));
|
| 524 |
|
|
ncode = concrete_meth->ncode;
|
| 525 |
|
|
}
|
| 526 |
|
|
else
|
| 527 |
|
|
ncode = vtable->get_method (meth->index);
|
| 528 |
|
|
}
|
| 529 |
|
|
else
|
| 530 |
|
|
ncode = _Jv_LookupInterfaceMethodIdx (vtable->clas, iface,
|
| 531 |
|
|
meth->index);
|
| 532 |
|
|
}
|
| 533 |
|
|
else
|
| 534 |
|
|
{
|
| 535 |
|
|
ncode = meth->ncode;
|
| 536 |
|
|
}
|
| 537 |
|
|
|
| 538 |
|
|
try
|
| 539 |
|
|
{
|
| 540 |
|
|
ffi_call (&cif, (void (*)()) ncode, &ffi_result, values);
|
| 541 |
|
|
}
|
| 542 |
|
|
catch (Throwable *ex)
|
| 543 |
|
|
{
|
| 544 |
|
|
// For JNI we just throw the real error. For reflection, we
|
| 545 |
|
|
// wrap the underlying method's exception in an
|
| 546 |
|
|
// InvocationTargetException.
|
| 547 |
|
|
if (! is_jni_call)
|
| 548 |
|
|
ex = new InvocationTargetException (ex);
|
| 549 |
|
|
throw ex;
|
| 550 |
|
|
}
|
| 551 |
|
|
|
| 552 |
|
|
// Since ffi_call returns integer values promoted to a word, use
|
| 553 |
|
|
// a narrowing conversion for jbyte, jchar, etc. results.
|
| 554 |
|
|
// Note that boolean is handled either by the FFI_TYPE_SINT8 or
|
| 555 |
|
|
// FFI_TYPE_SINT32 case.
|
| 556 |
|
|
if (is_constructor)
|
| 557 |
|
|
result->l = obj;
|
| 558 |
|
|
else
|
| 559 |
|
|
{
|
| 560 |
|
|
switch (rtype->type)
|
| 561 |
|
|
{
|
| 562 |
|
|
case FFI_TYPE_VOID:
|
| 563 |
|
|
break;
|
| 564 |
|
|
case FFI_TYPE_SINT8:
|
| 565 |
|
|
result->b = (jbyte)ffi_result.i;
|
| 566 |
|
|
break;
|
| 567 |
|
|
case FFI_TYPE_SINT16:
|
| 568 |
|
|
result->s = (jshort)ffi_result.i;
|
| 569 |
|
|
break;
|
| 570 |
|
|
case FFI_TYPE_UINT16:
|
| 571 |
|
|
result->c = (jchar)ffi_result.i;
|
| 572 |
|
|
break;
|
| 573 |
|
|
case FFI_TYPE_SINT32:
|
| 574 |
|
|
result->i = (jint)ffi_result.i;
|
| 575 |
|
|
break;
|
| 576 |
|
|
case FFI_TYPE_SINT64:
|
| 577 |
|
|
result->j = (jlong)ffi_result.l;
|
| 578 |
|
|
break;
|
| 579 |
|
|
case FFI_TYPE_FLOAT:
|
| 580 |
|
|
result->f = (jfloat)ffi_result.f;
|
| 581 |
|
|
break;
|
| 582 |
|
|
case FFI_TYPE_DOUBLE:
|
| 583 |
|
|
result->d = (jdouble)ffi_result.d;
|
| 584 |
|
|
break;
|
| 585 |
|
|
case FFI_TYPE_POINTER:
|
| 586 |
|
|
result->l = (jobject)ffi_result.o;
|
| 587 |
|
|
break;
|
| 588 |
|
|
default:
|
| 589 |
|
|
JvFail ("Unknown ffi_call return type");
|
| 590 |
|
|
break;
|
| 591 |
|
|
}
|
| 592 |
|
|
}
|
| 593 |
|
|
#else
|
| 594 |
|
|
throw new java::lang::UnsupportedOperationException(JvNewStringLatin1("reflection not available in this build"));
|
| 595 |
|
|
#endif // USE_LIBFFI
|
| 596 |
|
|
}
|
| 597 |
|
|
|
| 598 |
|
|
// This is another version of _Jv_CallAnyMethodA, but this one does
|
| 599 |
|
|
// more checking and is used by the reflection (and not JNI) code.
|
| 600 |
|
|
jobject
|
| 601 |
|
|
_Jv_CallAnyMethodA (jobject obj,
|
| 602 |
|
|
jclass return_type,
|
| 603 |
|
|
jmethodID meth,
|
| 604 |
|
|
jboolean is_constructor,
|
| 605 |
|
|
JArray<jclass> *parameter_types,
|
| 606 |
|
|
jobjectArray args,
|
| 607 |
|
|
jclass iface)
|
| 608 |
|
|
{
|
| 609 |
|
|
if (parameter_types->length == 0 && args == NULL)
|
| 610 |
|
|
{
|
| 611 |
|
|
// The JDK accepts this, so we do too.
|
| 612 |
|
|
}
|
| 613 |
|
|
else if (parameter_types->length != args->length)
|
| 614 |
|
|
throw new java::lang::IllegalArgumentException;
|
| 615 |
|
|
|
| 616 |
|
|
int param_count = parameter_types->length;
|
| 617 |
|
|
|
| 618 |
|
|
jclass *paramelts = elements (parameter_types);
|
| 619 |
|
|
jobject *argelts = args == NULL ? NULL : elements (args);
|
| 620 |
|
|
jvalue argvals[param_count];
|
| 621 |
|
|
|
| 622 |
|
|
#define COPY(Where, What, Type) \
|
| 623 |
|
|
do { \
|
| 624 |
|
|
Type val = (What); \
|
| 625 |
|
|
memcpy ((Where), &val, sizeof (Type)); \
|
| 626 |
|
|
} while (0)
|
| 627 |
|
|
|
| 628 |
|
|
for (int i = 0; i < param_count; ++i)
|
| 629 |
|
|
{
|
| 630 |
|
|
jclass k = argelts[i] ? argelts[i]->getClass() : NULL;
|
| 631 |
|
|
if (paramelts[i]->isPrimitive())
|
| 632 |
|
|
{
|
| 633 |
|
|
if (! argelts[i]
|
| 634 |
|
|
|| ! k
|
| 635 |
|
|
|| ! can_widen (k, paramelts[i]))
|
| 636 |
|
|
throw new java::lang::IllegalArgumentException;
|
| 637 |
|
|
|
| 638 |
|
|
if (paramelts[i] == JvPrimClass (boolean))
|
| 639 |
|
|
COPY (&argvals[i],
|
| 640 |
|
|
((java::lang::Boolean *) argelts[i])->booleanValue(),
|
| 641 |
|
|
jboolean);
|
| 642 |
|
|
else if (paramelts[i] == JvPrimClass (char))
|
| 643 |
|
|
COPY (&argvals[i],
|
| 644 |
|
|
((java::lang::Character *) argelts[i])->charValue(),
|
| 645 |
|
|
jchar);
|
| 646 |
|
|
else
|
| 647 |
|
|
{
|
| 648 |
|
|
java::lang::Number *num = (java::lang::Number *) argelts[i];
|
| 649 |
|
|
if (paramelts[i] == JvPrimClass (byte))
|
| 650 |
|
|
COPY (&argvals[i], num->byteValue(), jbyte);
|
| 651 |
|
|
else if (paramelts[i] == JvPrimClass (short))
|
| 652 |
|
|
COPY (&argvals[i], num->shortValue(), jshort);
|
| 653 |
|
|
else if (paramelts[i] == JvPrimClass (int))
|
| 654 |
|
|
COPY (&argvals[i], num->intValue(), jint);
|
| 655 |
|
|
else if (paramelts[i] == JvPrimClass (long))
|
| 656 |
|
|
COPY (&argvals[i], num->longValue(), jlong);
|
| 657 |
|
|
else if (paramelts[i] == JvPrimClass (float))
|
| 658 |
|
|
COPY (&argvals[i], num->floatValue(), jfloat);
|
| 659 |
|
|
else if (paramelts[i] == JvPrimClass (double))
|
| 660 |
|
|
COPY (&argvals[i], num->doubleValue(), jdouble);
|
| 661 |
|
|
}
|
| 662 |
|
|
}
|
| 663 |
|
|
else
|
| 664 |
|
|
{
|
| 665 |
|
|
if (argelts[i] && ! paramelts[i]->isAssignableFrom (k))
|
| 666 |
|
|
throw new java::lang::IllegalArgumentException;
|
| 667 |
|
|
COPY (&argvals[i], argelts[i], jobject);
|
| 668 |
|
|
}
|
| 669 |
|
|
}
|
| 670 |
|
|
|
| 671 |
|
|
jvalue ret_value;
|
| 672 |
|
|
_Jv_CallAnyMethodA (obj, return_type, meth, is_constructor,
|
| 673 |
|
|
_Jv_isVirtualMethod (meth),
|
| 674 |
|
|
parameter_types, argvals, &ret_value,
|
| 675 |
|
|
false, iface);
|
| 676 |
|
|
|
| 677 |
|
|
jobject r;
|
| 678 |
|
|
#define VAL(Wrapper, Field) (new Wrapper (ret_value.Field))
|
| 679 |
|
|
if (is_constructor)
|
| 680 |
|
|
r = ret_value.l;
|
| 681 |
|
|
else if (return_type == JvPrimClass (byte))
|
| 682 |
|
|
r = VAL (java::lang::Byte, b);
|
| 683 |
|
|
else if (return_type == JvPrimClass (short))
|
| 684 |
|
|
r = VAL (java::lang::Short, s);
|
| 685 |
|
|
else if (return_type == JvPrimClass (int))
|
| 686 |
|
|
r = VAL (java::lang::Integer, i);
|
| 687 |
|
|
else if (return_type == JvPrimClass (long))
|
| 688 |
|
|
r = VAL (java::lang::Long, j);
|
| 689 |
|
|
else if (return_type == JvPrimClass (float))
|
| 690 |
|
|
r = VAL (java::lang::Float, f);
|
| 691 |
|
|
else if (return_type == JvPrimClass (double))
|
| 692 |
|
|
r = VAL (java::lang::Double, d);
|
| 693 |
|
|
else if (return_type == JvPrimClass (boolean))
|
| 694 |
|
|
r = VAL (java::lang::Boolean, z);
|
| 695 |
|
|
else if (return_type == JvPrimClass (char))
|
| 696 |
|
|
r = VAL (java::lang::Character, c);
|
| 697 |
|
|
else if (return_type == JvPrimClass (void))
|
| 698 |
|
|
r = NULL;
|
| 699 |
|
|
else
|
| 700 |
|
|
{
|
| 701 |
|
|
JvAssert (return_type == NULL || ! return_type->isPrimitive());
|
| 702 |
|
|
r = ret_value.l;
|
| 703 |
|
|
}
|
| 704 |
|
|
|
| 705 |
|
|
return r;
|
| 706 |
|
|
}
|