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 7 of 7

Thread: Possible to check if classes contains a specific method and then run that method?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question Possible to check if classes contains a specific method and then run that method?

    Hello!
    I'm working on a simple 2D Game Framework. Currently it's creating a window, but I'm working on an update method.
    I constantly export the framework as a .jar file, and I've added it to the game's build path (using eclipse). I wonder if I can check all classes in the game-project (the project in which I've refecrenced the Framework in) for a specific method (for example update()) and call all found methods with that name from the framework's main class, like this:

    public class FrameworkClass1 {
     
        private void checkForUpdateMethods() {
            // Check for update methods on program start
        }
     
        public void update() {
            runAllFoundUpdateMethods();     // Run all update methods found in the scan
        }
    }

    I wan't to do this because it would be a simple way to update and render the game.
    If the main game-class look something like this:

    public class Game {
        public void update() {
            // Update the game every time this is ran
        }
    }

    it will be automatically updated, because it contains a method named update(), instead of naming the main game-class with a specific name etc.
    It will simply be more flexible that way!

    Thanks!
    /TheDDestroyer12


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Possible to check if classes contains a specific method and then run that method?

    How about an interface "Updateable" which provides the "update()" method?
    Then you could simply have a List of Updateables in your framework and update all updateables on a regular basis.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Possible to check if classes contains a specific method and then run that method?

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    There are no wildcard calls like: *.update(), where * would be a wildcard for any class name. And those would have to be static calls.

    I don't know enough about your design to make an informed suggestion, but it seems that the multiple update() methods or the objects that contain them could be responsible for knowing when the updates should occur by monitoring events from a game state controller. You might look into the Observer/Observable and Event Bus patterns to see if those would help simplify your design.

  4. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by Cornix View Post
    How about an interface "Updateable" which provides the "update()" method?
    Then you could simply have a List of Updateables in your framework and update all updateables on a regular basis.
    I'm not very familiar with interfaces. Can you pleas explain a little bit more in detail how I can use them for this?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Possible to check if classes contains a specific method and then run that method?

    The Reflection packages might be useful. It could be used to check class files for methods.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Possible to check if classes contains a specific method and then run that method?

    Quote Originally Posted by TheDDestroyer12 View Post
    I'm not very familiar with interfaces. Can you pleas explain a little bit more in detail how I can use them for this?
    I am not going to explain to you what an interface is since there is a lot to be explained about them, but you should definitely read up on them if you want to work with Java. They are a very important part of the language and the standard library and are especially useful when designing an API.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Okay, thanks! I'll try it.

Similar Threads

  1. class and check method
    By nepperso in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 23rd, 2013, 09:58 PM
  2. Help with method to check if two rectangles overlap
    By jikatz09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2013, 06:19 AM
  3. check for boolean method.
    By daitobu in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2012, 08:54 AM
  4. check for valid argument to a method
    By lupis in forum Exceptions
    Replies: 8
    Last Post: March 24th, 2012, 11:09 PM
  5. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 09:16 AM

Tags for this Thread