1 // natMethod.cc - Native code for Method class.
3 /* Copyright (C) 1998, 1999, 2000, 2001 , 2002 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/IllegalArgumentException.h>
32 #include <java/lang/NullPointerException.h>
33 #include <java/lang/Class.h>
34 #include <gcj/method.h>
35 #include <gnu/gcj/RawData.h>
42 #include <java/lang/UnsupportedOperationException.h>
51 // This is used to determine when a primitive widening conversion is
53 static cpair primitives
[] =
56 { JvPrimClass (boolean
), &java::lang::Boolean::class$
},
57 { JvPrimClass (byte
), &java::lang::Byte::class$
},
59 { JvPrimClass (short), &java::lang::Short::class$
},
61 { JvPrimClass (char), &java::lang::Character::class$
},
62 { JvPrimClass (int), &java::lang::Integer::class$
},
63 { JvPrimClass (long), &java::lang::Long::class$
},
64 { JvPrimClass (float), &java::lang::Float::class$
},
65 { JvPrimClass (double), &java::lang::Double::class$
},
69 static inline jboolean
70 can_widen (jclass from
, jclass to
)
72 int fromx
= -1, tox
= -1;
74 for (int i
= 0; primitives
[i
].prim
; ++i
)
76 if (primitives
[i
].wrap
== from
)
78 if (primitives
[i
].prim
== to
)
82 // Can't handle a miss.
83 if (fromx
== -1 || tox
== -1)
85 // Boolean arguments may not be widened.
86 if (fromx
== BOOLEAN
&& tox
!= BOOLEAN
)
88 // Nothing promotes to char.
89 if (tox
== CHAR
&& fromx
!= CHAR
)
96 static inline ffi_type
*
97 get_ffi_type (jclass klass
)
101 return &ffi_type_pointer
;
104 if (klass
== JvPrimClass (byte
))
106 else if (klass
== JvPrimClass (short))
107 r
= &ffi_type_sint16
;
108 else if (klass
== JvPrimClass (int))
109 r
= &ffi_type_sint32
;
110 else if (klass
== JvPrimClass (long))
111 r
= &ffi_type_sint64
;
112 else if (klass
== JvPrimClass (float))
114 else if (klass
== JvPrimClass (double))
115 r
= &ffi_type_double
;
116 else if (klass
== JvPrimClass (boolean
))
118 // On some platforms a bool is a byte, on others an int.
119 if (sizeof (jboolean
) == sizeof (jbyte
))
123 JvAssert (sizeof (jboolean
) == sizeof (jint
));
124 r
= &ffi_type_sint32
;
127 else if (klass
== JvPrimClass (char))
128 r
= &ffi_type_uint16
;
131 JvAssert (! klass
->isPrimitive());
132 r
= &ffi_type_pointer
;
140 java::lang::reflect::Method::invoke (jobject obj
, jobjectArray args
)
142 if (parameter_types
== NULL
)
145 jmethodID meth
= _Jv_FromReflectedMethod (this);
146 if (! java::lang::reflect::Modifier::isStatic(meth
->accflags
))
148 jclass k
= obj
? obj
->getClass() : NULL
;
150 throw new java::lang::NullPointerException
;
151 if (! declaringClass
->isAssignableFrom(k
))
152 throw new java::lang::IllegalArgumentException
;
153 // FIXME: access checks.
155 // Find the possibly overloaded method based on the runtime type
157 meth
= _Jv_LookupDeclaredMethod (k
, meth
->name
, meth
->signature
);
161 // We have to initialize a static class. It is safe to do this
162 // here and not in _Jv_CallAnyMethodA because JNI initializes a
163 // class whenever a method lookup is done.
164 _Jv_InitClass (declaringClass
);
167 return _Jv_CallAnyMethodA (obj
, return_type
, meth
, false,
168 parameter_types
, args
);
172 java::lang::reflect::Method::getModifiers ()
174 // Ignore all unknown flags.
175 return _Jv_FromReflectedMethod (this)->accflags
& Modifier::ALL_FLAGS
;
179 java::lang::reflect::Method::getName ()
182 name
= _Jv_NewStringUtf8Const (_Jv_FromReflectedMethod (this)->name
);
186 /* Internal method to set return_type and parameter_types fields. */
189 java::lang::reflect::Method::getType ()
191 _Jv_Method
*method
= _Jv_FromReflectedMethod (this);
192 _Jv_GetTypesFromSignature (method
,
198 if (method
->throws
!= NULL
)
200 while (method
->throws
[count
] != NULL
)
205 = (JArray
<jclass
> *) JvNewObjectArray (count
, &java::lang::Class::class$
,
207 jclass
*elts
= elements (exception_types
);
208 for (int i
= 0; i
< count
; ++i
)
209 elts
[i
] = _Jv_FindClass (method
->throws
[i
],
210 declaringClass
->getClassLoader ());
214 _Jv_GetTypesFromSignature (jmethodID method
,
215 jclass declaringClass
,
216 JArray
<jclass
> **arg_types_out
,
217 jclass
*return_type_out
)
220 _Jv_Utf8Const
* sig
= method
->signature
;
221 java::lang::ClassLoader
*loader
= declaringClass
->getClassLoader();
222 char *ptr
= sig
->data
;
224 /* First just count the number of parameters. */
250 while (*ptr
!= ';' && ptr
[1] != '\0');
256 JArray
<jclass
> *args
= (JArray
<jclass
> *)
257 JvNewObjectArray (numArgs
, &java::lang::Class::class$
, NULL
);
258 jclass
* argPtr
= elements (args
);
259 for (ptr
= sig
->data
; *ptr
!= '\0'; ptr
++)
263 for (; *ptr
== '['; ptr
++)
270 argPtr
= return_type_out
;
283 type
= _Jv_FindClassFromSignature(ptr
, loader
);
286 type
= _Jv_FindClassFromSignature(ptr
, loader
);
289 while (*ptr
!= ';' && ptr
[1] != '\0');
293 // FIXME: 2'nd argument should be "current loader"
294 while (--num_arrays
>= 0)
295 type
= _Jv_GetArrayClass (type
, 0);
296 // ARGPTR can be NULL if we are processing the return value of a
297 // call from Constructor.
301 *arg_types_out
= args
;
304 // This is a very rough analog of the JNI CallNonvirtual<type>MethodA
305 // functions. It handles both Methods and Constructors, and it can
306 // handle any return type. In the Constructor case, the `obj'
307 // argument is unused and should be NULL; also, the `return_type' is
308 // the class that the constructor will construct. RESULT is a pointer
309 // to a `jvalue' (see jni.h); for a void method this should be NULL.
310 // This function returns an exception (if one was thrown), or NULL if
313 _Jv_CallAnyMethodA (jobject obj
,
316 jboolean is_constructor
,
317 JArray
<jclass
> *parameter_types
,
322 JvAssert (! is_constructor
|| ! obj
);
323 JvAssert (! is_constructor
|| return_type
);
325 // See whether call needs an object as the first argument. A
326 // constructor does need a `this' argument, but it is one we create.
327 jboolean needs_this
= false;
329 || ! java::lang::reflect::Modifier::isStatic(meth
->accflags
))
332 int param_count
= parameter_types
->length
;
337 // A constructor itself always returns void.
338 if (is_constructor
|| return_type
== JvPrimClass (void))
339 rtype
= &ffi_type_void
;
341 rtype
= get_ffi_type (return_type
);
342 ffi_type
**argtypes
= (ffi_type
**) __builtin_alloca (param_count
343 * sizeof (ffi_type
*));
345 jclass
*paramelts
= elements (parameter_types
);
347 // FIXME: at some point the compiler is going to add extra arguments
348 // to some functions. In particular we are going to do this for
349 // handling access checks in reflection. We must add these hidden
352 // Special case for the `this' argument of a constructor. Note that
353 // the JDK 1.2 docs specify that the new object must be allocated
354 // before argument conversions are done.
357 // FIXME: must special-case String, arrays, maybe others here.
358 obj
= JvAllocObject (return_type
);
365 // The `NULL' type is `Object'.
366 argtypes
[i
++] = get_ffi_type (NULL
);
367 size
+= sizeof (jobject
);
370 for (int arg
= 0; i
< param_count
; ++i
, ++arg
)
372 argtypes
[i
] = get_ffi_type (paramelts
[arg
]);
373 if (paramelts
[arg
]->isPrimitive())
374 size
+= paramelts
[arg
]->size();
376 size
+= sizeof (jobject
);
380 if (ffi_prep_cif (&cif
, FFI_DEFAULT_ABI
, param_count
,
381 rtype
, argtypes
) != FFI_OK
)
383 // FIXME: throw some kind of VirtualMachineError here.
386 char *p
= (char *) __builtin_alloca (size
);
387 void **values
= (void **) __builtin_alloca (param_count
* sizeof (void *));
393 memcpy (p
, &obj
, sizeof (jobject
));
394 p
+= sizeof (jobject
);
398 for (int arg
= 0; i
< param_count
; ++i
, ++arg
)
401 if (paramelts
[arg
]->isPrimitive())
402 tsize
= paramelts
[arg
]->size();
404 tsize
= sizeof (jobject
);
406 // Copy appropriate bits from the jvalue into the ffi array.
407 // FIXME: we could do this copying all in one loop, above, by
408 // over-allocating a bit.
410 memcpy (p
, &args
[arg
], tsize
);
414 using namespace java::lang
;
415 using namespace java::lang::reflect
;
417 Throwable
*ex
= NULL
;
430 ffi_call (&cif
, (void (*)()) meth
->ncode
, &ffi_result
, values
);
432 catch (Throwable
*ex2
)
434 // FIXME: this is wrong for JNI. But if we just return the
435 // exception, then the non-JNI cases won't be able to
436 // distinguish it from exceptions we might generate ourselves.
438 ex
= new InvocationTargetException (ex2
);
441 // Since ffi_call returns integer values promoted to a word, use
442 // a narrowing conversion for jbyte, jchar, etc. results.
443 // Note that boolean is handled either by the FFI_TYPE_SINT8 or
444 // FFI_TYPE_SINT32 case.
454 result
->b
= (jbyte
)ffi_result
.i
;
456 case FFI_TYPE_SINT16
:
457 result
->s
= (jshort
)ffi_result
.i
;
459 case FFI_TYPE_UINT16
:
460 result
->c
= (jchar
)ffi_result
.i
;
462 case FFI_TYPE_SINT32
:
463 result
->i
= (jint
)ffi_result
.i
;
465 case FFI_TYPE_SINT64
:
466 result
->j
= (jlong
)ffi_result
.l
;
469 result
->f
= (jfloat
)ffi_result
.f
;
471 case FFI_TYPE_DOUBLE
:
472 result
->d
= (jdouble
)ffi_result
.d
;
474 case FFI_TYPE_POINTER
:
475 result
->l
= (jobject
)ffi_result
.o
;
478 JvFail ("Unknown ffi_call return type");
485 throw new java::lang::UnsupportedOperationException
;
490 // This is another version of _Jv_CallAnyMethodA, but this one does
491 // more checking and is used by the reflection (and not JNI) code.
493 _Jv_CallAnyMethodA (jobject obj
,
496 jboolean is_constructor
,
497 JArray
<jclass
> *parameter_types
,
500 // FIXME: access checks.
502 if (parameter_types
->length
== 0 && args
== NULL
)
504 // The JDK accepts this, so we do too.
506 else if (parameter_types
->length
!= args
->length
)
507 throw new java::lang::IllegalArgumentException
;
509 int param_count
= parameter_types
->length
;
511 jclass
*paramelts
= elements (parameter_types
);
512 jobject
*argelts
= args
== NULL
? NULL
: elements (args
);
513 jvalue argvals
[param_count
];
515 #define COPY(Where, What, Type) \
518 memcpy ((Where), &val, sizeof (Type)); \
521 for (int i
= 0; i
< param_count
; ++i
)
523 jclass k
= argelts
[i
] ? argelts
[i
]->getClass() : NULL
;
524 if (paramelts
[i
]->isPrimitive())
528 || ! can_widen (k
, paramelts
[i
]))
529 throw new java::lang::IllegalArgumentException
;
531 if (paramelts
[i
] == JvPrimClass (boolean
))
533 ((java::lang::Boolean
*) argelts
[i
])->booleanValue(),
535 else if (paramelts
[i
] == JvPrimClass (char))
537 ((java::lang::Character
*) argelts
[i
])->charValue(),
541 java::lang::Number
*num
= (java::lang::Number
*) argelts
[i
];
542 if (paramelts
[i
] == JvPrimClass (byte
))
543 COPY (&argvals
[i
], num
->byteValue(), jbyte
);
544 else if (paramelts
[i
] == JvPrimClass (short))
545 COPY (&argvals
[i
], num
->shortValue(), jshort
);
546 else if (paramelts
[i
] == JvPrimClass (int))
547 COPY (&argvals
[i
], num
->intValue(), jint
);
548 else if (paramelts
[i
] == JvPrimClass (long))
549 COPY (&argvals
[i
], num
->longValue(), jlong
);
550 else if (paramelts
[i
] == JvPrimClass (float))
551 COPY (&argvals
[i
], num
->floatValue(), jfloat
);
552 else if (paramelts
[i
] == JvPrimClass (double))
553 COPY (&argvals
[i
], num
->doubleValue(), jdouble
);
558 if (argelts
[i
] && ! paramelts
[i
]->isAssignableFrom (k
))
559 throw new java::lang::IllegalArgumentException
;
560 COPY (&argvals
[i
], argelts
[i
], jobject
);
565 java::lang::Throwable
*ex
= _Jv_CallAnyMethodA (obj
,
577 #define VAL(Wrapper, Field) (new Wrapper (ret_value.Field))
580 else if (return_type
== JvPrimClass (byte
))
581 r
= VAL (java::lang::Byte
, b
);
582 else if (return_type
== JvPrimClass (short))
583 r
= VAL (java::lang::Short
, s
);
584 else if (return_type
== JvPrimClass (int))
585 r
= VAL (java::lang::Integer
, i
);
586 else if (return_type
== JvPrimClass (long))
587 r
= VAL (java::lang::Long
, j
);
588 else if (return_type
== JvPrimClass (float))
589 r
= VAL (java::lang::Float
, f
);
590 else if (return_type
== JvPrimClass (double))
591 r
= VAL (java::lang::Double
, d
);
592 else if (return_type
== JvPrimClass (boolean
))
593 r
= VAL (java::lang::Boolean
, z
);
594 else if (return_type
== JvPrimClass (char))
595 r
= VAL (java::lang::Character
, c
);
596 else if (return_type
== JvPrimClass (void))
600 JvAssert (return_type
== NULL
|| ! return_type
->isPrimitive());