Skip to content

Commit 41ac966

Browse files
author
seal
committed
Add new converter option :None(Camel case),this could rename property name to camelcase without annotations.
Add some respect test cases Make IsComment option to be false by default Optimize UI Optimize package name declaration
1 parent 1cda5dc commit 41ac966

File tree

13 files changed

+202
-42
lines changed

13 files changed

+202
-42
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>wu.seal.tool.jsontokotlin</id>
33
<name>JsonToKotlinClass</name>
4-
<version>1.5.1</version>
4+
<version>1.6</version>
55
<vendor email="[email protected]" url="https://www.github.com/wuseal">Seal</vendor>
66

77
<description><![CDATA[

src/wu/seal/jsontokotlin/ConfigManager.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package wu.seal.jsontokotlin
22

33
import com.intellij.ide.util.PropertiesComponent
4-
import wu.seal.jsontokotlin.statistics.sendConfigInfo
54
import wu.seal.jsontokotlin.supporter.GsonSupporter
65

76
/**
@@ -47,7 +46,7 @@ interface IConfigManager {
4746

4847

4948
var isCommentOff: Boolean
50-
get() = if (isTestModel) TestConfig.isCommentOff else PropertiesComponent.getInstance().isTrueValue(IS_COMMENT_OFF)
49+
get() = if (isTestModel) TestConfig.isCommentOff else PropertiesComponent.getInstance().getBoolean(IS_COMMENT_OFF, true)
5150
set(value) = if (isTestModel) {
5251
} else {
5352
PropertiesComponent.getInstance().setValue(IS_COMMENT_OFF, value)
@@ -84,14 +83,14 @@ interface IConfigManager {
8483
PropertiesComponent.getInstance().setValue(USER_UUID_KEY, value)
8584
}
8685

87-
var customAnnotaionImportClassString:String
86+
var customAnnotaionImportClassString: String
8887
get() = if (isTestModel) GsonSupporter.annotationImportClassString else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, GsonSupporter.annotationImportClassString)
8988
set(value) = if (isTestModel) {
9089
} else {
9190
PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, value)
9291
}
9392

94-
var customAnnotaionFormatString:String
93+
var customAnnotaionFormatString: String
9594
get() = if (isTestModel) GsonSupporter.anotaionOnProperty else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, GsonSupporter.anotaionOnProperty)
9695
set(value) = if (isTestModel) {
9796
} else {
@@ -103,7 +102,7 @@ interface IConfigManager {
103102
* This means which Json convert library you are using in you project
104103
*/
105104
enum class TargetJsonConverter {
106-
None, Gson, FastJson, Jackson, MoShi, LoganSquare, Custom
105+
None, NoneWithCamelCase, Gson, FastJson, Jackson, MoShi, LoganSquare, Custom
107106
}
108107

109108

src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import wu.seal.jsontokotlin.statistics.sendHistoryActionInfo
1010
* Created by Seal.wu on 2017/8/21.
1111
*/
1212

13-
const val PLUGIN_VERSION = "1.5.1"
13+
const val PLUGIN_VERSION = "1.6"
1414

1515
class JsonToKotlinApplication : ApplicationComponent {
1616

src/wu/seal/jsontokotlin/codeelements/KProperty.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import wu.seal.jsontokotlin.TargetJsonConverter
55
import wu.seal.jsontokotlin.supporter.*
66

77
/**
8+
*
89
* Created by Seal.Wu on 2017/9/18.
910
*/
1011

@@ -29,6 +30,10 @@ class KProperty(private val rawPropertyName: String, private val propertyType: S
2930

3031
blockBulder.append(NoneSupporter.getNoneLibSupporterProperty(rawPropertyName, propertyType))
3132

33+
} else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.NoneWithCamelCase) {
34+
35+
blockBulder.append(NoneWithCamelCaseSupporter.getNoneLibSupporterProperty(rawPropertyName, propertyType))
36+
3237
} else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Gson) {
3338

3439
blockBulder.append(GsonSupporter.getGsonSupporterProperty(rawPropertyName, propertyType))
@@ -55,6 +60,7 @@ class KProperty(private val rawPropertyName: String, private val propertyType: S
5560

5661
}
5762

63+
5864
if (!ConfigManager.isCommentOff && propertyValue.isNotBlank()) {
5965
blockBulder.append(" //").append(propertyValue)
6066
}

src/wu/seal/jsontokotlin/supporter/GsonSupporter.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package wu.seal.jsontokotlin.supporter
22

3+
import wu.seal.jsontokotlin.ConfigManager
34
import wu.seal.jsontokotlin.codeelements.KPropertyKeyword
45
import wu.seal.jsontokotlin.codeelements.KPropertyName
56
import wu.seal.jsontokotlin.codeelements.getDefaultValue
@@ -20,12 +21,6 @@ interface IGsonSupporter {
2021

2122
}
2223

23-
24-
fun main(args: Array<String>) {
25-
wu.seal.jsontokotlin.isTestModel = true
26-
println("getGsonSupporterProperty:\n ${GsonSupporter.getGsonSupporterProperty("seal is **() good_man ", "Boy")}")
27-
}
28-
2924
object GsonSupporter : IGsonSupporter {
3025

3126
/**
@@ -53,7 +48,7 @@ object GsonSupporter : IGsonSupporter {
5348

5449
gsonSupportPropertyBuilder.append(propertyType)
5550

56-
if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) {
51+
if (ConfigManager.initWithDefaultValue) {
5752
gsonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType))
5853
}
5954

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package wu.seal.jsontokotlin.supporter
2+
3+
import wu.seal.jsontokotlin.ConfigManager
4+
import wu.seal.jsontokotlin.codeelements.KPropertyKeyword
5+
import wu.seal.jsontokotlin.codeelements.KPropertyName
6+
import wu.seal.jsontokotlin.codeelements.getDefaultValue
7+
import wu.seal.jsontokotlin.isTestModel
8+
9+
/**
10+
*
11+
* Created by Seal.Wu on 2018/2/6.
12+
*/
13+
14+
15+
object NoneWithCamelCaseSupporter : INoneLibSupporter {
16+
17+
override fun getNoneLibSupporterClassName(rawClassName: String):String {
18+
return ""
19+
}
20+
21+
22+
override fun getNoneLibSupporterProperty(rawPropertyName: String, propertyType: String): String {
23+
24+
val blockBuilder = StringBuilder()
25+
26+
blockBuilder.append(KPropertyKeyword.get())
27+
blockBuilder.append(" ")
28+
blockBuilder.append(KPropertyName.getName(rawPropertyName))
29+
blockBuilder.append(": ").append(propertyType)
30+
if (ConfigManager.initWithDefaultValue) {
31+
blockBuilder.append(" = ").append(getDefaultValue(propertyType))
32+
}
33+
blockBuilder.append(",")
34+
35+
return blockBuilder.toString()
36+
}
37+
38+
}
39+

src/wu/seal/jsontokotlin/ui/ConfigSettingDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ConfigSettingDialog(canBeParent: Boolean) : DialogWrapper(canBeParent), IC
4040

4141
tabbedPane.add("Property", propertyPanel)
4242

43-
tabbedPane.add("Target Json Lib", targetJsonLibConfigPanel)
43+
tabbedPane.add("JSON Converter", targetJsonLibConfigPanel)
4444

4545
tabbedPane.add("Comment", commentConfigPanel)
4646

src/wu/seal/jsontokotlin/ui/JsonInputDialog.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ val myInputValidator = MyInputValidator()
4343
/**
4444
* Json input Dialog
4545
*/
46-
class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please input the class name and JSON text for generating Kotlin data class", "Make Kotlin Data Class", Messages.getInformationIcon(), "", myInputValidator) {
46+
class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please input the class name and JSON String for generating Kotlin data class", "Make Kotlin Data Class", Messages.getInformationIcon(), "", myInputValidator) {
4747

4848
private lateinit var classNameInput: JTextField
4949

@@ -91,7 +91,8 @@ class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please
9191
centerContainer.addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer)
9292
centerContainer.addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer)
9393
messagePanel.add(centerContainer, java.awt.BorderLayout.CENTER)
94-
val settingButton = javax.swing.JButton("Config Settings")
94+
val settingButton = JButton("Config Settings")
95+
settingButton.horizontalAlignment = SwingConstants.CENTER
9596
settingButton.addActionListener(object : AbstractAction() {
9697
override fun actionPerformed(e: ActionEvent) {
9798
ConfigSettingDialog(false).show()

src/wu/seal/jsontokotlin/ui/Panel.kt

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import com.intellij.util.ui.JBUI
77
import wu.seal.jsontokotlin.ConfigManager
88
import wu.seal.jsontokotlin.TargetJsonConverter
99
import wu.seal.jsontokotlin.addComponentIntoVerticalBoxAlignmentLeft
10+
import java.awt.Color
11+
import java.awt.FlowLayout
12+
import java.awt.GridLayout
13+
import java.awt.LayoutManager
14+
import java.awt.event.FocusEvent
15+
import java.awt.event.FocusListener
1016
import javax.swing.*
1117
import javax.swing.border.EmptyBorder
1218

@@ -16,11 +22,11 @@ import javax.swing.border.EmptyBorder
1622
* property config panel
1723
* Created by seal wu on 2017/9/18.
1824
*/
19-
class PropertyPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
25+
class PropertyPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
2026

21-
constructor(layout: java.awt.LayoutManager?) : this(layout, false)
27+
constructor(layout: LayoutManager?) : this(layout, false)
2228

23-
constructor(isDoubleBuffered: Boolean) : this(java.awt.FlowLayout(), isDoubleBuffered)
29+
constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered)
2430

2531

2632
init {
@@ -67,7 +73,7 @@ class PropertyPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean)
6773
val line = com.intellij.util.xml.ui.TextPanel()
6874
line.maximumSize = JBDimension(480, 1)
6975
line.minimumSize = JBDimension(480, 1)
70-
line.background = java.awt.Color.GRAY
76+
line.background = Color.GRAY
7177

7278
add(line)
7379

@@ -102,17 +108,17 @@ class PropertyPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean)
102108
/**
103109
* Comment Config Panel
104110
*/
105-
class CommentConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
111+
class CommentConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
106112

107-
constructor(layout: java.awt.LayoutManager?) : this(layout, false)
113+
constructor(layout: LayoutManager?) : this(layout, false)
108114

109-
constructor(isDoubleBuffered: Boolean) : this(java.awt.FlowLayout(), isDoubleBuffered)
115+
constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered)
110116

111117
init {
112118

113119
val bordWidth = JBUI.scale(10)
114120
border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth)
115-
setLayout(java.awt.GridLayout(6, 1, 10, 10))
121+
setLayout(GridLayout(6, 1, 10, 10))
116122

117123
val radioButtonOpen = JRadioButton("On")
118124

@@ -144,11 +150,11 @@ class CommentConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Bool
144150
/**
145151
* Target JsonLib ConfigPanel container
146152
*/
147-
class TargetJsonLibConfigPanelContainer(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
153+
class TargetJsonLibConfigPanelContainer(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) {
148154

149-
constructor(layout: java.awt.LayoutManager?) : this(layout, false)
155+
constructor(layout: LayoutManager?) : this(layout, false)
150156

151-
constructor(isDoubleBuffered: Boolean) : this(java.awt.FlowLayout(), isDoubleBuffered)
157+
constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered)
152158

153159
init {
154160
val boxLayout = BoxLayout(this, BoxLayout.PAGE_AXIS)
@@ -164,37 +170,37 @@ class TargetJsonLibConfigPanelContainer(layout: java.awt.LayoutManager?, isDoubl
164170
subBoxPanel.layout = subBoxLayout
165171
val annotationStringPanel = JPanel(true)
166172
annotationStringPanel.maximumSize = JBDimension(480, 30)
167-
annotationStringPanel.layout = java.awt.FlowLayout(java.awt.FlowLayout.LEFT)
173+
annotationStringPanel.layout = FlowLayout(FlowLayout.LEFT)
168174
annotationStringPanel.add(JBLabel("Property Annotation Format: "))
169175
val annotationFormatField = JTextField(ConfigManager.customAnnotaionFormatString)
170176
val fieldDefaultFont = annotationFormatField.font
171-
annotationFormatField.addFocusListener(object : java.awt.event.FocusListener {
172-
override fun focusLost(e: java.awt.event.FocusEvent?) {
177+
annotationFormatField.addFocusListener(object : FocusListener {
178+
override fun focusLost(e: FocusEvent?) {
173179
ConfigManager.customAnnotaionFormatString = annotationFormatField.text
174180
}
175181

176-
override fun focusGained(e: java.awt.event.FocusEvent?) {
182+
override fun focusGained(e: FocusEvent?) {
177183
}
178184

179185
})
180-
annotationStringPanel.add(annotationFormatField, java.awt.FlowLayout.CENTER)
186+
annotationStringPanel.add(annotationFormatField, FlowLayout.CENTER)
181187
subBoxPanel.addComponentIntoVerticalBoxAlignmentLeft(annotationStringPanel)
182188

183189
val annotationImportClass = JPanel(true)
184-
annotationImportClass.layout = java.awt.FlowLayout(java.awt.FlowLayout.LEFT)
190+
annotationImportClass.layout = FlowLayout(FlowLayout.LEFT)
185191
val importClassLable = JBLabel("Property Annotation Import Class : ")
186-
importClassLable.border = JBEmptyBorder(3,0,3,0)
192+
importClassLable.border = JBEmptyBorder(3, 0, 3, 0)
187193
annotationImportClass.add(importClassLable)
188194
val annotationImportClassTextArea = JTextArea(ConfigManager.customAnnotaionImportClassString)
189195
annotationImportClassTextArea.font = fieldDefaultFont
190196
annotationImportClassTextArea.preferredSize = JBDimension(480, 30)
191-
annotationImportClassTextArea.addFocusListener(object : java.awt.event.FocusListener {
192-
override fun focusLost(e: java.awt.event.FocusEvent?) {
197+
annotationImportClassTextArea.addFocusListener(object : FocusListener {
198+
override fun focusLost(e: FocusEvent?) {
193199
ConfigManager.customAnnotaionImportClassString = annotationImportClassTextArea.text
194200

195201
}
196202

197-
override fun focusGained(e: java.awt.event.FocusEvent?) {
203+
override fun focusGained(e: FocusEvent?) {
198204
}
199205

200206
})
@@ -217,16 +223,17 @@ class TargetJsonLibConfigPanelContainer(layout: java.awt.LayoutManager?, isDoubl
217223
/**
218224
* Target JsonLib ConfigPanel
219225
*/
220-
class TargetJsonLibConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean, callBack: (selected: Boolean) -> Unit) : JPanel(layout, isDoubleBuffered) {
226+
class TargetJsonLibConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean, callBack: (selected: Boolean) -> Unit) : JPanel(layout, isDoubleBuffered) {
221227

222-
constructor(layout: java.awt.LayoutManager?, callBack: (selected: Boolean) -> Unit) : this(layout, false, callBack)
228+
constructor(layout: LayoutManager?, callBack: (selected: Boolean) -> Unit) : this(layout, false, callBack)
223229

224-
constructor(isDoubleBuffered: Boolean, callBack: (selected: Boolean) -> Unit) : this(java.awt.FlowLayout(), isDoubleBuffered, callBack)
230+
constructor(isDoubleBuffered: Boolean, callBack: (selected: Boolean) -> Unit) : this(FlowLayout(), isDoubleBuffered, callBack)
225231

226232
init {
227-
setLayout(java.awt.GridLayout(4, 2, 10, 10))
233+
setLayout(GridLayout(4, 2, 10, 10))
228234

229235
val radioButtonNone = JRadioButton("None")
236+
val radioButtonNoneWithCamelCase = JRadioButton("None (Camel Case)")
230237
val radioButtonGson = JRadioButton("Gson")
231238
val radioButtonJackson = JRadioButton("Jackson")
232239
val radioButtonFastjson = JRadioButton("Fastjson")
@@ -238,6 +245,11 @@ class TargetJsonLibConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered
238245
ConfigManager.targetJsonConverterLib = TargetJsonConverter.None
239246
callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom)
240247

248+
}
249+
radioButtonNoneWithCamelCase.addActionListener {
250+
ConfigManager.targetJsonConverterLib = TargetJsonConverter.NoneWithCamelCase
251+
callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom)
252+
241253
}
242254
radioButtonGson.addActionListener {
243255
ConfigManager.targetJsonConverterLib = TargetJsonConverter.Gson
@@ -271,6 +283,10 @@ class TargetJsonLibConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered
271283

272284
radioButtonNone.isSelected = true
273285

286+
} else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.NoneWithCamelCase) {
287+
288+
radioButtonNoneWithCamelCase.isSelected = true
289+
274290
} else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Gson) {
275291

276292
radioButtonGson.isSelected = true
@@ -295,6 +311,7 @@ class TargetJsonLibConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered
295311

296312
val buttonGroupProperty = ButtonGroup()
297313
buttonGroupProperty.add(radioButtonNone)
314+
buttonGroupProperty.add(radioButtonNoneWithCamelCase)
298315
buttonGroupProperty.add(radioButtonGson)
299316
buttonGroupProperty.add(radioButtonJackson)
300317
buttonGroupProperty.add(radioButtonFastjson)
@@ -303,6 +320,7 @@ class TargetJsonLibConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered
303320
buttonGroupProperty.add(radioButtonCustom)
304321

305322
add(radioButtonNone)
323+
add(radioButtonNoneWithCamelCase)
306324
add(radioButtonGson)
307325
add(radioButtonJackson)
308326
add(radioButtonFastjson)

test/wu/seal/jsontokotlin/KPropertyTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class KPropertyTest {
2828

2929
when(ConfigManager.targetJsonConverterLib){
3030
TargetJsonConverter.None-> assert(propertyStringBlock.contains("@").not())
31+
TargetJsonConverter.NoneWithCamelCase-> assert(propertyStringBlock.contains("@").not())
3132
TargetJsonConverter.Gson-> assert(propertyStringBlock.contains("@SerializedName"))
3233
TargetJsonConverter.Jackson-> assert(propertyStringBlock.contains("@JsonProperty"))
3334
TargetJsonConverter.FastJson-> assert(propertyStringBlock.contains("@JSONField(name"))

0 commit comments

Comments
 (0)