Skip to content

Instantly share code, notes, and snippets.

@corneil
Created August 12, 2019 14:18
Show Gist options
  • Save corneil/6b3712e7418068b9e5eba85c04675718 to your computer and use it in GitHub Desktop.
Save corneil/6b3712e7418068b9e5eba85c04675718 to your computer and use it in GitHub Desktop.
Spring Spel fails
import org.junit.Assert.assertEquals
import org.junit.Test
import org.springframework.core.convert.TypeDescriptor
import org.springframework.expression.spel.SpelParserConfiguration
import org.springframework.expression.spel.standard.SpelExpressionParser
import org.springframework.expression.spel.support.SimpleEvaluationContext
class PropertyHolder(val properties: Map<String, Any>)
class SpelTests {
@Test
fun testSpel() {
val exp = "properties.name + ':Extra:' + properties.value1 + ':' + properties.value2"
val properties = mapOf<String, Any>("name" to "joe", "value1" to 2, "value2" to 4.5)
val expressionParser = SpelExpressionParser(SpelParserConfiguration())
val expression = expressionParser.parseExpression(exp)
val root = PropertyHolder(properties)
val context = SimpleEvaluationContext.forReadOnlyDataBinding().withTypedRootObject(
root, TypeDescriptor.valueOf(PropertyHolder::class.java)
).build()
val output = expression.getValue(context, String::class)
assertEquals(output, "joe:Extra:2:4.5")
}
}
@corneil
Copy link
Author

corneil commented Aug 12, 2019

Test output:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'properties' cannot be found on object of type 'kotlin.reflect.jvm.internal.KClassImpl' - maybe not public or not valid?

	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:53)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:89)
	at org.springframework.expression.spel.ast.OpPlus.getValueInternal(OpPlus.java:83)
	at org.springframework.expression.spel.ast.OpPlus.getValueInternal(OpPlus.java:83)
	at org.springframework.expression.spel.ast.OpPlus.getValueInternal(OpPlus.java:83)
	at org.springframework.expression.spel.ast.OpPlus.getValueInternal(OpPlus.java:83)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment