Groovy - adding methods similar to JS prototype?












1















With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.



I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.



So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.



To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.



ObjectOption.prototype.object_0 = function(player, object) { }


And then I'm able to execute the functions from the actual JS class itself via



ObjectOption.prototype["object_" + object.getID()](player, object);


or



this["object_" + object.getID()](player, object);


My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?










share|improve this question























  • Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.

    – Dave Newton
    Nov 13 '18 at 22:18
















1















With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.



I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.



So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.



To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.



ObjectOption.prototype.object_0 = function(player, object) { }


And then I'm able to execute the functions from the actual JS class itself via



ObjectOption.prototype["object_" + object.getID()](player, object);


or



this["object_" + object.getID()](player, object);


My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?










share|improve this question























  • Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.

    – Dave Newton
    Nov 13 '18 at 22:18














1












1








1








With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.



I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.



So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.



To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.



ObjectOption.prototype.object_0 = function(player, object) { }


And then I'm able to execute the functions from the actual JS class itself via



ObjectOption.prototype["object_" + object.getID()](player, object);


or



this["object_" + object.getID()](player, object);


My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?










share|improve this question














With the future of JS on Java unclear (JS/Nashorn deprecated on Java 11), I've been looking into options completely outside of JS, Groovy being a major one given its popularity and similarity to Java.



I currently use JS/Nasahorn to handle a lot of actions for a game server, and while a lot of it I can see can be converted to Groovy, I do have one concern that I'm having trouble figuring out if possible or not.



So with my game server, there are new clickable map objects and npcs being added all the time (with a total of each being in the tens of thousands). To deal with the click actions of these, I have JS classes to handle them.



To add a new object or npc action, I have groups of JS files that split the ids up so that it isn't one giant file. Specifically, I use the JS prototype to be able to add these actions in without being the same file, and just load all the JS files at runtime.



ObjectOption.prototype.object_0 = function(player, object) { }


And then I'm able to execute the functions from the actual JS class itself via



ObjectOption.prototype["object_" + object.getID()](player, object);


or



this["object_" + object.getID()](player, object);


My question is, can any such setup be done with Groovy, or is this outside the scope of how Groovy works?







groovy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 22:09









PalidinoDHPalidinoDH

65




65













  • Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.

    – Dave Newton
    Nov 13 '18 at 22:18



















  • Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.

    – Dave Newton
    Nov 13 '18 at 22:18

















Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.

– Dave Newton
Nov 13 '18 at 22:18





Yes, you can, in a variety of ways (see the metaprogramming docs), but I don't see a need to do it like this, at least not yet. Why can't you just instantiate whatever sort of handler this is and add it to a hash keyed by ID when you need them? They're on the classpath and and can be instantiated at will. Perhaps I'm not really understanding the usecase here yet.

– Dave Newton
Nov 13 '18 at 22:18












1 Answer
1






active

oldest

votes


















1














Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:



class Person {
void greetings(String name) {
println "Greetings, $name!"
}
}

Person.metaClass.hello = { String name -> println "Hello, $name!" }

def john = new Person()
john.greetings("Paul")
john.hello("Mary")


Output:



Greetings, Paul!
Hello, Mary!


In this example, we have a simple Person class with a single method greetings(). We are able to add a new method hello() by accessing Person.metaClass object and adding method hello as a closure that accepts a single parameter name of type String.



Check the Groovy's documentation on metaprogramming, you will find many examples there.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290252%2fgroovy-adding-methods-similar-to-js-prototype%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:



    class Person {
    void greetings(String name) {
    println "Greetings, $name!"
    }
    }

    Person.metaClass.hello = { String name -> println "Hello, $name!" }

    def john = new Person()
    john.greetings("Paul")
    john.hello("Mary")


    Output:



    Greetings, Paul!
    Hello, Mary!


    In this example, we have a simple Person class with a single method greetings(). We are able to add a new method hello() by accessing Person.metaClass object and adding method hello as a closure that accepts a single parameter name of type String.



    Check the Groovy's documentation on metaprogramming, you will find many examples there.






    share|improve this answer




























      1














      Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:



      class Person {
      void greetings(String name) {
      println "Greetings, $name!"
      }
      }

      Person.metaClass.hello = { String name -> println "Hello, $name!" }

      def john = new Person()
      john.greetings("Paul")
      john.hello("Mary")


      Output:



      Greetings, Paul!
      Hello, Mary!


      In this example, we have a simple Person class with a single method greetings(). We are able to add a new method hello() by accessing Person.metaClass object and adding method hello as a closure that accepts a single parameter name of type String.



      Check the Groovy's documentation on metaprogramming, you will find many examples there.






      share|improve this answer


























        1












        1








        1







        Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:



        class Person {
        void greetings(String name) {
        println "Greetings, $name!"
        }
        }

        Person.metaClass.hello = { String name -> println "Hello, $name!" }

        def john = new Person()
        john.greetings("Paul")
        john.hello("Mary")


        Output:



        Greetings, Paul!
        Hello, Mary!


        In this example, we have a simple Person class with a single method greetings(). We are able to add a new method hello() by accessing Person.metaClass object and adding method hello as a closure that accepts a single parameter name of type String.



        Check the Groovy's documentation on metaprogramming, you will find many examples there.






        share|improve this answer













        Groovy supports metaprogramming that allows you e.g. adding new methods to existing classes at the runtime. Consider following example:



        class Person {
        void greetings(String name) {
        println "Greetings, $name!"
        }
        }

        Person.metaClass.hello = { String name -> println "Hello, $name!" }

        def john = new Person()
        john.greetings("Paul")
        john.hello("Mary")


        Output:



        Greetings, Paul!
        Hello, Mary!


        In this example, we have a simple Person class with a single method greetings(). We are able to add a new method hello() by accessing Person.metaClass object and adding method hello as a closure that accepts a single parameter name of type String.



        Check the Groovy's documentation on metaprogramming, you will find many examples there.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 8:20









        Szymon StepniakSzymon Stepniak

        17.3k83263




        17.3k83263






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290252%2fgroovy-adding-methods-similar-to-js-prototype%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma