Looping through class members and update member value in groovy












1















I have a question regarding loop through class members and update member value of the object in groovy:



class Test {    
String a
String b

Test(String a, String b) {
this.a = a
this.b = b
}

String toString() {
return "a is " + a + " b is " + b
}

}


And I want to loop through the object member and update the value of the member:



class Testing {
static void main(String args) {
Test test = new Test("hello", "world")
test.properties.findAll {
it.value.toString.equals('hello')
}.each {
it.setValue("new value")
}
}
}


I try to change the value of "hello" to "new value", looks like it can find the member contains the "hello", but the value the same after it.setvalue(), how to change the value of the member in the object in correct way?










share|improve this question



























    1















    I have a question regarding loop through class members and update member value of the object in groovy:



    class Test {    
    String a
    String b

    Test(String a, String b) {
    this.a = a
    this.b = b
    }

    String toString() {
    return "a is " + a + " b is " + b
    }

    }


    And I want to loop through the object member and update the value of the member:



    class Testing {
    static void main(String args) {
    Test test = new Test("hello", "world")
    test.properties.findAll {
    it.value.toString.equals('hello')
    }.each {
    it.setValue("new value")
    }
    }
    }


    I try to change the value of "hello" to "new value", looks like it can find the member contains the "hello", but the value the same after it.setvalue(), how to change the value of the member in the object in correct way?










    share|improve this question

























      1












      1








      1








      I have a question regarding loop through class members and update member value of the object in groovy:



      class Test {    
      String a
      String b

      Test(String a, String b) {
      this.a = a
      this.b = b
      }

      String toString() {
      return "a is " + a + " b is " + b
      }

      }


      And I want to loop through the object member and update the value of the member:



      class Testing {
      static void main(String args) {
      Test test = new Test("hello", "world")
      test.properties.findAll {
      it.value.toString.equals('hello')
      }.each {
      it.setValue("new value")
      }
      }
      }


      I try to change the value of "hello" to "new value", looks like it can find the member contains the "hello", but the value the same after it.setvalue(), how to change the value of the member in the object in correct way?










      share|improve this question














      I have a question regarding loop through class members and update member value of the object in groovy:



      class Test {    
      String a
      String b

      Test(String a, String b) {
      this.a = a
      this.b = b
      }

      String toString() {
      return "a is " + a + " b is " + b
      }

      }


      And I want to loop through the object member and update the value of the member:



      class Testing {
      static void main(String args) {
      Test test = new Test("hello", "world")
      test.properties.findAll {
      it.value.toString.equals('hello')
      }.each {
      it.setValue("new value")
      }
      }
      }


      I try to change the value of "hello" to "new value", looks like it can find the member contains the "hello", but the value the same after it.setvalue(), how to change the value of the member in the object in correct way?







      groovy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 13:34









      ratzipratzip

      4571722




      4571722
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Changing properties does not affect field value change. If you want to find a field that stores specific value, like hello, and change it to something else, then you can try doing it with setProperty method invoked on test object.



          Consider the following example:



          class Testing {
          static void main(String args) {
          Test test = new Test("hello", "world")

          test.properties.findAll {
          it.value == 'hello'
          }.each { field, _ ->
          test.setProperty(field as String, "new value")
          }

          println test
          }
          }


          Output:



          a is new value b is world





          share|improve this answer





















          • 1





            Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

            – billjamesdev
            Nov 14 '18 at 23:42













          • Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

            – Szymon Stepniak
            Nov 15 '18 at 5:35











          • why I can not set using set.Property()? intellij says the object of test does not have setProperty function

            – ratzip
            Nov 15 '18 at 13:12











          • I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

            – Szymon Stepniak
            Nov 15 '18 at 13:20











          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%2f53301485%2flooping-through-class-members-and-update-member-value-in-groovy%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









          0














          Changing properties does not affect field value change. If you want to find a field that stores specific value, like hello, and change it to something else, then you can try doing it with setProperty method invoked on test object.



          Consider the following example:



          class Testing {
          static void main(String args) {
          Test test = new Test("hello", "world")

          test.properties.findAll {
          it.value == 'hello'
          }.each { field, _ ->
          test.setProperty(field as String, "new value")
          }

          println test
          }
          }


          Output:



          a is new value b is world





          share|improve this answer





















          • 1





            Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

            – billjamesdev
            Nov 14 '18 at 23:42













          • Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

            – Szymon Stepniak
            Nov 15 '18 at 5:35











          • why I can not set using set.Property()? intellij says the object of test does not have setProperty function

            – ratzip
            Nov 15 '18 at 13:12











          • I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

            – Szymon Stepniak
            Nov 15 '18 at 13:20
















          0














          Changing properties does not affect field value change. If you want to find a field that stores specific value, like hello, and change it to something else, then you can try doing it with setProperty method invoked on test object.



          Consider the following example:



          class Testing {
          static void main(String args) {
          Test test = new Test("hello", "world")

          test.properties.findAll {
          it.value == 'hello'
          }.each { field, _ ->
          test.setProperty(field as String, "new value")
          }

          println test
          }
          }


          Output:



          a is new value b is world





          share|improve this answer





















          • 1





            Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

            – billjamesdev
            Nov 14 '18 at 23:42













          • Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

            – Szymon Stepniak
            Nov 15 '18 at 5:35











          • why I can not set using set.Property()? intellij says the object of test does not have setProperty function

            – ratzip
            Nov 15 '18 at 13:12











          • I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

            – Szymon Stepniak
            Nov 15 '18 at 13:20














          0












          0








          0







          Changing properties does not affect field value change. If you want to find a field that stores specific value, like hello, and change it to something else, then you can try doing it with setProperty method invoked on test object.



          Consider the following example:



          class Testing {
          static void main(String args) {
          Test test = new Test("hello", "world")

          test.properties.findAll {
          it.value == 'hello'
          }.each { field, _ ->
          test.setProperty(field as String, "new value")
          }

          println test
          }
          }


          Output:



          a is new value b is world





          share|improve this answer















          Changing properties does not affect field value change. If you want to find a field that stores specific value, like hello, and change it to something else, then you can try doing it with setProperty method invoked on test object.



          Consider the following example:



          class Testing {
          static void main(String args) {
          Test test = new Test("hello", "world")

          test.properties.findAll {
          it.value == 'hello'
          }.each { field, _ ->
          test.setProperty(field as String, "new value")
          }

          println test
          }
          }


          Output:



          a is new value b is world






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 13:17

























          answered Nov 14 '18 at 15:35









          Szymon StepniakSzymon Stepniak

          17.5k83364




          17.5k83364








          • 1





            Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

            – billjamesdev
            Nov 14 '18 at 23:42













          • Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

            – Szymon Stepniak
            Nov 15 '18 at 5:35











          • why I can not set using set.Property()? intellij says the object of test does not have setProperty function

            – ratzip
            Nov 15 '18 at 13:12











          • I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

            – Szymon Stepniak
            Nov 15 '18 at 13:20














          • 1





            Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

            – billjamesdev
            Nov 14 '18 at 23:42













          • Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

            – Szymon Stepniak
            Nov 15 '18 at 5:35











          • why I can not set using set.Property()? intellij says the object of test does not have setProperty function

            – ratzip
            Nov 15 '18 at 13:12











          • I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

            – Szymon Stepniak
            Nov 15 '18 at 13:20








          1




          1





          Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

          – billjamesdev
          Nov 14 '18 at 23:42







          Not a fan of using invokeMethod for this. Why not just use test.setProperty( it.key, 'new value' )?

          – billjamesdev
          Nov 14 '18 at 23:42















          Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

          – Szymon Stepniak
          Nov 15 '18 at 5:35





          Thanks @billjamesdev for a good suggestion! setProperty indeed is a much simpler solution to this problem.

          – Szymon Stepniak
          Nov 15 '18 at 5:35













          why I can not set using set.Property()? intellij says the object of test does not have setProperty function

          – ratzip
          Nov 15 '18 at 13:12





          why I can not set using set.Property()? intellij says the object of test does not have setProperty function

          – ratzip
          Nov 15 '18 at 13:12













          I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

          – Szymon Stepniak
          Nov 15 '18 at 13:20





          I don't know why IntelliJ complains about it. I run the code in my IntelliJ IDEA and there was no issue. Groovy console also does not see any problem (even with static compilation enabled) - groovyconsole.appspot.com/script/5147408312827904 You may have some kind of typo or something.

          – Szymon Stepniak
          Nov 15 '18 at 13:20




















          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%2f53301485%2flooping-through-class-members-and-update-member-value-in-groovy%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python