Fixed StandardTypeLocator to only know about java.lang.* out of the box. Doc fixes related to that.
This commit is contained in:
parent
f36b9eb088
commit
38c7f566e5
|
|
@ -65,7 +65,6 @@ public class StandardTypeComparator implements TypeComparator {
|
||||||
return ((Comparable) left).compareTo(right);
|
return ((Comparable) left).compareTo(right);
|
||||||
}
|
}
|
||||||
|
|
||||||
// How do we get to this line...?
|
|
||||||
throw new SpelException(SpelMessages.NOT_COMPARABLE, left.getClass(), right.getClass());
|
throw new SpelException(SpelMessages.NOT_COMPARABLE, left.getClass(), right.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,8 +47,8 @@ public class StandardTypeLocator implements TypeLocator {
|
||||||
|
|
||||||
public StandardTypeLocator(ClassLoader loader) {
|
public StandardTypeLocator(ClassLoader loader) {
|
||||||
this.loader = loader;
|
this.loader = loader;
|
||||||
|
// Similar to when writing Java, it only knows about java.lang by default
|
||||||
registerImport("java.lang");
|
registerImport("java.lang");
|
||||||
registerImport("java.util");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -96,4 +96,8 @@ public class StandardTypeLocator implements TypeLocator {
|
||||||
return Collections.unmodifiableList(this.knownPackagePrefixes);
|
return Collections.unmodifiableList(this.knownPackagePrefixes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeImport(String prefix) {
|
||||||
|
this.knownPackagePrefixes.remove(prefix);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,10 @@
|
||||||
package org.springframework.expression.spel;
|
package org.springframework.expression.spel;
|
||||||
|
|
||||||
import org.springframework.expression.EvaluationContext;
|
import org.springframework.expression.EvaluationContext;
|
||||||
|
import org.springframework.expression.EvaluationException;
|
||||||
import org.springframework.expression.Expression;
|
import org.springframework.expression.Expression;
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
|
import org.springframework.expression.spel.support.StandardTypeLocator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the evaluation of real expressions in a real context.
|
* Tests the evaluation of real expressions in a real context.
|
||||||
|
|
@ -259,4 +261,20 @@ public class EvaluationTests extends ExpressionTestCase {
|
||||||
assertTrue(trueValue);
|
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 class StandardTypeLocatorTests extends TestCase {
|
||||||
|
|
||||||
public void testPrimitives() throws EvaluationException {
|
public void testImports() throws EvaluationException {
|
||||||
StandardTypeLocator locator = new StandardTypeLocator();
|
StandardTypeLocator locator = new StandardTypeLocator();
|
||||||
assertEquals(Integer.class,locator.findType("java.lang.Integer"));
|
assertEquals(Integer.class,locator.findType("java.lang.Integer"));
|
||||||
assertEquals(String.class,locator.findType("java.lang.String"));
|
assertEquals(String.class,locator.findType("java.lang.String"));
|
||||||
|
|
||||||
List<String> prefixes = locator.getImportPrefixes();
|
List<String> prefixes = locator.getImportPrefixes();
|
||||||
assertEquals(2,prefixes.size());
|
assertEquals(1,prefixes.size());
|
||||||
assertTrue(prefixes.contains("java.lang"));
|
assertTrue(prefixes.contains("java.lang"));
|
||||||
assertTrue(prefixes.contains("java.util"));
|
assertFalse(prefixes.contains("java.util"));
|
||||||
|
|
||||||
assertEquals(Boolean.class,locator.findType("Boolean"));
|
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 {
|
try {
|
||||||
locator.findType("URL");
|
locator.findType("URL");
|
||||||
|
|
@ -51,5 +52,7 @@ public class StandardTypeLocatorTests extends TestCase {
|
||||||
}
|
}
|
||||||
locator.registerImport("java.net");
|
locator.registerImport("java.net");
|
||||||
assertEquals(java.net.URL.class,locator.findType("URL"));
|
assertEquals(java.net.URL.class,locator.findType("URL"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -499,8 +499,7 @@ boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(
|
||||||
|
|
||||||
<para>The relational operators; equal, not equal, less than, less than
|
<para>The relational operators; equal, not equal, less than, less than
|
||||||
or equal, greater than, and greater than or equal are supported using
|
or equal, greater than, and greater than or equal are supported using
|
||||||
standard operator notation. Support is not yet implemented for objects
|
standard operator notation. </para>
|
||||||
that implement the Comparable interface.</para>
|
|
||||||
|
|
||||||
<para><programlisting language="java">// evaluates to true
|
<para><programlisting language="java">// evaluates to true
|
||||||
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
|
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
|
<para>The special 'T' operator can be used to specify an instance of
|
||||||
java.lang.Class (the 'type'). Static methods are invoked using this
|
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);
|
<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 < T(java.math.RoundingMode).FLOOR").getValue(Boolean.class);
|
boolean trueValue = parser.parseExpression("T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR").getValue(Boolean.class);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue