Any idea what's happening here? `lastJobExecution`...
# getting-started
d
Any idea what's happening here?
lastJobExecution
is clearly of type
JobExecution?
m
What's the declaration of
getLastJobExecution
? Does this compile using gradle, sometimes the IDE reports false positive errors.
d
Copy code
@Nullable
	JobExecution getLastJobExecution(String jobName, JobParameters jobParameters);
And the maven build fails with the same error
m
What is
BatchStatus.FAILED
? Is that a mutable boolean variable or is it being compared to something under the tooltip?
This compiles for me
Copy code
class JobExecution(
    val isSomethingICannotSee: Boolean
)
typealias JobParameters = String
const val IMPORT_ORDER_JOB_NAME = "name"
interface JobRepository {
    fun getLastJobExecution(jobName: String, jobParameters: JobParameters): JobExecution?
}

lateinit var jobRepository: JobRepository
fun validate(jobParameters: JobParameters) {
    val lastJobExecution: JobExecution? = jobRepository.getLastJobExecution(
        IMPORT_ORDER_JOB_NAME,
        jobParameters
    )

    val error = when {
        lastJobExecution == null -> "Job does not exist"
        lastJobExecution.isSomethingICannotSee -> "other error"
        else -> null
    }
}
Could be some Java/Kotlin interop issue. but the null check should be valid.
j
Had the same issue years ago. Never figured it out
d
Thanks for sharing Jacob. I gave up after a couple of minutes and went with
lastJobExecution?.someProperty == null
which worked fine.
m
I would report a bug to Kotlin.
k
You could also try
lastJobExecution === null
- which seems unnecessarily longer than
==
but still shorter than your last workaround.
j
I suspect it's springs fault. https://github.com/spring-projects/spring-batch/blob/main/spring-batch-core/src/main/java/org/springframework/batch/core/package-info.java makes all parameters nonnullable by default which is preventing the equals implementation from being picked up by kotlin
k
That's interesting, but if Spring Batch inadvertently defines
equals(Any)
instead of
equals(Any?)
, wouldn't
ExitStatus
still inherit
equals(Any?)
from its superclass
Any
?
m
Not really, since even though
null
is part of the type system it doesn't change the function signatures on the JVM. So the function is overriding the function but not in a valid way since they are restricting the type of an input parameter. Kotlin probably wouldn't allow it, but since null is not part of the Java type system, Java cannot. So yes, Spring should annotate the override with Nullable, since they handle null and not handling null would be a contract violation. So Kotlin, knows if they call
equals
they will get the one in
ExitStatus
, and not the one in
Any
, but Spring says they cannot pass
null
into it, so the compiler is stuck saying the operation is illegal.