0

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.

arch1ve
  • 183
  • 1
  • 12
  • are you asking how to make a string with quotes in it? You can escape the quotes with `\ `: Sample: `String str = "new Subcondition1(new Subcondition2(\"done\")";` – Krease Dec 09 '16 at 02:26
  • @Krease No. The Bar constructor is asking for a Condition as the second argument, but I only have a String that stores what would need to be inputted as that argument. – arch1ve Dec 09 '16 at 02:29
  • You want to convert String values to keywords in java ? – msagala25 Dec 09 '16 at 02:29
  • @shmosel I edited my question to show why it isn't a duplicate of that (after testing). – arch1ve Dec 09 '16 at 03:29
  • Just because one of the answers didn't solve your problem, doesn't mean the question is not a duplicate. – shmosel Dec 09 '16 at 03:30
  • @choasia I am not entirely sure how it would help me. All Subcondition classes have totally different constructors. Either I don't understant that other question fully, or it is not the answer. – arch1ve Dec 09 '16 at 03:31
  • @shmosel I am not ignorant and I looked at the other answers as well. None of them are sensible for my problem which is more complex than evaluating a mathematical equation. If you have a valid point for being so sure that my question is a duplicate and it is already answered there, then please give me a hint. – arch1ve Dec 09 '16 at 03:36
  • @arch1ve Sorry, I think I misunderstood your question. – choasia Dec 09 '16 at 03:54
  • @arch1ve Actually, the 2nd (http://stackoverflow.com/a/2605050/616460) and 3rd (http://stackoverflow.com/a/27396372/616460) answer are entirely applicable and accurate for your problem. Either "Wrap the expression in the Java source code for a class with an eval method, send that to the Java compiler, and then load the resulting compiled class", make your own little parser and use reflection, or find another way to approach this. You might be able to convince XStream or something to do something like it from XML, with some added clunkiness, but, this isn't really a thing you can readily do. – Jason C Dec 09 '16 at 03:58

0 Answers0