Reputation: 27
Resize Class
class Resize {
int width = 12
int length = 10
}
I have this method to print data from Resize class
public String print(){
Resize resize = new Resize()
println(resize)
}
How to dynamically print values like this? I mean what if I don't know the key names.
[12, 10]
Upvotes: 1
Views: 758
Reputation: 3932
You can annotate the class with ToString
e.g.
import groovy.transform.ToString
@ToString(includeNames=true)
class Resize { ...
If the output from ToString isn't suitable you could try:
class Resize {
int width = 12
int length = 10
String toString() {
properties.findAll{it.key != 'class'}.collect{ it.value }
}
}
Upvotes: 1
Reputation: 10665
You can write a simple util function like this:
class Resize {
int width = 12;
int length = 10;
String getDimensionsAsString(){
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(width);
sb.append(",");
sb.append(length);
sb.append("]");
return sb.toString();
}
}
println new Resize().getDimensionsAsString();
Output:
[12,10]
Upvotes: 0