fix: prioritize getters over methods in property access #532
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Prioritize getters over methods in property access
Problem
When accessing properties on Java objects exposed to JavaScript, getters were not being prioritized over methods with the same name. This caused property access like
obj.nameto return a function reference instead of the value fromgetName().Example:
Root Cause
In
BaseJavetReflectionProxyHandler.getByMethod(), the method resolution order checkedmethodsMapbeforegettersMap. This meant that when both a getter (e.g.,getName()) and a method (e.g.,name()) existed, the method took precedence, causing property access to return a function instead of invoking the getter.Solution
Swapped the resolution order in
BaseJavetReflectionProxyHandler.getByMethod()to checkgettersMapfirst, thenmethodsMap. This ensures that:obj.name) invokes getters and returns valuesobj.getName()) still work correctlyChanges
src/main/java/com/caoccao/javet/interop/proxy/BaseJavetReflectionProxyHandler.javagetByMethod()(lines ~274-299)gettersMapbeforemethodsMapwhen resolving property accessTesting
Added test case
testGetterPriorityOverMethod()inTestJavetProxyConverter.javathat verifies:name,displayName)Impact
player.namenow works correctlyRelated
This fix addresses a common issue when bridging Java objects to JavaScript, particularly affecting frameworks that rely on JavaBean conventions (e.g., Bukkit/PaperMC plugins).