Skip to content

Commit

Permalink
Allow customization of SpEL method resolution
Browse files Browse the repository at this point in the history
This change introduces a protected ReflectiveMethodResolver#getMethods,
allowing subclasses to specify additional static methods not
declared directly on the type being evaluated. These methods then become
candidates for filtering by any registered MethodFilters and ultimately
become available within for use within SpEL expressions.

Issue: SPR-9038
  • Loading branch information
aclement authored and cbeams committed Feb 1, 2012
1 parent c7fd03b commit 90bed97
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,7 +90,7 @@ public MethodExecutor resolve(EvaluationContext context, Object targetObject, St
try {
TypeConverter typeConverter = context.getTypeConverter();
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
Method[] methods = type.getMethods();
Method[] methods = getMethods(type);

// If a filter is registered for this type, call it
MethodFilter filter = (this.filters != null ? this.filters.get(type) : null);
Expand Down Expand Up @@ -197,4 +197,16 @@ public void registerMethodFilter(Class<?> type, MethodFilter filter) {
}
}

/**
* Return the set of methods for this type. The default implementation returns the
* result of Class#getMethods for the given {@code type}, but subclasses may override
* in order to alter the results, e.g. specifying static methods declared elsewhere.
*
* @param type the class for which to return the methods
* @since 3.1.1
*/
protected Method[] getMethods(Class<?> type) {
return type.getMethods();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.springframework.expression.spel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -38,6 +40,7 @@
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.ParserContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
Expand Down Expand Up @@ -1137,6 +1140,41 @@ public ContextObject() {
public Map<String, String> getFourthContext() {return fourthContext;}
}

/**
* Test the ability to subclass the ReflectiveMethodResolver and change how it
* determines the set of methods for a type.
*/
@Test
public void testCustomStaticFunctions_SPR9038() {
try {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>();
methodResolvers.add(new ReflectiveMethodResolver() {
@Override
protected Method[] getMethods(Class<?> type) {
try {
return new Method[] {
Integer.class.getDeclaredMethod("parseInt", new Class[] {
String.class, Integer.TYPE }) };
} catch (NoSuchMethodException e1) {
return new Method[0];
}
}
});

context.setMethodResolvers(methodResolvers);
org.springframework.expression.Expression expression =
parser.parseExpression("parseInt('-FF', 16)");

Integer result = expression.getValue(context, "", Integer.class);
assertEquals("Equal assertion failed: ", -255, result.intValue());
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: "+e.toString());
}
}

}


0 comments on commit 90bed97

Please sign in to comment.