Fixed StandardTypeLocator to only know about java.lang.* out of the box. Doc fixes related to that.
This commit is contained in:
@@ -65,7 +65,6 @@ public class StandardTypeComparator implements TypeComparator {
|
||||
return ((Comparable) left).compareTo(right);
|
||||
}
|
||||
|
||||
// How do we get to this line...?
|
||||
throw new SpelException(SpelMessages.NOT_COMPARABLE, left.getClass(), right.getClass());
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +47,8 @@ public class StandardTypeLocator implements TypeLocator {
|
||||
|
||||
public StandardTypeLocator(ClassLoader loader) {
|
||||
this.loader = loader;
|
||||
// Similar to when writing Java, it only knows about java.lang by default
|
||||
registerImport("java.lang");
|
||||
registerImport("java.util");
|
||||
}
|
||||
|
||||
|
||||
@@ -95,5 +95,9 @@ public class StandardTypeLocator implements TypeLocator {
|
||||
public List<String> getImportPrefixes() {
|
||||
return Collections.unmodifiableList(this.knownPackagePrefixes);
|
||||
}
|
||||
|
||||
public void removeImport(String prefix) {
|
||||
this.knownPackagePrefixes.remove(prefix);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,8 +17,10 @@
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.EvaluationException;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.expression.spel.support.StandardTypeLocator;
|
||||
|
||||
/**
|
||||
* Tests the evaluation of real expressions in a real context.
|
||||
@@ -259,4 +261,20 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||
assertTrue(trueValue);
|
||||
}
|
||||
|
||||
public void testResolvingList() throws Exception {
|
||||
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
|
||||
try {
|
||||
assertFalse(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class));
|
||||
fail("should have failed to find List");
|
||||
} catch (EvaluationException ee) {
|
||||
// success - List not found
|
||||
}
|
||||
((StandardTypeLocator)context.getTypeLocator()).registerImport("java.util");
|
||||
assertTrue(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class));
|
||||
}
|
||||
|
||||
public void testResolvingString() throws Exception {
|
||||
Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);
|
||||
assertEquals(String.class,stringClass);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,18 +29,19 @@ import org.springframework.expression.spel.support.StandardTypeLocator;
|
||||
*/
|
||||
public class StandardTypeLocatorTests extends TestCase {
|
||||
|
||||
public void testPrimitives() throws EvaluationException {
|
||||
public void testImports() throws EvaluationException {
|
||||
StandardTypeLocator locator = new StandardTypeLocator();
|
||||
assertEquals(Integer.class,locator.findType("java.lang.Integer"));
|
||||
assertEquals(String.class,locator.findType("java.lang.String"));
|
||||
|
||||
List<String> prefixes = locator.getImportPrefixes();
|
||||
assertEquals(2,prefixes.size());
|
||||
assertEquals(1,prefixes.size());
|
||||
assertTrue(prefixes.contains("java.lang"));
|
||||
assertTrue(prefixes.contains("java.util"));
|
||||
assertFalse(prefixes.contains("java.util"));
|
||||
|
||||
assertEquals(Boolean.class,locator.findType("Boolean"));
|
||||
assertEquals(java.util.List.class,locator.findType("List"));
|
||||
// currently does not know about java.util by default
|
||||
// assertEquals(java.util.List.class,locator.findType("List"));
|
||||
|
||||
try {
|
||||
locator.findType("URL");
|
||||
@@ -51,5 +52,7 @@ public class StandardTypeLocatorTests extends TestCase {
|
||||
}
|
||||
locator.registerImport("java.net");
|
||||
assertEquals(java.net.URL.class,locator.findType("URL"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user