Good morning chat, been a while. How exactly does ...
# configuration-cache
j
Good morning chat, been a while. How exactly does Gradle instantiate objects when re-using configuration cache? Does it create them using
ObjectFactory#newInstance
(or whatever the internal version of that is) with service injection support or does it try to build the class raw using the saved values. Asking because I am using a transient private field that stores an instance of
Problems
which is normally provided through service injection in constructor parameter or bean getter.
v
If it is provided through injection, why do you have it additionally in a private property?
j
Because I wrap it in an object that contains convenient helper methods
Not a big deal if I have to rework it but I figured I might as well ask anyway
v
And you mentioned it is "transient", how do you make it so? Using the internal
@Transient
annotation?
j
nah, just the
transient
keyword in Java
stopped getting "cannot serialize" errors when using that
I'm still an old-head using Java, sorry 😛
v
I'm still an old-head using Java, sorry
No need to apologize, Java is also my main language and for a public Gradle plugin I would never use anything else, but plain Java with target set to the lowest version that is supported by the lowest supported Gradle version, so Java 8 unless it is Gradle 9 only. 🙂 Much better for compatibility and less headaches. 🙂
j
I am targetting Gradle 9 😎
but yeah up until this point I set the language level to 8 for plugins
I learned that the hard way unfortunately
v
Well, then 17 of course - at least until you want to add support for older versions. 😄
j
Can Gradle 9's daemon even run older than 17??
v
The daemon not, no, at least not officially. Worker daemons though can.
j
Ahh, that's news to me. Through
WorkerExecutor
?
v
Yep. Some of the Gradle folks told me so, that various things in the Gradle codebase need to stay Java 8 compatible as worker daemons are still allowed to run on Java 8.
j
That's kind of an insane design choice but sure, we'll run with it
Although I don't think I've written a lot of things in my plugins that would necessarily run in worker daemons. Most of what I do is configuration stuff.
I've tried to use
WorkerExecutor
in configuration before and found that it doesn't really work/do anything
v
Why insane? Otherwise for example plugins that use that would break. Like when they use project isolation and set a Java 8 executable as toolchain to use.
If you don't use worker daemons in your plugin, you should not be affected by that, and if you do then it would still be fine if you compile your stuff against Java 17.
j
Oh ok that's the info I was looking for
v
It is just that if a plugin for whatever reason needs to run something in a worker daemon with Java 8 it will still work.
j
Ahhh I see
Ok we are on the same page now
this is no longer insane
👌 1
v
If for example the work action uses some library that uses Java 8 API that was changed in a newer Java version and thus would break.
But back to the topic, if the field is transient, I guess you should have some lazy-initialization for it like wrapping it in your helper when you actually need to use it?
j
Probably. Anyways as we were having our discussion I figured a better solution (for now) would be to make the field not final, still marked as transient, and in the getter, if the field is null use the
ObjectFactory#newInstance
to create it again. The class itself uses service injectors to get
Problems
and
ProviderFactory
and I use that as my Problems "wrapper" so to speak
v
Ah, got what you mean now. So like - in Kotlin if you excuse -
Copy code
interface ProblemsHolder {
    @get:Inject
    val problems: Problems
}
abstract class Foo : DefaultTask() {
    @get:Inject
    abstract val objects: ObjectFactory

    @Transient
    private val problemsHolder = objects.newInstance<ProblemsHolder>()

    @TaskAction
    fun execute() {
        val problemId: ProblemId = ProblemId.create("adhoc-plugin-deprecation", "Task is deprecated", ProblemGroup.create("foo", "Foo"))
        problemsHolder.problems.reporter.report(problemId) {
            contextualLabel("Foo context")
            documentedAt("https://no.where")
            severity(Severity.ERROR)
            solution("Do something else")
        }
    }
}
val foo by tasks.registering(Foo::class)
I think that should work fine. Can you maybe show an MCVE of what you have that does not work?
Ah, no, that was the wrong
@Transient
, the Gradle-internal one instead of the Kotlin/JVM one. Using the Kotlin/JVM one (should resemble the
transient
in Java afair) the field is
null
even if it is set in the constructor.
Just like with Java serialization and transient fields.
j
Yeah that's exactly it
I may not write in Kotlin but I have learned to read it somewhat now
j
Ah I see
I think for now my traditional "check for not null" approach will be fine, especially since I have access to the object factory even without configuration
👌 1
m
Because I wrap it in an object that contains convenient helper methods
I'm a bit late to the party, but why don't just serialize the wrapper object? Does it have some problematic state aside the
Problems
service itself? Or does it implements
Problems
itself (this is known to cause issues)?
j
It implements
Problems
itself, which in hindsight I don't need to do as I never even call the
getReporter()
on it.
why don't just serialize the wrapper object?
This was a wonderful suggestion and ended up working perfectly for me. Thank you!
👌 1