Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OgnlOps#isEqual comparison of two enum thrown IllegalArgumentException #15

Closed
vacoor opened this issue Feb 22, 2016 · 2 comments
Closed

Comments

@vacoor
Copy link

vacoor commented Feb 22, 2016

OgnlOps#isEqual comparison of two enum thrown IllegalArgumentException

Only in this case:
enum Op {
NE("<>"), // getClass() is "Op$1"
EQ("=") // getClass() is "Op$2"
}
Op.NE.getClass(). isAssignableFrom(Op.EQ.getClass()) // false

Now:

OgnlOps#isEqual(Object, Object)    // line 124 
is
result = (object1 != null) && (object2 != null)
                    && (object1.equals(object2) || (compareWithConversion(object1, object2) == 0));

OgnlOps#compareWithConversion(Object, Object)   // line 87 ~ 95
if ((t1 == NONNUMERIC) && (t2 == NONNUMERIC)) {
                // **FIXME: when enum is abstract**
                if ((v1 instanceof Comparable) && v1.getClass().isAssignableFrom(v2.getClass())) {
                    result = ((Comparable) v1).compareTo(v2);
                    break;
                } else {
                    throw new IllegalArgumentException("invalid comparison: " + v1.getClass().getName() + " and "
                            + v2.getClass().getName());
                }
            }

Temporary solution
"Op.NE.name() == 'NE'"

Tip:

before version is ok;
OgnlOps#isEqual(Object, Object) 
    result = compareWithConversion(object1, object2, true) == 0 || object1.equals(object2);
 OgnlOps#compareWithConversion(Object, Object, boolean)
   if(!equals) {   // **HERE because equals args is true **
         throw new IllegalArgumentException("invalid comparison: " + v1.getClass().getName() + " and " + v2.getClass().getName());
   }
@peteruhnak
Copy link
Contributor

peteruhnak commented Mar 23, 2020

FIY this was fixed by #78 / #84 or #99.

public class EnumTest {

  enum Op {
    NE("<>") {}, // getClass() is "Op$1"
    EQ("=") {} // getClass() is "Op$2"
    ;

    Op(String s) {
    }
  }

  @Test
  public void testEnumCompare() throws OgnlException {
    assertThat(Op.NE.getClass().isAssignableFrom(Op.EQ.getClass()), is(false));
    assertThat(OgnlOps.isEqual(Op.NE, Op.EQ), is(false));
  }

}

3.1.10

java.lang.IllegalArgumentException: invalid comparison: EnumTest$Op$1 and EnumTest$Op$2

3.1.28

test green

@lukaszlenart
Copy link
Collaborator

Thanks @peteruhnak :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants