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

"hashCode()" being called when accessing a "code" field #32

Closed
danielfernandez opened this issue Oct 20, 2016 · 3 comments
Closed

"hashCode()" being called when accessing a "code" field #32

danielfernandez opened this issue Oct 20, 2016 · 3 comments

Comments

@danielfernandez
Copy link
Contributor

If we have a public code field in a class, such as:

public class Something {
   public String code = "abc123";
}

And, being s an object of type Something, we execute this expression:

s.code

Instead of returning "abc123", the hash code will be returned because the hashCode() method will be selected for execution.

The responsible for this is this block of code in OgnlRuntime: https://github.com/jkuhnert/ognl/blob/751084b4802b488e0386ab702e230cceca296254/src/java/ognl/OgnlRuntime.java#L2947-L2954

if (methods[i].getName().toLowerCase().endsWith(name)
    && !methods[i].getName().startsWith("set")
    && methods[i].getMethod().getReturnType() != Void.TYPE) {

    Method m = methods[i].getMethod();
    if (!candidates.contains(m))
        candidates.add(m);
}

IMHO the check applied here is far too lenient, especially given the fact that field names have not been checked yet by the time this executes…

@danielfernandez
Copy link
Contributor Author

This is the cause of thymeleaf/thymeleaf#546

@lukaszlenart
Copy link
Collaborator

Yeah... but a public field isn't something normal in Java ecosystem...

@danielfernandez
Copy link
Contributor Author

Fixed. All tests pass. Thanks!

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

2 participants