I have an interface as follows:
public interface Condition extends OtherConditionInterface {}
And multiple subclasses that implement it:
-One that can take multiple sub-conditions in the constructor
public class SubCondition1 implements Condition {
private final List<Condition> conditions;
public Subcondition1(final Condition... condition) {
this.conditions = Arrays.asList(condition);
}
}
-And some that take only a String:
public class SubCondition2 implements Condition {
private String cond;
public Subcondition2(final String cond) {
this.cond = cond;
}
}
Now, I have another class whose constructor looks something like this:
public class Bar {
private final String name;
private final Condition condition;
public Bar(String name, Condition condition) {
this.name = name;
this.condition = condition;
}
}
Presumably, when I want to create a new Bar object, I'd go about like so:
new Bar("sampleTitle", new Subcondition1(new Subcondition2("done"), new Subcondition1(new Subcondition2("foo"), new Subcondition2("bar")));
But the problem I am facing is that I have the second argument that I need to pass to Bar (the condition) stored as a String, like so:
String str = "new Subcondition1(new Subcondition2("done"), new Subcondition1(new Subcondition2("foo"), new Subcondition2("bar"))";
My question is if there is any way I could pass the string as the second argument when trying to create a new Bar, considering it is asking for a Condition.
I know this is far from ideal. Far from common sense even, but the fact that the data is stored in the String cannot be changed now.
Edit: Using ScriptEngine to parse the String into an Object and then cast it to (Condition) isn't viable. The reason is that the execution of the script will always throw an error of the form: "Condition" is not defined, because they are not part of JavaScript. Also, importing new libraries isn't possible as I am not allowed to do so.