Spring SpEL selects the wrong method to call

I am trying to evaluate the following SpEL expression (Spring -expression version 3.1.1):

T(com.google.common.collect.Lists).newArrayList(#iterable)

where #iterable is of type java.lang.Iterable. Google Guava com.google.common.collect.Lists (version 14.0) has a newArrayList (Iterable) method, but for some reason SpEL selects a call to another method: newArrayList (Object [])

I plunged into the code and found that the problem was with the implementation of org.springframework.expression.spel.support.ReflectiveMethodResolver : it seems to be sensitive to how methods are sorted by java.lang.Class :: GetMethods. If 2 methods correspond to the call (in the case when one of the methods is varargs), a later method will be called (in order) instead of choosing a method that is not varargs (more specifically). The JDK does not seem to guarantee the sort order of the methods: different traces show a different order.

Is there any way to overcome this problem?

+4
source share
1 answer

Spring EL, :

"#iterable.?[true]"

:

Iterable<Integer> it = () -> new Iterator<Integer>() {

  private int[] a = new int[]{1, 2, 3};
  private int index = 0;

  @Override
  public boolean hasNext() {
    return index < a.length;
  }

  @Override
  public Integer next() {
    return a[index++];
  }
};
Tmp tmp = new Tmp();
tmp.setO(it);
StandardEvaluationContext context = new StandardEvaluationContext(tmp);

ArrayList<Integer> list = parser.parseExpression("o.?[true]").getValue(context,
    ArrayList.class);
0

All Articles