congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
Expression
Code IndexAdd Tabnine to your IDE (free)

How to use
Expression
in
org.springframework.expression

Best Java code snippets using org.springframework.expression.Expression (Showing top 20 results out of 2,268)

Refine searchRefine arrow

  • SpelExpressionParser
  • StandardEvaluationContext
  • ExpressionParser
origin: spring-projects/spring-framework

@Test
public void testAssignment() throws Exception {
  Inventor inventor = new Inventor();
  StandardEvaluationContext inventorContext = new StandardEvaluationContext();
  inventorContext.setRootObject(inventor);
  parser.parseExpression("foo").setValue(inventorContext, "Alexander Seovic2");
  assertEquals("Alexander Seovic2",parser.parseExpression("foo").getValue(inventorContext,String.class));
  // alternatively
  String aleks = parser.parseExpression("foo = 'Alexandar Seovic'").getValue(inventorContext, String.class);
  assertEquals("Alexandar Seovic",parser.parseExpression("foo").getValue(inventorContext,String.class));
  assertEquals("Alexandar Seovic",aleks);
}
origin: spring-projects/spring-framework

/**
 * Output an indented representation of the expression syntax tree to the specified output stream.
 * @param printStream the output stream to print into
 * @param expression the expression to be displayed
 */
public static void printAbstractSyntaxTree(PrintStream printStream, Expression expression) {
  printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST start");
  printAST(printStream, ((SpelExpression) expression).getAST(), "");
  printStream.println("===> Expression '" + expression.getExpressionString() + "' - AST end");
}
origin: spring-projects/spring-framework

@Test
public void testSetParameterizedList() throws Exception {
  StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
  Expression e = parser.parseExpression("listOfInteger.size()");
  assertEquals(0,e.getValue(context,Integer.class).intValue());
  context.setTypeConverter(new TypeConvertorUsingConversionService());
  // Assign a List<String> to the List<Integer> field - the component elements should be converted
  parser.parseExpression("listOfInteger").setValue(context,listOfString);
  assertEquals(3,e.getValue(context,Integer.class).intValue()); // size now 3
  Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer
  assertEquals(Integer.class,clazz);
}
origin: spring-projects/spring-framework

@Test
public void setGenericPropertyContainingMap() {
  Map<String, String> property = new HashMap<>();
  property.put("foo", "bar");
  this.property = property;
  SpelExpressionParser parser = new SpelExpressionParser();
  Expression expression = parser.parseExpression("property");
  assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(this).toString());
  assertEquals(property, expression.getValue(this));
  expression = parser.parseExpression("property['foo']");
  assertEquals("bar", expression.getValue(this));
  expression.setValue(this, "baz");
  assertEquals("baz", expression.getValue(this));
}
origin: spring-projects/spring-framework

/** Should be accessing Goo.setKey field because 'bar' variable evaluates to "key" */
@Test
public void indexingAsAPropertyAccess_SPR6968_5() {
  Goo g = Goo.instance;
  StandardEvaluationContext context = new StandardEvaluationContext(g);
  Expression expr = null;
  expr = new SpelExpressionParser().parseRaw("instance[bar]='world'");
  expr.getValue(context, String.class);
  assertEquals("world", g.value);
  expr.getValue(context, String.class); // will be using the cached accessor this time
  assertEquals("world", g.value);
}
origin: spring-projects/spring-framework

@Test(expected = SpelEvaluationException.class)
public void spelExpressionListIndexAccessNullVariables() {
  ExpressionParser parser = new SpelExpressionParser();
  Expression spelExpression = parser.parseExpression("#aList[0] eq 'one'");
  spelExpression.getValue();
}
origin: spring-projects/spring-framework

@Test
public void varargsAgainstProxy_SPR16122() {
  SpelExpressionParser parser = new SpelExpressionParser();
  Expression expr = parser.parseExpression("process('a', 'b')");
  VarargsReceiver receiver = new VarargsReceiver();
  VarargsInterface proxy = (VarargsInterface) Proxy.newProxyInstance(
      getClass().getClassLoader(), new Class<?>[] {VarargsInterface.class},
      (proxy1, method, args) -> method.invoke(receiver, args));
  assertEquals("OK", expr.getValue(new StandardEvaluationContext(receiver)));
  assertEquals("OK", expr.getValue(new StandardEvaluationContext(proxy)));
}
origin: spring-projects/spring-framework

@Test
public void SPR10125() {
  StandardEvaluationContext context = new StandardEvaluationContext();
  String fromInterface = parser.parseExpression("T(" + StaticFinalImpl1.class.getName() + ").VALUE").getValue(
      context, String.class);
  assertThat(fromInterface, is("interfaceValue"));
  String fromClass = parser.parseExpression("T(" + StaticFinalImpl2.class.getName() + ").VALUE").getValue(
      context, String.class);
  assertThat(fromClass, is("interfaceValue"));
}
origin: spring-projects/spring-framework

@Test
public void mapWithAllStringValues() {
  Map<String, Object> map = new HashMap<>();
  map.put("x", "1");
  map.put("y", "2");
  map.put("z", "3");
  Expression expression = parser.parseExpression("foo(#props)");
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setVariable("props", map);
  String result = expression.getValue(context, new TestBean(), String.class);
  assertEquals("123", result);
}
origin: spring-projects/spring-framework

@Test
public void testParsingNormalExpressionThroughTemplateParser() throws Exception {
  Expression expr = parser.parseExpression("1+2+3");
  assertEquals(6, expr.getValue());
}
origin: spring-projects/spring-framework

@Test
public void testVariableMapAccess() throws Exception {
  ExpressionParser parser = new SpelExpressionParser();
  StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
  ctx.setVariable("day", "saturday");
  Expression expr = parser.parseExpression("testMap[#day]");
  Object value = expr.getValue(ctx, String.class);
  assertEquals("samstag", value);
}
origin: spring-projects/spring-framework

@Test
public void testCustomMapAccessor() throws Exception {
  ExpressionParser parser = new SpelExpressionParser();
  StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
  ctx.addPropertyAccessor(new MapAccessor());
  Expression expr = parser.parseExpression("testMap.monday");
  Object value = expr.getValue(ctx, String.class);
  assertEquals("montag", value);
}
origin: spring-projects/spring-framework

@Test
public void propertyReadWrite() {
  EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
  Expression expr = parser.parseExpression("name");
  Person target = new Person("p1");
  assertEquals("p1", expr.getValue(context, target));
  target.setName("p2");
  assertEquals("p2", expr.getValue(context, target));
  parser.parseExpression("name='p3'").getValue(context, target);
  assertEquals("p3", target.getName());
  assertEquals("p3", expr.getValue(context, target));
  expr.setValue(context, target, "p4");
  assertEquals("p4", target.getName());
  assertEquals("p4", expr.getValue(context, target));
}
origin: spring-projects/spring-framework

@Test
public void limitCollectionGrowing() {
  TestClass instance = new TestClass();
  StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
  SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(true, true, 3));
  Expression e = parser.parseExpression("foo[2]");
  e.setValue(ctx, "2");
  assertThat(instance.getFoo().size(), equalTo(3));
  e = parser.parseExpression("foo[3]");
  try {
    e.setValue(ctx, "3");
  }
  catch (SpelEvaluationException see) {
    assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode());
    assertThat(instance.getFoo().size(), equalTo(3));
  }
}
origin: spring-projects/spring-framework

protected void setValue(String expression, Object value) {
  try {
    Expression e = parser.parseExpression(expression);
    if (e == null) {
      fail("Parser returned null for expression");
    }
    if (DEBUG) {
      SpelUtilities.printAbstractSyntaxTree(System.out, e);
    }
    StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
    assertTrue("Expression is not writeable but should be", e.isWritable(lContext));
    e.setValue(lContext, value);
    assertEquals("Retrieved value was not equal to set value", value, e.getValue(lContext,value.getClass()));
  }
  catch (EvaluationException | ParseException ex) {
    ex.printStackTrace();
    fail("Unexpected Exception: " + ex.getMessage());
  }
}
origin: spring-projects/spring-framework

@Test
public void SPR16123() {
  ExpressionParser parser = new SpelExpressionParser();
  parser.parseExpression("simpleProperty").setValue(new BooleanHolder(), null);
  try {
    parser.parseExpression("primitiveProperty").setValue(new BooleanHolder(), null);
    fail("Should have thrown EvaluationException");
  }
  catch (EvaluationException ex) {
    // expected
  }
}
origin: spring-projects/spring-framework

@Test
public void SPR5905_InnerTypeReferences() {
  StandardEvaluationContext context = new StandardEvaluationContext(new Spr5899Class());
  Expression expr = new SpelExpressionParser().parseRaw("T(java.util.Map$Entry)");
  assertEquals(Map.Entry.class, expr.getValue(context));
  expr = new SpelExpressionParser().parseRaw("T(org.springframework.expression.spel.SpelReproTests$Outer$Inner).run()");
  assertEquals(12, expr.getValue(context));
  expr = new SpelExpressionParser().parseRaw("new org.springframework.expression.spel.SpelReproTests$Outer$Inner().run2()");
  assertEquals(13, expr.getValue(context));
}
origin: spring-projects/spring-framework

@Test(expected = SpelEvaluationException.class)
public void spelExpressionListNullVariables() {
  ExpressionParser parser = new SpelExpressionParser();
  Expression spelExpression = parser.parseExpression("#aList.contains('one')");
  spelExpression.getValue();
}
origin: spring-projects/spring-framework

@Test
public void shouldAlwaysUsePropertyAccessorFromEvaluationContext() {
  SpelExpressionParser parser = new SpelExpressionParser();
  Expression expression = parser.parseExpression("name");
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Ollie")));
  assertEquals("Ollie", expression.getValue(context));
  context = new StandardEvaluationContext();
  context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Jens")));
  assertEquals("Jens", expression.getValue(context));
}
origin: spring-projects/spring-framework

@Test
public void testAccessingPropertyOfClass() {
  Expression expression = parser.parseExpression("name");
  Object value = expression.getValue(new StandardEvaluationContext(String.class));
  assertEquals("java.lang.String", value);
}
org.springframework.expressionExpression

Javadoc

An expression capable of evaluating itself against context objects. Encapsulates the details of a previously parsed expression string. Provides a common abstraction for expression evaluation.

Most used methods

  • getValue
    Evaluate the expression in a specified context which can resolve references to properties, methods,
  • getExpressionString
    Return the original string used to create this expression (unmodified).
  • setValue
    Set this expression in the provided context to the value provided. The supplied root object override
  • getValueType
    Return the most general type that can be passed to the #setValue(EvaluationContext,Object,Object) me
  • getValueTypeDescriptor
    Return the most general type that can be passed to the #setValue(EvaluationContext,Object,Object) me
  • isWritable
    Determine if an expression can be written to, i.e. setValue() can be called. The supplied root objec

Popular in Java

  • Making http post requests using okhttp
  • getSystemService (Context)
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • putExtra (Intent)
  • OutputStream (java.io)
    A writable sink for bytes.Most clients will use output streams that write data to the file system (
  • ResultSet (java.sql)
    An interface for an object which represents a database table entry, returned as the result of the qu
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • Options (org.apache.commons.cli)
    Main entry-point into the library. Options represents a collection of Option objects, which describ
  • Table (org.hibernate.mapping)
    A relational table
  • Option (scala)
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now