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

SPEL and Enum variable resolution using wrong cachedExecutor [SPR-10452] #15085

Closed
spring-issuemaster opened this issue Apr 9, 2013 · 4 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Apr 9, 2013

Martin Frey opened SPR-10452 and commented

The bug is appearing if the Enum used is bound in a variable and the method "values()" is called. I'm currently using Thymeleaf as the templateEngine and I'm not fully certain that this is related. Probably it's possible to reproduce in a plain Java test but i had no time yet.

The screenshot shows the issue.

  • MethodReference$MethodValueRef's target is the enum RiskLevel
  • The executorToUse is resolved to the Method ActionType.values()

The second screenshot shows the thymeleaf code for the iteration.


Affects: 3.2.2

Attachments:

Issue Links:

  • #15285 Cached MethodExecutor invokes wrong method
  • #14129 MethodResolver results are cached in SpEL expressions, so changing the context at evaluation time has no effect

Referenced from: commits 5e6044c, b9d726f

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 9, 2013

Martin Frey commented

Found a way to reproduce it with plain java. (I'm not sure if we are allowed to use EL like this though)

SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);

StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");

context.setVariable("enumType", RiskLevel.class);
Object result = spel.getValue(context);

context.setVariable("enumType", ActionType.class);
result = spel.getValue(context);

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 9, 2013

Martin Frey commented

Funny enough the method "getSimpleName()" is resolved properly:

SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);

StandardEvaluationContext context = new StandardEvaluationContext();
Expression spelName = parser.parseExpression("#enumType.getSimpleName()");
Expression spelValues = parser.parseExpression("#enumType.values()");

context.setVariable("enumType", RiskLevel.class);
Object name = spelName.getValue(context);
Object values = spelValues.getValue(context);
System.out.println(name + ": " + values);

context.setVariable("enumType", ActionType.class);
name = spelName.getValue(context);
values = spelValues.getValue(context);
System.out.println(name + ": " + values);

--------------------- Results in -----------------
RiskLevel: [Lcom.RiskLevel;@f61730
ActionType: [Lcom.RiskLevel;@6c623e

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 9, 2013

Martin Frey commented

Workaround:

@Component
public class EnumHelper {
public Enum<?>[] values(Class<Enum<?>> enumClass) {
return enumClass.getEnumConstants();
}
}


Looks like it has to do with the "values()" method beeing a static method. Once the expression has a cachedExecutor for "values()" this one is always referencing the first enum.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Sep 4, 2013

Juergen Hoeller commented

Indeed, this turns out to be a general problem with EL resolution against static methods. Fixed for 4.0 RC1 and 3.2.5 now.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.