Skip to content

Commit

Permalink
Builder-style method is used instead of getter (#421)
Browse files Browse the repository at this point in the history
* test: verify field access with arguments

* fix: ignore methods with parameters as fields

* an expression can access properties of a bean via field or method lookup
* ignore methods with arguments for field access
  • Loading branch information
saig0 committed May 4, 2022
1 parent 11f0947 commit 2c2d512
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ case class ObjectContext(obj: Any) extends Context {
val methods = objClass.getMethods find (method => {
val methodName = method.getName
val returnType = method.getReturnType
methodName == name ||
val hasParameters = method.getParameterCount > 0

!hasParameters && (methodName == name ||
methodName == getGetterName(name) ||
((returnType == java.lang.Boolean.TYPE ||
returnType == classOf[java.lang.Boolean]) &&
methodName == getBooleanGetterName(name))
methodName == getBooleanGetterName(name)))
})

methods.map(_.invoke(obj))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ class InterpreterBeanExpressionTest

}

it should "ignore getter method with arguments as field" in {

class A(x: Int) {
def getResult(y: Int): Int = x + y
}

eval("a.result", Map("a" -> new A(2))) should be(
ValError("context contains no entry with key 'result'")
)
}

it should "ignore method with arguments as field (builder-style)" in {

class A(x: Int) {
def plus(y: Int): A = new A(x + y)
}

eval("a.plus", Map("a" -> new A(2))) should be(
ValError("context contains no entry with key 'plus'")
)
}

it should "invoke a method without arguments" in {

class A { def foo() = "foo" }
Expand Down

0 comments on commit 2c2d512

Please sign in to comment.