Fixed StandardTypeLocator to only know about java.lang.* out of the box. Doc fixes related to that.

This commit is contained in:
Andy Clement 2009-04-10 03:20:09 +00:00
parent f36b9eb088
commit 38c7f566e5
5 changed files with 38 additions and 9 deletions

View File

@ -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());
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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"));
}
}

View File

@ -499,8 +499,7 @@ boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(
<para>The relational operators; equal, not equal, less than, less than
or equal, greater than, and greater than or equal are supported using
standard operator notation. Support is not yet implemented for objects
that implement the Comparable interface.</para>
standard operator notation. </para>
<para><programlisting language="java">// evaluates to true
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
@ -627,10 +626,16 @@ String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inve
<para>The special 'T' operator can be used to specify an instance of
java.lang.Class (the 'type'). Static methods are invoked using this
operator as well</para>
operator as well. The <classname>StandardEvaluationContext</classname>
uses a <classname>TypeLocator</classname> to find types and
the <classname>StandardTypeLocator</classname> (which can be replaced)
is built with an understanding of the java.lang package. This means T()
references to types within java.lang do not need to be fully qualified,
but all other type references must be.</para>
<programlisting language="java">Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);
Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);
boolean trueValue = parser.parseExpression("T(java.math.RoundingMode).CEILING &lt; T(java.math.RoundingMode).FLOOR").getValue(Boolean.class);
</programlisting>