本文整理汇总了Java中org.springframework.expression.spel.standard.SpelExpressionParser.parseExpression方法的典型用法代码示例。如果您正苦于以下问题:Java SpelExpressionParser.parseExpression方法的具体用法?Java SpelExpressionParser.parseExpression怎么用?Java SpelExpressionParser.parseExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.expression.spel.standard.SpelExpressionParser
的用法示例。
在下文中一共展示了SpelExpressionParser.parseExpression方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testParsingSimpleTemplateExpression04
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void testParsingSimpleTemplateExpression04() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
Object o = expr.getValue();
assertEquals("hello world", o.toString());
expr = parser.parseExpression("", DEFAULT_TEMPLATE_PARSER_CONTEXT);
o = expr.getValue();
assertEquals("", o.toString());
expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
o = expr.getValue();
assertEquals("abc", o.toString());
expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
o = expr.getValue((Object)null);
assertEquals("abc", o.toString());
}
示例2: indexIntoGenericPropertyContainingMapObject
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void indexIntoGenericPropertyContainingMapObject() {
Map<String, Map<String, String>> property = new HashMap<String, Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("foo", "bar");
property.put("property", map);
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new MapAccessor());
context.setRootObject(property);
Expression expression = parser.parseExpression("property");
assertEquals("java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(context).toString());
assertEquals(map, expression.getValue(context));
assertEquals(map, expression.getValue(context, Map.class));
expression = parser.parseExpression("property['foo']");
assertEquals("bar", expression.getValue(context));
}
示例3: checkTemplateParsingError
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
private void checkTemplateParsingError(String expression, ParserContext context, String expectedMessage) throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
try {
parser.parseExpression(expression, context);
fail("Should have failed with message: " + expectedMessage);
}
catch (Exception ex) {
String message = ex.getMessage();
if (ex instanceof ExpressionException) {
message = ((ExpressionException) ex).getSimpleMessage();
}
if (!message.equals(expectedMessage)) {
ex.printStackTrace();
}
assertThat(expectedMessage, equalTo(message));
}
}
示例4: indexerMapAccessor_12045
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void indexerMapAccessor_12045() throws Exception {
SpelParserConfiguration spc = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,this.getClass().getClassLoader());
SpelExpressionParser sep = new SpelExpressionParser(spc);
expression=sep.parseExpression("headers[command]");
MyMessage root = new MyMessage();
assertEquals("wibble",expression.getValue(root));
// This next call was failing because the isCompilable check in Indexer did not check on the key being compilable
// (and also generateCode in the Indexer was missing the optimization that it didn't need necessarily need to call
// generateCode for that accessor)
assertEquals("wibble",expression.getValue(root));
assertCanCompile(expression);
// What about a map key that is an expression - ensure the getKey() is evaluated in the right scope
expression=sep.parseExpression("headers[getKey()]");
assertEquals("wobble",expression.getValue(root));
assertEquals("wobble",expression.getValue(root));
expression=sep.parseExpression("list[getKey2()]");
assertEquals("wobble",expression.getValue(root));
assertEquals("wobble",expression.getValue(root));
expression = sep.parseExpression("ia[getKey2()]");
assertEquals(3,expression.getValue(root));
assertEquals(3,expression.getValue(root));
}
示例5: indexIntoGenericPropertyContainingGrowingList
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void indexIntoGenericPropertyContainingGrowingList() {
List<String> property = new ArrayList<String>();
this.property = property;
SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property");
assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
assertEquals(property, expression.getValue(this));
expression = parser.parseExpression("property[0]");
try {
assertEquals("bar", expression.getValue(this));
} catch (EvaluationException e) {
assertTrue(e.getMessage().startsWith("EL1053E"));
}
}
示例6: limitCollectionGrowing
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void limitCollectionGrowing() throws Exception {
TestClass instance = new TestClass();
StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(true, true, 3));
Expression expression = parser.parseExpression("foo[2]");
expression.setValue(ctx, "2");
assertThat(instance.getFoo().size(), equalTo(3));
expression = parser.parseExpression("foo[3]");
try {
expression.setValue(ctx, "3");
} catch(SpelEvaluationException see) {
assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode());
assertThat(instance.getFoo().size(), equalTo(3));
}
}
示例7: SPR11348
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void SPR11348() {
Collection<String> coll = new LinkedHashSet<String>();
coll.add("one");
coll.add("two");
coll = Collections.unmodifiableCollection(coll);
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("new java.util.ArrayList(#root)");
Object value = expr.getValue(coll);
assertTrue(value instanceof ArrayList);
@SuppressWarnings("rawtypes")
ArrayList list = (ArrayList) value;
assertEquals("one", list.get(0));
assertEquals("two", list.get(1));
}
示例8: testMethodThrowingException_SPR6941
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
/**
* Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped.
*/
@Test
public void testMethodThrowingException_SPR6941() {
// Test method on inventor: throwException()
// On 1 it will throw an IllegalArgumentException
// On 2 it will throw a RuntimeException
// On 3 it will exit normally
// In each case it increments the Inventor field 'counter' when invoked
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("throwException(#bar)");
eContext.setVariable("bar", 2);
try {
expr.getValue(eContext);
fail();
}
catch (Exception ex) {
if (ex instanceof SpelEvaluationException) {
fail("Should not be a SpelEvaluationException: " + ex);
}
// normal
}
}
示例9: getMartinis
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Override
public Collection<Martini> getMartinis(@Nullable String spelFilter) {
String trimmed = null == spelFilter ? "" : spelFilter.trim();
Collection<Martini> martinis = null;
if (!trimmed.isEmpty()) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(spelFilter);
martinis = getMartinis(expression);
}
return null == martinis ? getMartinis() : martinis;
}
示例10: encryptUsingPlatformBean
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Override
public String encryptUsingPlatformBean(String cleartext) {
SpelExpressionParser expressionParser = new SpelExpressionParser();
String encryptExpressionString = ApplicationProperties.get("encryptExpression");
Expression encryptExpression = expressionParser.parseExpression(encryptExpressionString);
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("stringToEncrypt", cleartext);
return encryptExpression.getValue(context, String.class);
}
示例11: parseSpelExpression
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
public static void parseSpelExpression(String property) {
SpelExpressionParser parser = new SpelExpressionParser();
//Static value not that useful .. but safe
Expression exp1 = parser.parseExpression("'safe expression'");
String constantValue = exp1.getValue(String.class);
System.out.println("exp1="+constantValue);
StandardEvaluationContext testContext = new StandardEvaluationContext(TEST_PERSON);
//Unsafe if the input is control by the user..
Expression exp2 = parser.parseExpression(property+" == 'Benoit'");
String dynamicValue = exp2.getValue(testContext, String.class);
System.out.println("exp2=" + dynamicValue);
}
示例12: renderQueryIfExpressionOrReturnQuery
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
/**
* @param query, the query expression potentially containing a SpEL expression. Must not be {@literal null}.}
* @param metadata the {@link EntityMetadata} for the given entity. Must not be {@literal null}.
* @param parser Must not be {@literal null}.
* @return
*/
private static String renderQueryIfExpressionOrReturnQuery(String query, EntityMetadata<?> metadata,
SpelExpressionParser parser) {
Assert.notNull(query, "query must not be null!");
Assert.notNull(metadata, "metadata must not be null!");
Assert.notNull(parser, "parser must not be null!");
if (!containsExpression(query)) {
return query;
}
StandardEvaluationContext evalContext = new StandardEvaluationContext();
evalContext.setVariable(ENTITY_NAME, metadata.getJavaType().getName());
query = potentiallyQuoteExpressionsParameter(query);
Expression expr = parser.parseExpression(query, ParserContext.TEMPLATE_EXPRESSION);
String result = expr.getValue(evalContext, String.class);
if (result == null) {
return query;
}
return potentiallyUnquoteParameterExpressions(result);
}
示例13: SPR12522
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
@SuppressWarnings("rawtypes")
public void SPR12522() {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("T(java.util.Arrays).asList('')");
Object value = expression.getValue();
assertTrue(value instanceof List);
assertTrue(((List) value).isEmpty());
}
示例14: mapAccessorCompilable
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void mapAccessorCompilable() {
Map<String, Object> testMap = getSimpleTestMap();
StandardEvaluationContext sec = new StandardEvaluationContext();
sec.addPropertyAccessor(new MapAccessor());
SpelExpressionParser sep = new SpelExpressionParser();
// basic
Expression ex = sep.parseExpression("foo");
assertEquals("bar",ex.getValue(sec,testMap));
assertTrue(SpelCompiler.compile(ex));
assertEquals("bar",ex.getValue(sec,testMap));
// compound expression
ex = sep.parseExpression("foo.toUpperCase()");
assertEquals("BAR",ex.getValue(sec,testMap));
assertTrue(SpelCompiler.compile(ex));
assertEquals("BAR",ex.getValue(sec,testMap));
// nested map
Map<String,Map<String,Object>> nestedMap = getNestedTestMap();
ex = sep.parseExpression("aaa.foo.toUpperCase()");
assertEquals("BAR",ex.getValue(sec,nestedMap));
assertTrue(SpelCompiler.compile(ex));
assertEquals("BAR",ex.getValue(sec,nestedMap));
// avoiding inserting checkcast because first part of expression returns a Map
ex = sep.parseExpression("getMap().foo");
MapGetter mapGetter = new MapGetter();
assertEquals("bar",ex.getValue(sec,mapGetter));
assertTrue(SpelCompiler.compile(ex));
assertEquals("bar",ex.getValue(sec,mapGetter));
}
示例15: resolveMapKeyValueTypes
import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void resolveMapKeyValueTypes() {
mapNotGeneric = new HashMap();
mapNotGeneric.put("baseAmount", 3.11);
mapNotGeneric.put("bonusAmount", 7.17);
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("mapNotGeneric");
assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(this).toString());
}