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
17 #include <java/lang/reflect/Method.h>
18 #include <java/lang/reflect/Constructor.h>
19 #include <java/lang/reflect/InvocationTargetException.h>
20 #include <java/lang/reflect/Modifier.h>
22 #include <java/lang/Void.h>
23 #include <java/lang/Byte.h>
24 #include <java/lang/Boolean.h>
25 #include <java/lang/Character.h>
26 #include <java/lang/Short.h>
27 #include <java/lang/Integer.h>
28 #include <java/lang/Long.h>
29 #include <java/lang/Float.h>
30 #include <java/lang/Double.h>
31 #include <java/lang/IllegalAccessException.h>
32 #include <java/lang/IllegalArgumentException.h>
33 #include <java/lang/IncompatibleClassChangeError.h>
34 #include <java/lang/NullPointerException.h>
35 #include <java/lang/ArrayIndexOutOfBoundsException.h>
36 #include <java/lang/VirtualMachineError.h>
37 #include <java/lang/Class.h>
38 #include <gcj/method.h>
39 #include <gnu/gcj/RawData.h>
46 #include <java/lang/UnsupportedOperationException.h>
55 // This is used to determine when a primitive widening conversion is
57 static cpair primitives
[] =
60 { JvPrimClass (boolean
), &java::lang::Boolean::class$
},
61 { JvPrimClass (byte
), &java::lang::Byte::class$
},
63 { JvPrimClass (short), &java::lang::Short::class$
},
65 { JvPrimClass (char), &java::lang::Character::class$
},
66 { JvPrimClass (int), &java::lang::Integer::class$
},
67 { JvPrimClass (long), &java::lang::Long::class$
},
68 { JvPrimClass (float), &java::lang::Float::class$
},
69 { JvPrimClass (double), &java::lang::Double::class$
},
73 static inline jboolean
74 can_widen (jclass from
, jclass to
)
76 int fromx
= -1, tox
= -1;
78 for (int i
= 0; primitives
[i
].prim
; ++i
)
80 if (primitives
[i
].wrap
== from
)
82 if (primitives
[i
].prim
== to
)
86 // Can't handle a miss.
87 if (fromx
== -1 || tox
== -1)
89 // Boolean arguments may not be widened.
90 if (fromx
== BOOLEAN
&& tox
!= BOOLEAN
)
92 // Nothing promotes to char.
93 if (tox
== CHAR
&& fromx
!= CHAR
)
100 static inline ffi_type
*
101 get_ffi_type (jclass klass
)
105 return &ffi_type_pointer
;
108 if (klass
== JvPrimClass (byte
))
110 else if (klass
== JvPrimClass (short))
111 r
= &ffi_type_sint16
;
112 else if (klass
== JvPrimClass (int))
113 r
= &ffi_type_sint32
;
114 else if (klass
== JvPrimClass (long))
115 r
= &ffi_type_sint64
;
116 else if (klass
== JvPrimClass (float))
118 else if (klass
== JvPrimClass (double))
119 r
= &ffi_type_double
;
120 else if (klass
== JvPrimClass (boolean
))
122 // On some platforms a bool is a byte, on others an int.
123 if (sizeof (jboolean
) == sizeof (jbyte
))
127 JvAssert (sizeof (jboolean
) == sizeof (jint
));
128 r
= &ffi_type_sint32
;
131 else if (klass
== JvPrimClass (char))
132 r
= &ffi_type_uint16
;
135 JvAssert (! klass
->isPrimitive());
136 r
= &ffi_type_pointer
;
144 java::lang::reflect::Method::invoke (jobject obj
, jobjectArray args
)
146 using namespace java::lang::reflect
;
149 if (parameter_types
== NULL
)
152 jmethodID meth
= _Jv_FromReflectedMethod (this);
154 if (Modifier::isStatic(meth
->accflags
))
156 // We have to initialize a static class. It is safe to do this
157 // here and not in _Jv_CallAnyMethodA because JNI initializes a
158 // class whenever a method lookup is done.
159 _Jv_InitClass (declaringClass
);
163 jclass objClass
= JV_CLASS (obj
);
164 if (! _Jv_IsAssignableFrom (declaringClass
, objClass
))
165 throw new java::lang::IllegalArgumentException
;
168 // Check accessibility, if required.
169 if (! (Modifier::isPublic (meth
->accflags
) || this->isAccessible()))
171 gnu::gcj::runtime::StackTrace
*t
172 = new gnu::gcj::runtime::StackTrace(4);
173 Class
*caller
= NULL
;
176 for (int i
= 1; !caller
; i
++)
178 caller
= t
->classAt (i
);
181 catch (::java::lang::ArrayIndexOutOfBoundsException
*e
)
185 if (! _Jv_CheckAccess(caller
, declaringClass
, meth
->accflags
))
186 throw new IllegalAccessException
;
189 if (declaringClass
->isInterface())
190 iface
= declaringClass
;
192 return _Jv_CallAnyMethodA (obj
, return_type
, meth
, false,
193 parameter_types
, args
, iface
);
197 java::lang::reflect::Method::getModifiers ()
199 // Ignore all unknown flags.
200 return _Jv_FromReflectedMethod (this)->accflags
& Modifier::ALL_FLAGS
;
204 java::lang::reflect::Method::getName ()
207 name
= _Jv_NewStringUtf8Const (_Jv_FromReflectedMethod (this)->name
);
211 /* Internal method to set return_type and parameter_types fields. */
214 java::lang::reflect::Method::getType ()
216 _Jv_Method
*method
= _Jv_FromReflectedMethod (this);
217 _Jv_GetTypesFromSignature (method
,
223 if (method
->throws
!= NULL
)
225 while (method
->throws
[count
] != NULL
)
230 = (JArray
<jclass
> *) JvNewObjectArray (count
, &java::lang::Class::class$
,
232 jclass
*elts
= elements (exception_types
);
233 for (int i
= 0; i
< count
; ++i
)
234 elts
[i
] = _Jv_FindClass (method
->throws
[i
],
235 declaringClass
->getClassLoaderInternal ());
239 _Jv_GetTypesFromSignature (jmethodID method
,
240 jclass declaringClass
,
241 JArray
<jclass
> **arg_types_out
,
242 jclass
*return_type_out
)
245 _Jv_Utf8Const
* sig
= method
->signature
;
246 java::lang::ClassLoader
*loader
= declaringClass
->getClassLoaderInternal();
247 char *ptr
= sig
->chars();
249 /* First just count the number of parameters. */
275 while (*ptr
!= ';' && ptr
[1] != '\0');
281 JArray
<jclass
> *args
= (JArray
<jclass
> *)
282 JvNewObjectArray (numArgs
, &java::lang::Class::class$
, NULL
);
283 jclass
* argPtr
= elements (args
);
284 for (ptr
= sig
->chars(); *ptr
!= '\0'; ptr
++)
288 for (; *ptr
== '['; ptr
++)
295 argPtr
= return_type_out
;
308 type
= _Jv_FindClassFromSignature(ptr
, loader
);
311 type
= _Jv_FindClassFromSignature(ptr
, loader
);
314 while (*ptr
!= ';' && ptr
[1] != '\0');
318 while (--num_arrays
>= 0)
319 type
= _Jv_GetArrayClass (type
, loader
);
320 // ARGPTR can be NULL if we are processing the return value of a
321 // call from Constructor.
325 *arg_types_out
= args
;
328 // This is a very rough analog of the JNI CallNonvirtual<type>MethodA
329 // functions. It handles both Methods and Constructors, and it can
330 // handle any return type. In the Constructor case, the `obj'
331 // argument is unused and should be NULL; also, the `return_type' is
332 // the class that the constructor will construct. RESULT is a pointer
333 // to a `jvalue' (see jni.h); for a void method this should be NULL.
334 // This function returns an exception (if one was thrown), or NULL if
337 _Jv_CallAnyMethodA (jobject obj
,
340 jboolean is_constructor
,
341 jboolean is_virtual_call
,
342 JArray
<jclass
> *parameter_types
,
345 jboolean is_jni_call
,
348 using namespace java::lang::reflect
;
351 JvAssert (! is_constructor
|| ! obj
);
352 JvAssert (! is_constructor
|| return_type
);
354 // See whether call needs an object as the first argument. A
355 // constructor does need a `this' argument, but it is one we create.
356 jboolean needs_this
= false;
358 || ! Modifier::isStatic(meth
->accflags
))
361 int param_count
= parameter_types
->length
;
366 // A constructor itself always returns void.
367 if (is_constructor
|| return_type
== JvPrimClass (void))
368 rtype
= &ffi_type_void
;
370 rtype
= get_ffi_type (return_type
);
371 ffi_type
**argtypes
= (ffi_type
**) __builtin_alloca (param_count
372 * sizeof (ffi_type
*));
374 jclass
*paramelts
= elements (parameter_types
);
376 // Special case for the `this' argument of a constructor. Note that
377 // the JDK 1.2 docs specify that the new object must be allocated
378 // before argument conversions are done.
380 obj
= _Jv_AllocObject (return_type
);
382 const int size_per_arg
= sizeof(jvalue
);
385 char *p
= (char *) __builtin_alloca (param_count
* size_per_arg
);
386 // Overallocate to get correct alignment.
387 void **values
= (void **)
388 __builtin_alloca (param_count
* sizeof (void *));
393 // The `NULL' type is `Object'.
394 argtypes
[i
] = get_ffi_type (NULL
);
396 memcpy (p
, &obj
, sizeof (jobject
));
401 for (int arg
= 0; i
< param_count
; ++i
, ++arg
)
405 argtypes
[i
] = get_ffi_type (paramelts
[arg
]);
406 if (paramelts
[arg
]->isPrimitive())
407 tsize
= paramelts
[arg
]->size();
409 tsize
= sizeof (jobject
);
411 // Copy appropriate bits from the jvalue into the ffi array.
412 // FIXME: we could do this copying all in one loop, above, by
413 // over-allocating a bit.
414 // How do we do this without breaking big-endian platforms?
416 memcpy (p
, &args
[arg
], tsize
);
420 if (ffi_prep_cif (&cif
, FFI_DEFAULT_ABI
, param_count
,
421 rtype
, argtypes
) != FFI_OK
)
422 throw new java::lang::VirtualMachineError(JvNewStringLatin1("internal error: ffi_prep_cif failed"));
424 using namespace java::lang
;
425 using namespace java::lang::reflect
;
443 case FFI_TYPE_SINT16
:
446 case FFI_TYPE_UINT16
:
449 case FFI_TYPE_SINT32
:
452 case FFI_TYPE_SINT64
:
458 case FFI_TYPE_DOUBLE
:
461 case FFI_TYPE_POINTER
:
465 JvFail ("Unknown ffi_call return type");
471 // FIXME: If a vtable index is -1 at this point it is invalid, so we
472 // have to use the ncode.
474 // This can happen because methods in final classes don't have
475 // vtable entries, but _Jv_isVirtualMethod() doesn't know that. We
476 // could solve this problem by allocating a vtable index for methods
479 && ! Modifier::isFinal (meth
->accflags
)
480 && (_Jv_ushort
)-1 != meth
->index
)
482 _Jv_VTable
*vtable
= *(_Jv_VTable
**) obj
;
485 if (is_jni_call
&& Modifier::isAbstract (meth
->accflags
))
487 // With JNI we don't know if this is an interface call
488 // or a call to an abstract method. Look up the method
489 // by name, the slow way.
490 _Jv_Method
*concrete_meth
491 = _Jv_LookupDeclaredMethod (vtable
->clas
,
495 if (concrete_meth
== NULL
496 || concrete_meth
->ncode
== NULL
497 || Modifier::isAbstract(concrete_meth
->accflags
))
498 throw new java::lang::IncompatibleClassChangeError
499 (_Jv_GetMethodString (vtable
->clas
, meth
));
500 ncode
= concrete_meth
->ncode
;
503 ncode
= vtable
->get_method (meth
->index
);
506 ncode
= _Jv_LookupInterfaceMethodIdx (vtable
->clas
, iface
,
516 ffi_call (&cif
, (void (*)()) ncode
, &ffi_result
, values
);
518 catch (Throwable
*ex
)
520 // For JNI we just throw the real error. For reflection, we
521 // wrap the underlying method's exception in an
522 // InvocationTargetException.
524 ex
= new InvocationTargetException (ex
);
528 // Since ffi_call returns integer values promoted to a word, use
529 // a narrowing conversion for jbyte, jchar, etc. results.
530 // Note that boolean is handled either by the FFI_TYPE_SINT8 or
531 // FFI_TYPE_SINT32 case.
541 result
->b
= (jbyte
)ffi_result
.i
;
543 case FFI_TYPE_SINT16
:
544 result
->s
= (jshort
)ffi_result
.i
;
546 case FFI_TYPE_UINT16
:
547 result
->c
= (jchar
)ffi_result
.i
;
549 case FFI_TYPE_SINT32
:
550 result
->i
= (jint
)ffi_result
.i
;
552 case FFI_TYPE_SINT64
:
553 result
->j
= (jlong
)ffi_result
.l
;
556 result
->f
= (jfloat
)ffi_result
.f
;
558 case FFI_TYPE_DOUBLE
:
559 result
->d
= (jdouble
)ffi_result
.d
;
561 case FFI_TYPE_POINTER
:
562 result
->l
= (jobject
)ffi_result
.o
;
565 JvFail ("Unknown ffi_call return type");
570 throw new java::lang::UnsupportedOperationException(JvNewStringLatin1("reflection not available in this build"));
574 // This is another version of _Jv_CallAnyMethodA, but this one does
575 // more checking and is used by the reflection (and not JNI) code.
577 _Jv_CallAnyMethodA (jobject obj
,
580 jboolean is_constructor
,
581 JArray
<jclass
> *parameter_types
,
585 if (parameter_types
->length
== 0 && args
== NULL
)
587 // The JDK accepts this, so we do too.
589 else if (parameter_types
->length
!= args
->length
)
590 throw new java::lang::IllegalArgumentException
;
592 int param_count
= parameter_types
->length
;
594 jclass
*paramelts
= elements (parameter_types
);
595 jobject
*argelts
= args
== NULL
? NULL
: elements (args
);
596 jvalue argvals
[param_count
];
598 #define COPY(Where, What, Type) \
601 memcpy ((Where), &val, sizeof (Type)); \
604 for (int i
= 0; i
< param_count
; ++i
)
606 jclass k
= argelts
[i
] ? argelts
[i
]->getClass() : NULL
;
607 if (paramelts
[i
]->isPrimitive())
611 || ! can_widen (k
, paramelts
[i
]))
612 throw new java::lang::IllegalArgumentException
;
614 if (paramelts
[i
] == JvPrimClass (boolean
))
616 ((java::lang::Boolean
*) argelts
[i
])->booleanValue(),
618 else if (paramelts
[i
] == JvPrimClass (char))
620 ((java::lang::Character
*) argelts
[i
])->charValue(),
624 java::lang::Number
*num
= (java::lang::Number
*) argelts
[i
];
625 if (paramelts
[i
] == JvPrimClass (byte
))
626 COPY (&argvals
[i
], num
->byteValue(), jbyte
);
627 else if (paramelts
[i
] == JvPrimClass (short))
628 COPY (&argvals
[i
], num
->shortValue(), jshort
);
629 else if (paramelts
[i
] == JvPrimClass (int))
630 COPY (&argvals
[i
], num
->intValue(), jint
);
631 else if (paramelts
[i
] == JvPrimClass (long))
632 COPY (&argvals
[i
], num
->longValue(), jlong
);
633 else if (paramelts
[i
] == JvPrimClass (float))
634 COPY (&argvals
[i
], num
->floatValue(), jfloat
);
635 else if (paramelts
[i
] == JvPrimClass (double))
636 COPY (&argvals
[i
], num
->doubleValue(), jdouble
);
641 if (argelts
[i
] && ! paramelts
[i
]->isAssignableFrom (k
))
642 throw new java::lang::IllegalArgumentException
;
643 COPY (&argvals
[i
], argelts
[i
], jobject
);
648 _Jv_CallAnyMethodA (obj
, return_type
, meth
, is_constructor
,
649 _Jv_isVirtualMethod (meth
),
650 parameter_types
, argvals
, &ret_value
,
654 #define VAL(Wrapper, Field) (new Wrapper (ret_value.Field))
657 else if (return_type
== JvPrimClass (byte
))
658 r
= VAL (java::lang::Byte
, b
);
659 else if (return_type
== JvPrimClass (short))
660 r
= VAL (java::lang::Short
, s
);
661 else if (return_type
== JvPrimClass (int))
662 r
= VAL (java::lang::Integer
, i
);
663 else if (return_type
== JvPrimClass (long))
664 r
= VAL (java::lang::Long
, j
);
665 else if (return_type
== JvPrimClass (float))
666 r
= VAL (java::lang::Float
, f
);
667 else if (return_type
== JvPrimClass (double))
668 r
= VAL (java::lang::Double
, d
);
669 else if (return_type
== JvPrimClass (boolean
))
670 r
= VAL (java::lang::Boolean
, z
);
671 else if (return_type
== JvPrimClass (char))
672 r
= VAL (java::lang::Character
, c
);
673 else if (return_type
== JvPrimClass (void))
677 JvAssert (return_type
== NULL
|| ! return_type
->isPrimitive());