https://kotlinlang.org logo
#compiler
Title
# compiler
c

Chachako

02/03/2022, 5:54 PM
I wonder if the frontend of compiler plugin has extension that implicitly modify visibility of declarations. I want to make the constructors of all classes marked with an annotation
private
. Is that possible now?
d

dmitriy.novozhilov

02/03/2022, 6:35 PM
No, but it will be possible with K2 plugins API
K 4
r

ralf

02/03/2022, 7:46 PM
That’s exciting!
c

Chachako

02/04/2022, 3:35 AM
No, but it will be possible with K2 plugins API
And is it possible to remove implicit declarations now or K2, for example Kotlin will create a primary constructor for the class implicitly, I want to remove it
d

dmitriy.novozhilov

02/04/2022, 7:10 AM
No, declarations removal is not supported, because it may break some contracts (e.g. contract that each class should have at least one constructor) What's your usecase?
c

Chachako

02/04/2022, 8:23 AM
What’s your usecase?
I have a class that doesn’t allow manual creation (instance are usually returned by certain methods), so I want to delete the primary constructor or make its visibility
private
, also, when there is a primary constructor,
operator fun invoke()
will be overwritten.
d

dmitriy.novozhilov

02/04/2022, 8:34 AM
You can not delete it, because without any constructor you can not instantiate it (lack of constructor also breaks jvm bytecode contracts) But, if class have no explicit constructors frontend generates implicit one for which you can change visibiltiy to
private
as for explicitly declared constructor
You even can distinguish implicit primary constrcutor from explicit one (e.g. with no arguments) and apply some different logic to them if you want
c

Chachako

02/04/2022, 8:39 AM
Yes, I mean, if any constructor is explicitly declared, I will use the
Checker
to report errors, so I just want to hide the primary constructor automatically generated by the frontend
3 Views