1 // natMethod.cc - Native code for Method class.
3 /* Copyright (C) 1998, 1999, 2000, 2001 , 2002, 2003, 2004, 2005 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
16 #include <java-stack.h>
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>
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>
48 #include <java/lang/UnsupportedOperationException.h>
57 // This is used to determine when a primitive widening conversion is
59 static cpair primitives[] =
62 { JvPrimClass (boolean), &java::lang::Boolean::class$ },
63 { JvPrimClass (byte), &java::lang::Byte::class$ },
65 { JvPrimClass (short), &java::lang::Short::class$ },
67 { JvPrimClass (char), &java::lang::Character::class$ },
68 { JvPrimClass (int), &java::lang::Integer::class$ },
69 { JvPrimClass (long), &java::lang::Long::class$ },
70 { JvPrimClass (float), &java::lang::Float::class$ },
71 { JvPrimClass (double), &java::lang::Double::class$ },
75 static inline jboolean
76 can_widen (jclass from, jclass to)
78 int fromx = -1, tox = -1;
80 for (int i = 0; primitives[i].prim; ++i)
82 if (primitives[i].wrap == from)
84 if (primitives[i].prim == to)
88 // Can't handle a miss.
89 if (fromx == -1 || tox == -1)
91 // Boolean arguments may not be widened.
92 if (fromx == BOOLEAN && tox != BOOLEAN)
94 // Nothing promotes to char.
95 if (tox == CHAR && fromx != CHAR)
102 static inline ffi_type *
103 get_ffi_type (jclass klass)
107 return &ffi_type_pointer;
110 if (klass == JvPrimClass (byte))
112 else if (klass == JvPrimClass (short))
113 r = &ffi_type_sint16;
114 else if (klass == JvPrimClass (int))
115 r = &ffi_type_sint32;
116 else if (klass == JvPrimClass (long))
117 r = &ffi_type_sint64;
118 else if (klass == JvPrimClass (float))
120 else if (klass == JvPrimClass (double))
121 r = &ffi_type_double;
122 else if (klass == JvPrimClass (boolean))
124 // On some platforms a bool is a byte, on others an int.
125 if (sizeof (jboolean) == sizeof (jbyte))
129 JvAssert (sizeof (jboolean) == sizeof (jint));
130 r = &ffi_type_sint32;
133 else if (klass == JvPrimClass (char))
134 r = &ffi_type_uint16;
137 JvAssert (! klass->isPrimitive());
138 r = &ffi_type_pointer;
146 java::lang::reflect::Method::invoke (jobject obj, jobjectArray args)
148 using namespace java::lang::reflect;
151 if (parameter_types == NULL)
154 jmethodID meth = _Jv_FromReflectedMethod (this);
156 if (Modifier::isStatic(meth->accflags))
158 // We have to initialize a static class. It is safe to do this
159 // here and not in _Jv_CallAnyMethodA because JNI initializes a
160 // class whenever a method lookup is done.
161 _Jv_InitClass (declaringClass);
165 jclass objClass = JV_CLASS (obj);
166 if (! _Jv_IsAssignableFrom (declaringClass, objClass))
167 throw new java::lang::IllegalArgumentException;
170 // Check accessibility, if required.
171 if (! (Modifier::isPublic (meth->accflags) || this->isAccessible()))
173 Class *caller = _Jv_StackTrace::GetCallingClass (&Method::class$);
174 if (! _Jv_CheckAccess(caller, declaringClass, meth->accflags))
175 throw new IllegalAccessException;
178 if (declaringClass->isInterface())
179 iface = declaringClass;
181 return _Jv_CallAnyMethodA (obj, return_type, meth, false,
182 parameter_types, args, iface);
186 java::lang::reflect::Method::getModifiers ()
188 // Ignore all unknown flags.
189 return _Jv_FromReflectedMethod (this)->accflags & Modifier::ALL_FLAGS;
193 java::lang::reflect::Method::getName ()
196 name = _Jv_NewStringUtf8Const (_Jv_FromReflectedMethod (this)->name);
200 /* Internal method to set return_type and parameter_types fields. */
203 java::lang::reflect::Method::getType ()
205 _Jv_Method *method = _Jv_FromReflectedMethod (this);
206 _Jv_GetTypesFromSignature (method,
212 if (method->throws != NULL)
214 while (method->throws[count] != NULL)
219 = (JArray<jclass> *) JvNewObjectArray (count, &java::lang::Class::class$,
221 jclass *elts = elements (exception_types);
222 for (int i = 0; i < count; ++i)
223 elts[i] = _Jv_FindClass (method->throws[i],
224 declaringClass->getClassLoaderInternal ());
228 _Jv_GetTypesFromSignature (jmethodID method,
229 jclass declaringClass,
230 JArray<jclass> **arg_types_out,
231 jclass *return_type_out)
234 _Jv_Utf8Const* sig = method->signature;
235 java::lang::ClassLoader *loader = declaringClass->getClassLoaderInternal();
236 char *ptr = sig->chars();
238 /* First just count the number of parameters. */
239 // FIXME: should do some validation here, e.g., that there is only
266 while (*ptr != ';' && ptr[1] != '\0');
272 JArray<jclass> *args = (JArray<jclass> *)
273 JvNewObjectArray (numArgs, &java::lang::Class::class$, NULL);
274 jclass* argPtr = elements (args);
275 for (ptr = sig->chars(); *ptr != '\0'; ptr++)
281 argPtr = return_type_out;
286 jclass type = _Jv_FindClassFromSignature (ptr, loader, &end_ptr);
288 // FIXME: This isn't ideal.
289 throw new java::lang::NoClassDefFoundError (sig->toString());
291 // ARGPTR can be NULL if we are processing the return value of a
292 // call from Constructor.
298 *arg_types_out = args;
301 // This is a very rough analog of the JNI CallNonvirtual<type>MethodA
302 // functions. It handles both Methods and Constructors, and it can
303 // handle any return type. In the Constructor case, the `obj'
304 // argument is unused and should be NULL; also, the `return_type' is
305 // the class that the constructor will construct. RESULT is a pointer
306 // to a `jvalue' (see jni.h); for a void method this should be NULL.
307 // This function returns an exception (if one was thrown), or NULL if
310 _Jv_CallAnyMethodA (jobject obj,
313 jboolean is_constructor,
314 jboolean is_virtual_call,
315 JArray<jclass> *parameter_types,
318 jboolean is_jni_call,
321 using namespace java::lang::reflect;
324 JvAssert (! is_constructor || ! obj);
325 JvAssert (! is_constructor || return_type);
327 // See whether call needs an object as the first argument. A
328 // constructor does need a `this' argument, but it is one we create.
329 jboolean needs_this = false;
331 || ! Modifier::isStatic(meth->accflags))
334 int param_count = parameter_types->length;
339 // A constructor itself always returns void.
340 if (is_constructor || return_type == JvPrimClass (void))
341 rtype = &ffi_type_void;
343 rtype = get_ffi_type (return_type);
344 ffi_type **argtypes = (ffi_type **) __builtin_alloca (param_count
345 * sizeof (ffi_type *));
347 jclass *paramelts = elements (parameter_types);
349 // Special case for the `this' argument of a constructor. Note that
350 // the JDK 1.2 docs specify that the new object must be allocated
351 // before argument conversions are done.
353 obj = _Jv_AllocObject (return_type);
355 const int size_per_arg = sizeof(jvalue);
358 char *p = (char *) __builtin_alloca (param_count * size_per_arg);
359 // Overallocate to get correct alignment.
360 void **values = (void **)
361 __builtin_alloca (param_count * sizeof (void *));
366 // The `NULL' type is `Object'.
367 argtypes[i] = get_ffi_type (NULL);
369 memcpy (p, &obj, sizeof (jobject));
374 for (int arg = 0; i < param_count; ++i, ++arg)
378 argtypes[i] = get_ffi_type (paramelts[arg]);
379 if (paramelts[arg]->isPrimitive())
380 tsize = paramelts[arg]->size();
382 tsize = sizeof (jobject);
384 // Copy appropriate bits from the jvalue into the ffi array.
385 // FIXME: we could do this copying all in one loop, above, by
386 // over-allocating a bit.
387 // How do we do this without breaking big-endian platforms?
389 memcpy (p, &args[arg], tsize);
393 if (ffi_prep_cif (&cif, FFI_DEFAULT_ABI, param_count,
394 rtype, argtypes) != FFI_OK)
395 throw new java::lang::VirtualMachineError(JvNewStringLatin1("internal error: ffi_prep_cif failed"));
397 using namespace java::lang;
398 using namespace java::lang::reflect;
416 case FFI_TYPE_SINT16:
419 case FFI_TYPE_UINT16:
422 case FFI_TYPE_SINT32:
425 case FFI_TYPE_SINT64:
431 case FFI_TYPE_DOUBLE:
434 case FFI_TYPE_POINTER:
438 JvFail ("Unknown ffi_call return type");
444 // FIXME: If a vtable index is -1 at this point it is invalid, so we
445 // have to use the ncode.
447 // This can happen because methods in final classes don't have
448 // vtable entries, but _Jv_isVirtualMethod() doesn't know that. We
449 // could solve this problem by allocating a vtable index for methods
452 && ! Modifier::isFinal (meth->accflags)
453 && (_Jv_ushort)-1 != meth->index)
455 _Jv_VTable *vtable = *(_Jv_VTable **) obj;
458 if (is_jni_call && Modifier::isAbstract (meth->accflags))
460 // With JNI we don't know if this is an interface call
461 // or a call to an abstract method. Look up the method
462 // by name, the slow way.
463 _Jv_Method *concrete_meth
464 = _Jv_LookupDeclaredMethod (vtable->clas,
468 if (concrete_meth == NULL
469 || concrete_meth->ncode == NULL
470 || Modifier::isAbstract(concrete_meth->accflags))
471 throw new java::lang::IncompatibleClassChangeError
472 (_Jv_GetMethodString (vtable->clas, meth));
473 ncode = concrete_meth->ncode;
476 ncode = vtable->get_method (meth->index);
479 ncode = _Jv_LookupInterfaceMethodIdx (vtable->clas, iface,
489 ffi_call (&cif, (void (*)()) ncode, &ffi_result, values);
491 catch (Throwable *ex)
493 // For JNI we just throw the real error. For reflection, we
494 // wrap the underlying method's exception in an
495 // InvocationTargetException.
497 ex = new InvocationTargetException (ex);
501 // Since ffi_call returns integer values promoted to a word, use
502 // a narrowing conversion for jbyte, jchar, etc. results.
503 // Note that boolean is handled either by the FFI_TYPE_SINT8 or
504 // FFI_TYPE_SINT32 case.
514 result->b = (jbyte)ffi_result.i;
516 case FFI_TYPE_SINT16:
517 result->s = (jshort)ffi_result.i;
519 case FFI_TYPE_UINT16:
520 result->c = (jchar)ffi_result.i;
522 case FFI_TYPE_SINT32:
523 result->i = (jint)ffi_result.i;
525 case FFI_TYPE_SINT64:
526 result->j = (jlong)ffi_result.l;
529 result->f = (jfloat)ffi_result.f;
531 case FFI_TYPE_DOUBLE:
532 result->d = (jdouble)ffi_result.d;
534 case FFI_TYPE_POINTER:
535 result->l = (jobject)ffi_result.o;
538 JvFail ("Unknown ffi_call return type");
543 throw new java::lang::UnsupportedOperationException(JvNewStringLatin1("reflection not available in this build"));
547 // This is another version of _Jv_CallAnyMethodA, but this one does
548 // more checking and is used by the reflection (and not JNI) code.
550 _Jv_CallAnyMethodA (jobject obj,
553 jboolean is_constructor,
554 JArray<jclass> *parameter_types,
558 if (parameter_types->length == 0 && args == NULL)
560 // The JDK accepts this, so we do too.
562 else if (parameter_types->length != args->length)
563 throw new java::lang::IllegalArgumentException;
565 int param_count = parameter_types->length;
567 jclass *paramelts = elements (parameter_types);
568 jobject *argelts = args == NULL ? NULL : elements (args);
569 jvalue argvals[param_count];
571 #define COPY(Where, What, Type) \
574 memcpy ((Where), &val, sizeof (Type)); \
577 for (int i = 0; i < param_count; ++i)
579 jclass k = argelts[i] ? argelts[i]->getClass() : NULL;
580 if (paramelts[i]->isPrimitive())
584 || ! can_widen (k, paramelts[i]))
585 throw new java::lang::IllegalArgumentException;
587 if (paramelts[i] == JvPrimClass (boolean))
589 ((java::lang::Boolean *) argelts[i])->booleanValue(),
591 else if (paramelts[i] == JvPrimClass (char))
593 ((java::lang::Character *) argelts[i])->charValue(),
597 java::lang::Number *num = (java::lang::Number *) argelts[i];
598 if (paramelts[i] == JvPrimClass (byte))
599 COPY (&argvals[i], num->byteValue(), jbyte);
600 else if (paramelts[i] == JvPrimClass (short))
601 COPY (&argvals[i], num->shortValue(), jshort);
602 else if (paramelts[i] == JvPrimClass (int))
603 COPY (&argvals[i], num->intValue(), jint);
604 else if (paramelts[i] == JvPrimClass (long))
605 COPY (&argvals[i], num->longValue(), jlong);
606 else if (paramelts[i] == JvPrimClass (float))
607 COPY (&argvals[i], num->floatValue(), jfloat);
608 else if (paramelts[i] == JvPrimClass (double))
609 COPY (&argvals[i], num->doubleValue(), jdouble);
614 if (argelts[i] && ! paramelts[i]->isAssignableFrom (k))
615 throw new java::lang::IllegalArgumentException;
616 COPY (&argvals[i], argelts[i], jobject);
621 _Jv_CallAnyMethodA (obj, return_type, meth, is_constructor,
622 _Jv_isVirtualMethod (meth),
623 parameter_types, argvals, &ret_value,
627 #define VAL(Wrapper, Field) (new Wrapper (ret_value.Field))
630 else if (return_type == JvPrimClass (byte))
631 r = VAL (java::lang::Byte, b);
632 else if (return_type == JvPrimClass (short))
633 r = VAL (java::lang::Short, s);
634 else if (return_type == JvPrimClass (int))
635 r = VAL (java::lang::Integer, i);
636 else if (return_type == JvPrimClass (long))
637 r = VAL (java::lang::Long, j);
638 else if (return_type == JvPrimClass (float))
639 r = VAL (java::lang::Float, f);
640 else if (return_type == JvPrimClass (double))
641 r = VAL (java::lang::Double, d);
642 else if (return_type == JvPrimClass (boolean))
643 r = VAL (java::lang::Boolean, z);
644 else if (return_type == JvPrimClass (char))
645 r = VAL (java::lang::Character, c);
646 else if (return_type == JvPrimClass (void))
650 JvAssert (return_type == NULL || ! return_type->isPrimitive());