Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Howdy all!

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Howdy all!

    This is my first post and I guess everyone must do their first one here. I've been programming professionally in Java off and on since 1998 - but I don't think of myself as an expert by any means.

    I have encountered a Java issue that I feel certain I solved once 8 years ago, but I can't remember how I did it, I left the code at that employers, and Google hasn't helped me resolve it this time. It involves making a call to a method in an already instantiated class without knowing until runtime which class that will be.

    On the one hand, I could write a case block that would match the String generated method name against a list of known methods and then execute a hard coded method call from there. But - in this case there are over 300 possible methods (all getters) so I'm hoping there's a more elegant way to do it. What I'm hoping to do is execute the appropriate method by name using a String I have generated at runtime to point to the correct method.

    I've looked at reflection as a possibility a lot - but the Java.lang.reflect.Method.invoke class seems to insist on instantiating it's own copy of the class - and that would be very slow since there's a SQL Query involved that I'm hoping to only run once for all ~300 calls. Ideally, the class has already been instantiated, the query has already run, and the 300+ fields are already accessible via 300+ getters.

    What do you think? Does anyone know what I'm looking for?

    Thanks


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Howdy all!

    Hello sludge.

    Welcome to the Java Programming Forums. It's always good to have a seasoned professional join the community

    Hmm.. I'm going to have to look into this one myself! I will post back as soon as possible.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Howdy all!

    Welcome...

    To address your question, consider writing an interface whose implementation runs the method in question. Then store the references to each via a Map keyed with the appropriate string and valued with the appropriate interface implementation. For 300 it might be hard to initially hard code the implementations, but - if you want to load at runtime - you could use reflection once to create the single instance of the object in question and populate the Map values with the actual Method object (then just retrieve the Method from the Map for a given string and invoke the method).

  4. The Following User Says Thank You to copeg For This Useful Post:

    JavaPF (January 18th, 2011)

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Howdy all!

    Thanks for the welcome, guys.

    copeg - I think that would work, and it's pretty elegant too, but what I'm looking for is simpler. What I recall from the last time I was in this situation is that there is a way to create the call at runtime without doing the legwork ahead of time. It was, like, 3 lines of code and worked great.

    What I'm trying to do is a little similar to the Oracle 'execute immidiate' technology in which a string is treated as a command. This allows you to dynamically create a call to an existing method at runtime. It's sort of a dynamic link and exectue.

    At this point, I've already coded it as a 1000+ line 'if-else if...' - but I would happily replace that with the above if I could only remember how I did it! I think it would be less efficient, but be much easier to maintain.

Similar Threads

  1. howdy..
    By drago34 in forum Member Introductions
    Replies: 3
    Last Post: October 5th, 2009, 02:16 AM