3/11 Java Q: How to do function pointers in Java? Specifically, are these
Perl codes portable to Java?
$hash =
{ 'var1' => { 'class' => 'Foo',
'fnct_ptr' => 'fooMethod',
'args' => ['a', 1, 'b'] },
'var2' => { 'class' => 'Bar',
'fnct_ptr' => 'barMethod',
'args' => {'arg1' => 'hello', 'arg2' => 'John'} }
};
sub process {
$type = shift;
my $class = $hash->{$type}{'class'};
my $fnct_ptr = $hash->{$type}{'fnct_ptr'};
my $args = $hash->{$type}{'args'};
return $class->$fnct_ptr($args);
}
I know I can do something like
Class myClass = Class.forname($classname);
for the class and either pass in a vector or hash for the args.
However, I have not been able to figure how to do the function pointer
concept.
Any suggestions and/or pointers would be greatly appreciated.
\_ Java explicitly forbids direct function pointers b/c of its security
policies. You can kind of do it with interfaces (define interface
FooInterface with methods foo_1 ... foo_n; then implement the
interface in FooClass; foo = new FooClass(); and pass foo (the
object) to whatever needs to run foo (the method).
\_ Assuming security policies are set to allow it, you can
do function pointer-like things using the java reflection
API. |