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

OGNL improve getter/setter detection algorithm: #75

Conversation

JCgH4164838Gh792C124B5
Copy link
Contributor

OGNL improve getter/setter detection algorithm:

  • Add detail to isDefaultMethod() method comment.
  • Add a new isNonDefaultPublicInterfaceMethod() method.
  • Modify collectAccessors() method to consider public interface methods in addition to default methods.
  • Modify _getGetMethod() to prefer the first valid public getter over other getters, then the first valid public non-Default interface getter and lastly the first getter of any kind.
  • Modify _getSetMethod() to prefer the first valid public setter over other setters, then the first valid public non-Default interface setter and lastly the first setter of any kind.

- Add detail to isDefaultMethod() method comment.
- Add a new isNonDefaultPublicInterfaceMethod() method.
- Modify collectAccessors() method to consider public interface methods
  in addition to default methods.
- Modify _getGetMethod() to prefer the first valid public getter over other
  getters, then the first valid public non-Default interface getter and
  lastly the first getter of any kind.
- Modify _getSetMethod() to prefer the first valid public setter over other
  setters, then the first valid public non-Default interface setter and
  lastly the first setter of any kind.
@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello OGNL Team.

This PR attempts to address a behaviour that @yasserzamani identified when reviewing PR#74 and illegal reflective access warnings running Struts 2.5.x under JDK 11.

Currently OGNL 3.1.x picks the first getter/setter that it can find, even if the method is non-public or the declaring class is non-public. The updated logic in this PR attempts to find the first publicly accessible getter/setter method (both method and declaring class public) if possible, considering methods from the class and interfaces implemented by the class.

After a few failed attempts, this implementation seems to work. It passes the regression tests for OGNL as well as the regression build for Struts 2.5.21-SNAPSHOT when used as part of the build.

Running the 2.5.21. Showcase application under JDK 11 with this PR's implementation also seems to avoid the illegal reflective access warnings on the console too. 😄

Since the logic for choosing the getter/setter has changed, this change presents some risk - since it could choose differently than previous OGNL 3.1.x versions. However, choosing a publicly accessible getter/setter seems like better practice if possible to do so.

Please advise what you think about the proposed change, and whether the logic seems sound or not.

@yasserzamani
Copy link
Contributor

osm 😮 LGTM 👍

@yasserzamani
Copy link
Contributor

And consequently it seems #74 isn't essential anymore (?)

@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Thanks for taking a look. 😄
I think the changes for #74 are still desirable for OGNL 3.1.x in general (to avoid breaking existing application usage and keeping options open for JDK11).
At least this PR should help "head in the direction" of minimizing the need for the accessibility modification feature, at least for getter/setter methods.

…calOGNL_3_1_GetSetFixEnhance_1

Update local branch to the latest version to deal with any conflicts and
then update the @SInCE tags to a new level.
@JCgH4164838Gh792C124B5
Copy link
Contributor Author

This PR will be updated (hopefully soon) after merging with 3.1.24, making any necessary changes and confirming functionality.

- Updated @SInCE and related comments to refer to 3.1.25 (post-merge of
3.1.24 changes).
- Minor optimization in _getGetMethod() and _getSetMethod() to stop search
once best possible match found. Previously was finding all 3 types of
matches before stopping the search.
@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello.

I'm wondering if this PR also needs a control flag to decide whether to use the old or new lookup logic.. Although the new logic should choose a "better" match it's possible that existing applications could encounter unexpected behaviour as a result (due to a different getter or setter chosen with new vs. old logic).

An environment flag (or static library method) allowing users to control whether to use the new or old lookup logic would limit that particular risk.

The PR can be adjusted to add such a control mechanism (or any other changes that people think should be made).

- Provide a Java option (environment) flag allowing users to revert to the
original "return first match" getter/setter logic if needed.  By default
the new "best match" getter/setter logic will be used.
// Unavailable (SecurityException, etc.)
}
if (optionDefinedInEnvironment) {
System.out.println("System property " + OgnlRuntime.USE_FIRSTMATCH_GETSET_LOOKUP + " value: " + flagValueFromEnvironment);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it would be better to use logging instead of sysout even for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi.
Given the nature of this library, having tests output to sysout seems sufficient for now (AFAIK it's the only mechanism that has been available up until now, and the results are clearly visible in the Maven test run output).
Maybe someone will introduce a testing-only logging replacement for the sysouts in the tests in a future PR. 😄

@lukaszlenart
Copy link
Collaborator

@JCgH4164838Gh792C124B5 conflicts :(

…calOGNL_3_1_GetSetFixEnhance_1

# Conflicts:
#	src/java/ognl/OgnlRuntime.java
#	src/test/java/ognl/OgnlRuntimeTest.java

- Manually resolved merge conflicts for OgnlRuntime.java and OgnlRuntimeTest.java.
@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Fixed the merge conflicts but now Travis CI complains about usage of source level 1.5. 😕
Will change source/target to 1.6 (same as Yasser did for #79) and see if that makes Travis CI happy.

@JCgH4164838Gh792C124B5
Copy link
Contributor Author

Hello @lukaszlenart .

Manual resolution of merge conflicts appears to have worked. 😄
A follow-up commit to use source/target 1.6 was required to get Travis CI checks to complete (could be set back to 1.5 if needed, though - compiles fine locally with JDK 7).

@lukaszlenart
Copy link
Collaborator

LGTM 👍

@lukaszlenart lukaszlenart merged commit c2a6cb5 into orphan-oss:ognl-3-1-x Sep 12, 2019
@JCgH4164838Gh792C124B5 JCgH4164838Gh792C124B5 deleted the localOGNL_3_1_GetSetFixEnhance_1 branch September 28, 2019 16:33
JCgH4164838Gh792C124B5 added a commit to JCgH4164838Gh792C124B5/ognl that referenced this pull request Oct 5, 2019
Resolved merge conflicts manually (only pom.xml, decided to include
modified comment from 3.1.x).
----------
Merge pull request orphan-oss#75 from JCgH4164838Gh792C124B5/localOGNL_3_1_GetSetFixEnhance_1

OGNL improve getter/setter detection algorithm:

(cherry picked from commit c2a6cb5)

# Conflicts:
#	pom.xml
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

Successfully merging this pull request may close these issues.

4 participants