Skip to content

ArrayIndexOutOfBoundsException in MethodBasedEvaluationContext [SPR-14554] #19122

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

Closed
spring-projects-issues opened this issue Aug 2, 2016 · 4 comments
Assignees
Labels
status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 2, 2016

Matthias Rühl opened SPR-14554 and commented

java.lang.ArrayIndexOutOfBoundsException: 2
at org.springframework.context.expression.MethodBasedEvaluationContext.lazyLoadArguments(MethodBasedEvaluationContext.java:94)
at org.springframework.context.expression.MethodBasedEvaluationContext.lookupVariable(MethodBasedEvaluationContext.java:68)
at org.springframework.cache.interceptor.CacheEvaluationContext.lookupVariable(CacheEvaluationContext.java:74)
at org.springframework.expression.spel.ExpressionState.lookupVariable(ExpressionState.java:144)
at org.springframework.expression.spel.ast.VariableReference.getValueInternal(VariableReference.java:75)
at org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:154)
at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:84)
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
at org.springframework.cache.interceptor.CacheOperationExpressionEvaluator.key(CacheOperationExpressionEvaluator.java:117)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:738)
at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:554)
at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:498)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:389)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)

This is my cacheable annotation:

@Cacheable(value = "dummyCache", key = "'getSomethingList_'.concat(#var1).concat(T(java.util.Arrays).deepHashCode(#var2))")
List<Something> getSomethingList(String var1, Integer... var2);

Affects: 4.2.7, 4.3.2

Issue Links:

Referenced from: commits 4543a28, 48980a2, fae503d

Backported to: 4.2.8

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

Is that exception thrown for every call or with a specific set of arguments? If so provide them, a sample would be better to reduce the amount of time to spend to reproduce the issue.

Also, I would strongly recommend you to use a KeyGenerator for this via the keyGenerator argument on the annotation. This bug isn't critical, you have an easy way out that is better IMO than your current code.

@spring-projects-issues
Copy link
Collaborator Author

Matthias Rühl commented

With the following arguments the error occurs:
var1 = "A"
var2 = [10,20,30]

Sounds good, I will take a look at the KeyGenerator feature :)

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Aug 8, 2016

Juergen Hoeller commented

It seems that this is a side effect of the change made for #19022 in 4.3.2: While args may be shorter than parameterNames in an empty-vararg scenario there, it may also be longer in a multi-vararg scenario... So this seems like a regression in 4.3.2, fixing an index-out-of-bounds in one scenario at the expense of causing it in the other. In order to address both scenarios, some more sophisticated treatment will be necessary: possibly loop until parameterNames is at its last element, then explicitly specify null if there is no args entry anymore, pass in the args value as-is if there is exactly one element left, or extract the remaining args entries into a dedicated array and specify that as variable value.

Let's also backport the revised algorithm to 4.2.8, addressing both scenarios there as well.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Since I've been researching our general varargs handling in SpEL a bit, I guess I should take this issue right away...

The existing MethodBasedEvaluationContextTests do not expose varargs the SpEL way (i.e. in a flattened array), which is why they are not leading to the index-out-of-bounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants