Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I was sitting in the middle of a boring presentation, and I saw this post and thought "Hey, I'm pretty bored at the moment, I'll write an auto-dispatcher for him!". Of course, by the I finished it, samtregar had already responded with the correct solution. :)

But, I'm going to post my solution anyways (even though it definitely isn't the optimal solution here), just because you might find it interesting. Also note that I do not have java/perl access on the lab computers that we are using, so this code is most definitely untested.

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 fo +r 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 fo +r 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); }

Usage would be:

my $obj = Inline::Java::Classfile->new("SomeClass", [$cntr_arg1, $ +arg2, $etc]); print $obj->hashcode(); # or whatever method

In reply to Re: Using Java methods in perl by jryan
in thread Using Java methods in perl by AcidHawk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found