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

Reflective method invocation does not detect interface method when interface is declared in a subclass (e.g. HashMap.HashIterator.hasNext) #27995

Closed
iay opened this issue Feb 2, 2022 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@iay
Copy link

iay commented Feb 2, 2022

Affects: 5.3.14, 6.0.0-M2

If you use spring expression to access a public interface method, it instead accesses the method on the implementation class. If that class is not public, the access will fail.

Example: the standard Java collections have Iterator implementations which are private inner classes:

package test;

import java.util.HashMap;

import org.springframework.expression.spel.standard.SpelExpressionParser;

public class SpelTest {

	public static void main(String[] args) {

		var map = new HashMap<String, String>();
		map.put("key", "value");
		var iter = map.entrySet().iterator();
		final Object output = new SpelExpressionParser().parseExpression("hasNext()").getValue(iter);
		System.out.println("output is " + output);

	}

}

Under Java 11 and Spring Framework 5.3.14, the above will succeed with a warning:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.util.ReflectionUtils (file:/Users/iay/.m2/repository/org/springframework/spring-core/5.3.14/spring-core-5.3.14.jar) to method java.util.HashMap$HashIterator.hasNext()
WARNING: Please consider reporting this to the maintainers of org.springframework.util.ReflectionUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
output is true

Under Java 17 and either Spring Framework 5.3.14 or 6.0.0-M2, the above will fail:

Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1029E: A problem occurred when trying to execute method 'hasNext' on object of type 'java.util.HashMap$EntryIterator': 'Problem invoking method: public final boolean java.util.HashMap$HashIterator.hasNext()'

It's worth noting that the equivalent problem for property accesses was described in issue #22242 and fixed in commit dec6d69 for 5.1.18 back in 2019. This issue relates to methods instead but it seems like the same approach should be possible.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 2, 2022
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Feb 2, 2022
@jhoeller jhoeller added this to the Triage Queue milestone Feb 2, 2022
@jhoeller
Copy link
Contributor

jhoeller commented Feb 2, 2022

ReflectiveMethodExecutor is trying to get the interface method if possible as well, just like ReflectivePropertyAccessor. I'm not quite sure why this isn't kicking in here, we'll have a look.

@scantor
Copy link

scantor commented Feb 2, 2022

From my earlier JDK spelunking, I would speculate that it's because the base class they use for the LinkedHashSet iterators doesn't in fact implement Iterator (or anything else). The subclasses do, but the hasNext method itself is declared on the Abstract base class that doesn't, so it's probably not spotting the interface. It's a bit of an odd layout.

@jhoeller jhoeller changed the title Reflective access to methods should be through interfaces where necessary, as is already the case for properties Reflective method invocation does not detect interface method when interface is declared in a subclass (e.g. HashMap.HashIterator.hasNext) Feb 3, 2022
@jhoeller jhoeller self-assigned this Feb 3, 2022
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 3, 2022
@jhoeller jhoeller modified the milestones: Triage Queue, 5.3.16 Feb 3, 2022
@jhoeller
Copy link
Contributor

jhoeller commented Feb 3, 2022

Some debugging reveals that it is indeed the interface declaration in a subclass, late-binding a method from the superclass to an interface method, which is not being discovered by our getInterfaceMethodIfPossible algorithm here. I'll revisit this to pass along the original target class, not just operating on the method's declaring class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants