Looping through class members and update member value in groovy
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
add a comment |
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
add a comment |
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
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
groovy
asked Nov 14 '18 at 13:34
ratzipratzip
4571722
4571722
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
1
Not a fan of using invokeMethod for this. Why not just usetest.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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
Not a fan of using invokeMethod for this. Why not just usetest.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
add a comment |
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
1
Not a fan of using invokeMethod for this. Why not just usetest.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
add a comment |
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
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
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 usetest.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
add a comment |
1
Not a fan of using invokeMethod for this. Why not just usetest.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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