|
| 1 | +If you want to customize you own `JsonToKotlinClass` Plugin or want to contribute some new featrues for this project, This guide will help you. |
| 2 | + |
| 3 | +#### Customize Your Own Plugin |
| 4 | + |
| 5 | +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 |
| 6 | + |
| 7 | +1. Fork `JsonToKotlinClass` from Github |
| 8 | + |
| 9 | +2. Clone your forked project to local |
| 10 | + |
| 11 | +3. Open Project with Intellij Idea |
| 12 | + |
| 13 | +4. Create package under ..src/main/kotlin/extensions |
| 14 | + |
| 15 | +  |
| 16 | + |
| 17 | + Package name could be your own domain name. in this case we will you `wu.seal` |
| 18 | + |
| 19 | +5. Create a Extension object class file under your created package |
| 20 | + |
| 21 | +  |
| 22 | + |
| 23 | +6. Now the created file would like this: |
| 24 | + |
| 25 | +  |
| 26 | + |
| 27 | +7. Add interfaces to be implemente |
| 28 | + |
| 29 | +  |
| 30 | + |
| 31 | +8. Implement all the needed interface |
| 32 | + |
| 33 | + ```kotlin |
| 34 | + package extensions.wu.seal |
| 35 | + |
| 36 | + import extensions.Extension |
| 37 | + import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass |
| 38 | + import javax.swing.JPanel |
| 39 | + |
| 40 | + object AllUpperCase :Extension(){ |
| 41 | + |
| 42 | + override fun createUI(): JPanel { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass { |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +9. Then we create a new UI for switching to enable all property name to be all upper case |
| 53 | + |
| 54 | + ```kotlin |
| 55 | + override fun createUI(): JPanel { |
| 56 | + |
| 57 | + val configKey = "wu.seal.all_to_be_upper_case" |
| 58 | + |
| 59 | + val checkBox = JCheckBox("Make all properties name to be all upper case").apply { |
| 60 | + isSelected = getConfig(configKey).toBoolean() |
| 61 | + addActionListener { |
| 62 | + setConfig(configKey, isSelected.toString()) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return panel { |
| 67 | + row { |
| 68 | + checkBox() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + ``` |
| 74 | + |
| 75 | +10. And make the properties names to be all upper case |
| 76 | + |
| 77 | + ```kotlin |
| 78 | + override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass { |
| 79 | + |
| 80 | + //make all properties name to be all upper case |
| 81 | + val newProperties = kotlinDataClass.properties.map { it.copy(name = it.name.toUpperCase()) } |
| 82 | + |
| 83 | + return kotlinDataClass.copy(properties = newProperties) |
| 84 | + } |
| 85 | + ``` |
| 86 | + |
| 87 | + We use `map`method 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. |
| 88 | + |
| 89 | +11. Add the new object class into `ExtensionsCollector` object class which inside extensions package |
| 90 | + |
| 91 | + ```kotlin |
| 92 | + /** |
| 93 | + * extension collect, all extensions will be hold by this class's extensions property |
| 94 | + */ |
| 95 | + object ExtensionsCollector { |
| 96 | + /** |
| 97 | + * all extensions |
| 98 | + */ |
| 99 | + val extensions = listOf( |
| 100 | + PropertyPrefixSupport, |
| 101 | + PropertySuffixSupport, |
| 102 | + AllUpperCase |
| 103 | + ) |
| 104 | + } |
| 105 | + ``` |
| 106 | + |
| 107 | +12. Run the plugin by clicking`runIde`task |
| 108 | + |
| 109 | +  |
| 110 | + |
| 111 | + Then we will see the new IDE opened |
| 112 | + |
| 113 | +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. |
| 114 | +
|
| 115 | +  |
| 116 | +
|
| 117 | +14. Then Let's see the tested result: |
| 118 | + |
| 119 | +  |
| 120 | + |
| 121 | + ```kotlin |
| 122 | + import com.google.gson.annotations.SerializedName |
| 123 | + data class TestCode( |
| 124 | + @SerializedName("hello") |
| 125 | + val HELLO: String = "" |
| 126 | + ) |
| 127 | + ``` |
| 128 | + |
| 129 | + Yeah! That what we want. Greate we have created the first our style plugin. |
| 130 | + |
| 131 | + Any Question? Welcome to raise an issue [here](https://github.com/wuseal/JsonToKotlinClass/issues) |
| 132 | + |
| 133 | + Demo could be found [here](https://github.com/wuseal/JsonToKotlinClass/tree/3.0-extension-demo) |
| 134 | + |
| 135 | + |
0 commit comments