If you want to customize you own JsonToKotlinClass
Plugin or want to contribute some new featrues for this project, This guide will help you.
Customize your own plugin by these steps, and you will have your own plugin, in these guide we will create a extension that will make all the property name to be all upper case
-
Fork
JsonToKotlinClass
from Github -
Clone your forked project to local
-
Open Project with Intellij Idea
-
Create package under ..src/main/kotlin/extensions
Package name could be your own domain name. in this case we will use
wu.seal
-
Create a Extension object class file under your created package
-
Now the created file would like this:
-
Add interfaces to be implemente
-
Implement all the needed interface
package extensions.wu.seal import extensions.Extension import wu.seal.jsontokotlin.model.classscodestruct.KotlinDataClass import javax.swing.JPanel object AllUpperCase :Extension(){ override fun createUI(): JPanel { } override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass { } }
-
Then we create a new UI for switching to enable all property name to be all upper case
override fun createUI(): JPanel { val configKey = "wu.seal.all_to_be_upper_case" val checkBox = JCheckBox("Make all properties name to be all upper case").apply { isSelected = getConfig(configKey).toBoolean() addActionListener { setConfig(configKey, isSelected.toString()) } } return panel { row { checkBox() } } }
-
And make the properties names to be all upper case
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass { return if (getConfig(configKey).toBoolean()) { //make all properties name to be all upper case val newProperties = kotlinDataClass.properties.map { it.copy(name = it.name.toUpperCase()) } kotlinDataClass.copy(properties = newProperties) } else { kotlinDataClass } }
We use
map
method to create a new properties which all properties names are translate to be all upper case, And copy the incoming parameterkotlinDataClass
with replacing the new created properties, and return it to the system. -
Add the new object class into
ExtensionsCollector
object class which inside extensions package/** * extension collect, all extensions will be hold by this class's extensions property */ object ExtensionsCollector { /** * all extensions */ val extensions = listOf( PropertyPrefixSupport, PropertySuffixSupport, AllUpperCase ) }
-
Run the plugin by clicking
runIde
taskThen we will see the new IDE opened
-
Create a new project in new opened IDE and create a new Kotlin File for inserting new Kotlin Data Class code, and open
JsonToKotlinClass
Plugin, Then we will see that new Extension was added in the extensions tab,Let's selected the new added checkbox and have a test if it works ok. -
Then Let's see the tested result:
import com.google.gson.annotations.SerializedName data class TestCode( @SerializedName("hello") val HELLO: String = "" )
Yeah! That what we want. Greate we have created the first our style plugin.
Any Question? Welcome to raise an issue here
Demo could be found here