Issue
I have a simple sealed class
sealed class Target {
class User(val id: Long) : Target()
class City(val id: String) : Target()
}
that is used as a parameter of s Spring bean method. I'd like to cache the method via the @Cacheable
conditionally only when the parameter is User
.
@Cacheable(CACHE_NAME, condition = "#target is Target.User")
open fun getFeed(target: Target): Map<String, Any?> { ... }
However I get an error: '(' or <operator> expected, got 'is'
How can I use is
in the condition string?
Solution
Thanks to Raphael's answer I was able to find out that
- Instead of Kotlin's
is
there's Java'sinstanceof
. - SpEL has a special syntax for using
instanceof
where you need to use a wrapper around the class:filterObject instanceof T(YourClass)
. - The fully qualified class name must be used for classes from any other package than
java.lang
. - The fully qualified name available on runtime for a class defined inside the body of a sealed class is
<package>.<SealedClass>$<SubClass>
. In my case it wasnet.goout.feed.model.Target$User
.
Answered By - Jen