Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose public getters of a POJO #423

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,45 @@ package org.camunda.feel.impl.interpreter
import org.camunda.feel.context.{Context, FunctionProvider, VariableProvider}
import org.camunda.feel.syntaxtree.ValFunction

import java.lang.reflect.Method

/**
* A context that wraps the fields and methods of a given JVM object
*
* @param obj the JVM object to be wrapped
*/
case class ObjectContext(obj: Any) extends Context {

private lazy val publicFields = obj.getClass.getFields
private lazy val allFields = obj.getClass.getDeclaredFields

private lazy val publicMethodsWithoutArguments = obj.getClass.getMethods
.filter(method => method.getParameterCount == 0)

override val variableProvider = new VariableProvider {
override def getVariable(name: String): Option[Any] = {

val objClass = obj.getClass
val fields = objClass.getFields find (field => field.getName == name)
val fieldForName = publicFields find (field => field.getName == name)

fields.map(_.get(obj)) orElse {
val methods = objClass.getMethods find (method => {
val methodName = method.getName
val returnType = method.getReturnType
val hasParameters = method.getParameterCount > 0

!hasParameters && (methodName == name ||
methodName == getGetterName(name) ||
((returnType == java.lang.Boolean.TYPE ||
returnType == classOf[java.lang.Boolean]) &&
methodName == getBooleanGetterName(name)))
})
fieldForName.map(_.get(obj)) orElse {
val methods = publicMethodsWithoutArguments find (method =>
isGetterOf(method, name) || isBooleanGetterOf(method, name))

methods.map(_.invoke(obj))
}
}

override def keys: Iterable[String] = obj.getClass.getFields.map(_.getName)
override def keys: Iterable[String] = {
val fieldsWithPublicGetter = allFields.filter(
field =>
publicMethodsWithoutArguments.exists(
method =>
isGetterOf(method, field.getName) || isBooleanGetterOf(
method,
field.getName)))

publicFields.map(_.getName) ++ fieldsWithPublicGetter.map(_.getName)
}
}

override val functionProvider = new FunctionProvider {
Expand Down Expand Up @@ -86,4 +94,17 @@ case class ObjectContext(obj: Any) extends Context {
private def getBooleanGetterName(fieldName: String) =
"is" + fieldName.capitalize

private def isGetterOf(method: Method, fieldName: String): Boolean = {
val methodName = method.getName

methodName == fieldName || methodName == getGetterName(fieldName)
}

private def isBooleanGetterOf(method: Method, fieldName: String): Boolean = {
val returnType = method.getReturnType

method.getName == getBooleanGetterName(fieldName) && (returnType == java.lang.Boolean.TYPE || returnType == classOf[
java.lang.Boolean])
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package org.camunda.feel.impl.interpreter

import org.camunda.feel.impl.FeelIntegrationTest
import org.camunda.feel.syntaxtree._
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

/**
* @author Philipp Ossler
Expand All @@ -39,7 +39,9 @@ class InterpreterBeanExpressionTest

it should "access a getter method as field" in {

class A(b: Int) { def getFoo() = b + 1 }
class A(b: Int) {
def getFoo() = b + 1
}

eval("a.foo", Map("a" -> new A(2))) should be(ValNumber(3))

Expand Down Expand Up @@ -67,17 +69,39 @@ class InterpreterBeanExpressionTest
)
}

it should "not access a private field" in {
class A(private val x: Int)

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

it should "not access a private method" in {
class A(val x: Int) {
private def getResult(): Int = x
}

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

it should "invoke a method without arguments" in {

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

eval("a.foo()", Map("a" -> new A())) should be(ValString("foo"))

}

it should "invoke a method with one argument" in {

class A { def incr(x: Int) = x + 1 }
class A {
def incr(x: Int) = x + 1
}

eval("a.incr(1)", Map("a" -> new A())) should be(ValNumber(2))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,57 @@ class DefaultValueMapperTest extends AnyFlatSpec with Matchers {
}

it should "convert from object" in {
case class Obj(val a: Int, val b: String)

case class Obj(a: Int, b: String)
val result = valueMapper.toVal(Obj(a = 2, b = "foo"))
result shouldBe a[ValContext]
}

it should "convert from object with public fields" in {
case class Obj(val a: Int, val b: String)

valueMapper.toVal(Obj(a = 2, b = "foo")) match {
case ValContext(context) =>
val variables = context.variableProvider.getVariables
variables should be(Map("a" -> 2, "b" -> "foo"))
}
}

it should "convert from object with public getters" in {
case class Obj(private val a: Int, private val b: String) {
def getA(): Int = a
def getB(): String = b
}

valueMapper.toVal(Obj(a = 2, b = "foo")) match {
case ValContext(context) =>
val variables = context.variableProvider.getVariables
variables should be(Map("a" -> 2, "b" -> "foo"))
}
}

it should "convert from object with public boolean getter" in {
case class Obj(private val a: Boolean) {
def isA(): Boolean = a
}

valueMapper.toVal(Obj(2, "foo")) shouldBe a[ValContext]
valueMapper.toVal(Obj(a = true)) match {
case ValContext(context) =>
val variables = context.variableProvider.getVariables
variables should be(Map("a" -> true))
}
}

it should "convert from object ignore private fields and methods" in {
case class Obj(val a: Int, private val b: String) {
def getC(): String = "c"
}

valueMapper.toVal(Obj(a = 2, b = "foo")) match {
case ValContext(context) =>
val variables = context.variableProvider.getVariables
variables should be(Map("a" -> 2))
}
}

it should "convert from Some" in {
Expand Down