-3

So I have a string that is made of three parts: fieldName, operator and value

Assuming the parameters have these values:

fieldName: firstName
operator: equals
value: Nikola

I want to be able to evaluate that, firstName equals Nikola as a condition with the help of Java reflections. Is this possible? if(obj.getFirstName().equals(value))

Edit (Xenteros):
According to the discussion in the comments, I decided to add the explanation to the question as the MCVE should be in the question itself:

Problem description:
There is a class which has multiple fields, all of them contain Strings. There is a request for a solution to get the field value by field's name and compare to another String.
There are multiple operators available. For example: <, <=, >= etc, but there is also equals available which was unfortunately added as an example before. If the user passes "equals" to the method, equals should be invoked.

Example input:
User u, such that u.firstName.equals("John"))
"firstName",
"John",
"<=".

Expected output:

true

xenteros
  • 15,586
  • 12
  • 56
  • 91
Kei
  • 315
  • 1
  • 4
  • 18

1 Answers1

1

You're asking about reflection:

Let's assume that you have an object called obj.

Field field = obj.getClass().getField(fieldName);
Method method = field.getType().getMethod(operator, Object.class); //in case you mean method

if (method.invoke(field.get(), value) {}

Remember to surround it with try-catch as there are many possibilities of Exceptions to be thrown.

According to your update to the question I suggest the following:

boolean check(Object obj, String fieldName, String operator, String value) throws IllegalArgumentException, someOtherExceptions {
    Field field = obj.getClass().getField(fieldName);
    if (operator.equals("equals") {
        return field.get().equals(value);
    }
    if (operator.equals("<=")) {
        return field.get().compareTo(value) <=0;
    }
    if (operator.equals("<")) {
        return field.get().compareTo(value) < 0;
    //other operators impl.
    //at the end:
    throw new IllegalArgumentException("Unknown operator");
}

Instead of Object obj you should put the class which contains all the fields the 'fieldName' might refer to.

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • I guess that will have to do. Thanks for your help. – Kei Sep 30 '16 at 05:35
  • That's the way. Feel free to upvote and mark as a solution. I'll edit your question to add what you've explained in the comments. – xenteros Sep 30 '16 at 05:36
  • @Kei please have a look at the question edit and tell me if it's what you're looking for. – xenteros Sep 30 '16 at 05:43
  • Yes! Sums up what my purpose was. – Kei Sep 30 '16 at 05:49
  • I believe, my answer is the only applicable solution then. Nevermind, I recommend reading [ask] and [mcve]. These are two tutorials on how to ask a question and not get downvoted. On the other hand this will most often implicate in receiving an answer. – xenteros Sep 30 '16 at 05:52
  • @Kei I've browsed your questions. You have asked many, but haven't accepted any answer as a solution which is important for the site. In each question, if there is an answer that solved your problem, you should mark it as a solution by clicking on a gray tick below the answer score. This can be changed at any time if a better answer appear. Both of use will be awarded with some reputation then – xenteros Sep 30 '16 at 06:21