Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
139 lines (91 sloc) 4.54 KB

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

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

  1. Fork JsonToKotlinClass from Github

  2. Clone your forked project to local

  3. Open Project with Intellij Idea

  4. Create package under ..src/main/kotlin/extensions

    image

    Package name could be your own domain name. in this case we will use wu.seal

  5. Create a Extension object class file under your created package

    image

  6. Now the created file would like this:

    image

  7. Add interfaces to be implemente

    image

  8. Implement all the needed interface

    package extensions.wu.seal
    
    import extensions.Extension
    import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
    import javax.swing.JPanel
    
    object AllUpperCase :Extension(){
    
        override fun createUI(): JPanel {
    
        }
    
        override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
    
        }
    }
  9. 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()
                }
            }
    
        }
  10. 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 mapmethod to create a new properties which all properties names are translate to be all upper case, And copy the incoming parameter kotlinDataClass with replacing the new created properties, and return it to the system.

  11. 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
        )
    }
  12. Run the plugin by clickingrunIdetask

    image

    Then we will see the new IDE opened

  13. 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.

    image

  14. Then Let's see the tested result:

    image

    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

You can’t perform that action at this time.