package Inline::Java::Classfile; sub new { my ($type, $args) = @_; Inline->bind(Java => << "__CODE__"); // package org.perl.cpan.inline.java; import java.lang.*; import java.lang.reflect.*; class InlineJavaDispatcher { protected $type inst; public InlineJavaDispatcher(Object[] args) throws InstantiationException ,IllegalAccessException ,IllegalArgumentException ,InvocationTargetException ,SecurityException { Class clazz = $type.class; Constructor[] cntrs = clazz.getDeclaredConstructors(); for (int i=0; i < cntrs.length(); i++) { try { inst = cntrs[i].newInstance(args); return; } catch (IllegalArgumentException e) { } } throw new IllegalArgumentException( methodName + " was not found for the arguments supplied for Class $type." ); } public Object dispatch(String methodName ,Object[] args) throws IllegalAccessException ,IllegalArgumentException ,InvocationTargetException ,NullPointerException ,ExceptionInInitializerError { Class clazz = inst.getClass(); Method am[] = inst.getDeclaredMethods(); for (int i=0; i < am.length; i++) { if (am[i].getName().equals(methodName)) { try { return am[i].invoke(inst, args); } catch (IllegalArgumentException e) { } } } throw new IllegalArgumentException( methodName + " was not found for the arguments supplied for Class $type." ); } } __CODE__ return InlineJavaDispatcher->new($args); } sub AUTOLOAD { my ($self,$args) = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion $self->dispatch($name, $args); } #### my $obj = Inline::Java::Classfile->new("SomeClass", [$cntr_arg1, $arg2, $etc]); print $obj->hashcode(); # or whatever method