I have some Groovy scripts which all have a ConfJava static attribute. A script can call another. So any script could be a parent or a child script.
If the script is called as a child script, I want them to use the last value in the config of the parent script. If it is called as the parent, I want them to use the value given by default in the static field. Here is two examples:
//procedure1.groovy
package procedureUtils.test
import groovy.transform.Field
import procedureUtils.config.Conf1
@Field static ConfJava config = new Conf1() // config.mode = "1"
exec()
static exec() {
println "Procedure1 " + config.getMode()
Procedure2.exec()
}
//procedure2.groovy
package procedureUtils.test
import groovy.transform.Field
import procedureUtils.config.Conf2
import procedureUtils.config.Conf3
import procedureUtils.config.ConfJava
@Field static ConfJava config = new Conf2() // config.mode = "2"
exec()
static exec() {
println "Procedure2 " + config.getMode()
config = new Conf3()
println "Procedure2 after " + config.getMode()
Procedure3.exec()
}
The important point is that ConfJava can be extended to create other custom Configuration classes based on ConfJava. Also, I can't pass the config in the exec() arguments (this is a constraint) nor change the static context in the groovy procedures files.
If I take the examples above, if Procedure2 is called by Procedure1, the output should be :
Procedure1 1
Procedure2 1
Procedure2 after 3
// and Conf3.mode value should propagate to Procedure3 etc. because it is the last state wanted by the developer in the exec() function
If I call directly Procedure2 as the parent, it should be:
Procedure2 2
Procedure2 after 3
// and Conf3.mode value should propagate to Procedure3 etc. because it is the last state wanted by the developer in the exec() function
The weird thing is, if I put @Field static ConfJava config = new ConfJava() in each files, except for one (lets say the second one) it correctly takes the parent script configuration (the ConfJava mode = "abs"). But if I change the ConfJava by Conf1, mode will equals "1" instead of the parent mode value.
Why does it only work correctly with the ConfJava class? What can I do to respect all the constraints and solve my problem?
P.S.: I tried the option 4 of this topic
Edit :
How to run
Here my configurations classes (ConfAbs and ConfDef are in Java, the others are groovy classes)
// ConfAbs.java
package procedureUtils.config;
public abstract class ConfAbs {
private static String mode = "abs";
public static String getMode() {
return mode;
}
public static void setMode(String newMode) {
mode = newMode;
}
}
// ConfDef.java
package procedureUtils.config;
public class ConfDef extends ConfAbs {
public ConfDef() {
setMode("Def");
}
}
All the (except the abstract) have the same implementation for now, only the number changes
// Conf1.groovy
package procedureUtils.config
class Conf1 extends ConfAbs {
Conf1() {
setMode("1")
}
}
// same for Conf2, etc.
Run the file Procedure1 above with IntelliJ or a groovy compiler (I do it with IntelliJ) , right click -> run Procedure1.groovy)

you tagged your question with
OOPwhen using static methods and fields is too far fromOOP.just pass your config as a parameter:
P1.groovy
P2.groovy
groovy P1command line will print:groovy P2will print:option 2 - declare config variable just once and use it from all scripts
Global.config
P1.groovy
P2.groovy