From f310716869f889c4956637eb79c14a7cc6aeb030 Mon Sep 17 00:00:00 2001 From: seal Date: Wed, 1 Nov 2017 21:13:48 +0800 Subject: [PATCH] 1.support for LoganSquare and Moshi. 2.support for cutomize annotation. 3.simply usage steps. --- resources/META-INF/plugin.xml | 23 +- src/wu/seal/jsontokotlin/ConfigManager.kt | 46 ++- src/wu/seal/jsontokotlin/ImportClassWriter.kt | 43 ++- src/wu/seal/jsontokotlin/JsonInputDialog.kt | 82 ----- .../jsontokotlin/JsonToKotlinApplication.kt | 2 +- src/wu/seal/jsontokotlin/KPropertyKeyword.kt | 22 -- src/wu/seal/jsontokotlin/KotlinMaker.kt | 5 + .../jsontokotlin/MakeKotlinClassAction.kt | 13 +- src/wu/seal/jsontokotlin/Panel.kt | 205 ------------ src/wu/seal/jsontokotlin/TypeHelper.kt | 1 + .../{ => codeelements}/DefaultValue.kt | 3 +- .../codeelements/KClassAnnotation.kt | 28 ++ .../{ => codeelements}/KClassName.kt | 20 +- .../jsontokotlin/{ => codeelements}/KName.kt | 4 +- .../{ => codeelements}/KProperty.kt | 26 +- .../codeelements/KPropertyKeyword.kt | 22 ++ .../{ => codeelements}/KPropertyName.kt | 20 +- .../supporter/CustomJsonLibSupporter.kt | 46 +++ .../{ => supporter}/FastjsonSupporter.kt | 21 +- .../{ => supporter}/GsonSupporter.kt | 20 +- .../{ => supporter}/IJsonLibSupporter.kt | 3 +- .../{ => supporter}/JacksonSupporter.kt | 18 +- .../supporter/LoganSquareSupporter.kt | 49 +++ .../jsontokotlin/supporter/MoShiSupporter.kt | 46 +++ .../{ => supporter}/NoneSupporter.kt | 9 +- .../{ => ui}/ConfigSettingDialog.kt | 4 +- .../seal/jsontokotlin/ui/JsonInputDialog.kt | 142 ++++++++ src/wu/seal/jsontokotlin/ui/Panel.kt | 315 ++++++++++++++++++ .../CustomJsonLibSupporterTest.kt | 30 ++ .../jsontokotlin/FastjsonSupporterTest.kt | 3 +- test/wu/seal/jsontokotlin/KPropertyTest.kt | 44 +++ .../jsontokotlin/LoganSquareSupporterTest.kt | 41 +++ .../seal/jsontokotlin/MoShiSupporterTest.kt | 32 ++ 33 files changed, 975 insertions(+), 413 deletions(-) delete mode 100644 src/wu/seal/jsontokotlin/JsonInputDialog.kt delete mode 100644 src/wu/seal/jsontokotlin/KPropertyKeyword.kt delete mode 100644 src/wu/seal/jsontokotlin/Panel.kt rename src/wu/seal/jsontokotlin/{ => codeelements}/DefaultValue.kt (93%) create mode 100644 src/wu/seal/jsontokotlin/codeelements/KClassAnnotation.kt rename src/wu/seal/jsontokotlin/{ => codeelements}/KClassName.kt (53%) rename src/wu/seal/jsontokotlin/{ => codeelements}/KName.kt (94%) rename src/wu/seal/jsontokotlin/{ => codeelements}/KProperty.kt (63%) create mode 100644 src/wu/seal/jsontokotlin/codeelements/KPropertyKeyword.kt rename src/wu/seal/jsontokotlin/{ => codeelements}/KPropertyName.kt (62%) create mode 100644 src/wu/seal/jsontokotlin/supporter/CustomJsonLibSupporter.kt rename src/wu/seal/jsontokotlin/{ => supporter}/FastjsonSupporter.kt (63%) rename src/wu/seal/jsontokotlin/{ => supporter}/GsonSupporter.kt (60%) rename src/wu/seal/jsontokotlin/{ => supporter}/IJsonLibSupporter.kt (68%) rename src/wu/seal/jsontokotlin/{ => supporter}/JacksonSupporter.kt (60%) create mode 100644 src/wu/seal/jsontokotlin/supporter/LoganSquareSupporter.kt create mode 100644 src/wu/seal/jsontokotlin/supporter/MoShiSupporter.kt rename src/wu/seal/jsontokotlin/{ => supporter}/NoneSupporter.kt (79%) rename src/wu/seal/jsontokotlin/{ => ui}/ConfigSettingDialog.kt (96%) create mode 100644 src/wu/seal/jsontokotlin/ui/JsonInputDialog.kt create mode 100644 src/wu/seal/jsontokotlin/ui/Panel.kt create mode 100644 test/wu/seal/jsontokotlin/CustomJsonLibSupporterTest.kt create mode 100644 test/wu/seal/jsontokotlin/KPropertyTest.kt create mode 100644 test/wu/seal/jsontokotlin/LoganSquareSupporterTest.kt create mode 100644 test/wu/seal/jsontokotlin/MoShiSupporterTest.kt diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index b5b1f8e2..86e25ae7 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -1,7 +1,7 @@ wu.seal.tool.jsontokotlin JsonToKotlinClass - 1.4.1 + 1.5 Seal - * Fix an issue about Fastjson property name [#ISSUE9](https://github.com/wuseal/JsonToKotlinClass/issues/9)
- * Fix an issue about using in none network condition it will be stuck [#ISSUE10](https://github.com/wuseal/JsonToKotlinClass/issues/10)
- 1.4:
- * Add supporter for Jackson annotation generate,supporter json lib --Jackson.
- * Add supporter for Fastjson annotation generate,supporter json lib --Fastjson.
- * Beautify the config settings dialog.
- 1.3:
- * add property init with default value option in property tab.
- * add property could be nullable option in property tab.
- * fix a bug when the property name is 'list' and it's type is array then the plugin will broken.
- * beautify dialog layout.
- 1.2.1:
- * Fix insert improt class code upon package declare
- * Format property name and class name to camelcase name
- 1.2:
- * Add support for generate anotations for target json lib --gson
- * Add Comment option to switch the comment content to append
+ 1.support for LoganSquare and Moshi.
+ 2.support for cutomize annotation.
+ 3.simply usage steps. ]]>
diff --git a/src/wu/seal/jsontokotlin/ConfigManager.kt b/src/wu/seal/jsontokotlin/ConfigManager.kt index 1848624b..ebecf05c 100644 --- a/src/wu/seal/jsontokotlin/ConfigManager.kt +++ b/src/wu/seal/jsontokotlin/ConfigManager.kt @@ -2,6 +2,7 @@ package wu.seal.jsontokotlin import com.intellij.ide.util.PropertiesComponent import wu.seal.jsontokotlin.statistics.sendConfigInfo +import wu.seal.jsontokotlin.supporter.GsonSupporter /** * ConfigManager @@ -31,12 +32,18 @@ interface IConfigManager { get() = "jsonToKotlin_user_uuid_value_key" + private val USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS: String + get() = "jsonToKotlin_user_custom_json_lib_annotation_import_class" + + private val USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING: String + get() = "jsontokotlin_user_custom_json_lib_annotation_format_string" + var isPropertiesVar: Boolean get() = if (isTestModel) TestConfig.isPropertiesVar else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTIES_VAR_KEY) set(value) = if (isTestModel) { } else { PropertiesComponent.getInstance().setValue(IS_PROPERTIES_VAR_KEY, value) - Thread(){ + Thread() { sendConfigInfo() }.start() } @@ -47,7 +54,7 @@ interface IConfigManager { set(value) = if (isTestModel) { } else { PropertiesComponent.getInstance().setValue(IS_COMMENT_OFF, value) - Thread(){ + Thread() { sendConfigInfo() }.start() } @@ -56,9 +63,10 @@ interface IConfigManager { var targetJsonConverterLib: TargetJsonConverter get() = if (isTestModel) TestConfig.targetJsonConvertLib else TargetJsonConverter.valueOf(PropertiesComponent.getInstance().getValue(TARGET_JSON_CONVERTER_LIB_KEY) ?: TargetJsonConverter.None.name) set(value) = if (isTestModel) { + TestConfig.targetJsonConvertLib = value } else { PropertiesComponent.getInstance().setValue(TARGET_JSON_CONVERTER_LIB_KEY, value.name) - Thread(){ + Thread() { sendConfigInfo() }.start() } @@ -68,7 +76,7 @@ interface IConfigManager { set(value) = if (isTestModel) { } else { PropertiesComponent.getInstance().setValue(IS_PROPERTY_NULLABLE_KEY, value) - Thread(){ + Thread() { sendConfigInfo() }.start() } @@ -77,9 +85,9 @@ interface IConfigManager { var initWithDefaultValue: Boolean get() = if (isTestModel) TestConfig.initWithDefaultValue else PropertiesComponent.getInstance().getBoolean(INIT_WITH_DEFAULT_VALUE_KEY) set(value) = if (isTestModel) { - } else{ + } else { PropertiesComponent.getInstance().setValue(INIT_WITH_DEFAULT_VALUE_KEY, value) - Thread(){ + Thread() { sendConfigInfo() }.start() } @@ -89,7 +97,27 @@ interface IConfigManager { set(value) = if (isTestModel) { } else { PropertiesComponent.getInstance().setValue(USER_UUID_KEY, value) - Thread(){ + Thread() { + sendConfigInfo() + }.start() + } + + var customAnnotaionImportClassString:String + get() = if (isTestModel) GsonSupporter.annotationImportClassString else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, GsonSupporter.annotationImportClassString) + set(value) = if (isTestModel) { + } else { + PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, value) + Thread() { + sendConfigInfo() + }.start() + } + + var customAnnotaionFormatString:String + get() = if (isTestModel) GsonSupporter.anotaionOnProperty else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, GsonSupporter.anotaionOnProperty) + set(value) = if (isTestModel) { + } else { + PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, value) + Thread() { sendConfigInfo() }.start() } @@ -99,7 +127,7 @@ interface IConfigManager { * This means which Json convert library you are using in you project */ enum class TargetJsonConverter { - None, Gson, FastJson, Jackson + None, Gson, FastJson, Jackson, MoShi, LoganSquare, Custom } @@ -116,7 +144,7 @@ var isTestModel = false object TestConfig { var isCommentOff = false var isPropertiesVar = false - var targetJsonConvertLib = TargetJsonConverter.None + var targetJsonConvertLib = TargetJsonConverter.Gson var isPropertyNullable = true var initWithDefaultValue = true } \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/ImportClassWriter.kt b/src/wu/seal/jsontokotlin/ImportClassWriter.kt index 2926479b..dcf811af 100644 --- a/src/wu/seal/jsontokotlin/ImportClassWriter.kt +++ b/src/wu/seal/jsontokotlin/ImportClassWriter.kt @@ -2,6 +2,7 @@ package wu.seal.jsontokotlin import com.intellij.openapi.editor.Document import com.intellij.openapi.project.Project +import wu.seal.jsontokotlin.supporter.* /** * to be a helper to insert Import class declare code @@ -22,11 +23,32 @@ interface IImportClassWriter { fun insertImportClassCode(project: Project?, editFile: Document) + fun insertMoShiImportClass(project: Project?, editFile: Document) + fun insertLoganSquareImportClass(project: Project?, editFile: Document) + + fun insertCustomImportClass(project: Project?, editFile: Document) } object ImportClassWriter : IImportClassWriter { + + + override fun insertCustomImportClass(project: Project?, editFile: Document) { + val importClassString = CustomJsonLibSupporter.annotationImportClassString + insertImportClassString(editFile, importClassString, project) + } + + override fun insertMoShiImportClass(project: Project?, editFile: Document) { + val importClassString = MoShiSupporter.annotationImportClassString + insertImportClassString(editFile, importClassString, project) + } + + override fun insertLoganSquareImportClass(project: Project?, editFile: Document) { + val importClassString = LoganSquareSupporter.annotationImportClassString + insertImportClassString(editFile, importClassString, project) + } + override fun insertImportClassCode(project: Project?, editFile: Document) { when (ConfigManager.targetJsonConverterLib) { @@ -34,6 +56,9 @@ object ImportClassWriter : IImportClassWriter { TargetJsonConverter.Gson -> insertGsonImportClass(project, editFile) TargetJsonConverter.FastJson -> insertFastJsonImportClass(project, editFile) TargetJsonConverter.Jackson -> insertJackSonImportClass(project, editFile) + TargetJsonConverter.MoShi -> insertMoShiImportClass(project, editFile) + TargetJsonConverter.LoganSquare -> insertLoganSquareImportClass(project, editFile) + TargetJsonConverter.Custom -> insertCustomImportClass(project, editFile) else -> { println("No need to import any Class code") @@ -59,18 +84,22 @@ object ImportClassWriter : IImportClassWriter { } private fun insertImportClassString(editFile: Document, importClassString: String, project: Project?) { + val text = editFile.text - if (importClassString !in text) { + importClassString.split("\n").forEach { importClassLineString -> - val packageIndex = text.indexOf("package ") - val importIndex = Math.max(text.lastIndexOf("import"), packageIndex) - val insertIndex = if (importIndex == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(importIndex)) + if (importClassLineString !in text) { + val packageIndex = text.indexOf("package ") + val importIndex = Math.max(text.lastIndexOf("import"), packageIndex) + val insertIndex = if (importIndex == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(importIndex)) - executeCouldRollBackAction(project) { - editFile.insertString(insertIndex, "\n" + importClassString + "\n") - } + executeCouldRollBackAction(project) { + editFile.insertString(insertIndex, "\n" + importClassLineString + "\n") + } + + } } } diff --git a/src/wu/seal/jsontokotlin/JsonInputDialog.kt b/src/wu/seal/jsontokotlin/JsonInputDialog.kt deleted file mode 100644 index c5d3561b..00000000 --- a/src/wu/seal/jsontokotlin/JsonInputDialog.kt +++ /dev/null @@ -1,82 +0,0 @@ -package wu.seal.jsontokotlin - -import com.google.gson.JsonParser -import com.google.gson.JsonSyntaxException -import com.intellij.openapi.project.Project -import com.intellij.openapi.ui.InputValidator -import com.intellij.openapi.ui.Messages -import com.intellij.ui.components.JBScrollPane -import com.intellij.util.ui.JBDimension -import java.awt.BorderLayout -import java.awt.event.ActionEvent -import javax.swing.* -import javax.swing.text.JTextComponent - -/** - * Dialog widget relative - * Created by Seal.wu on 2017/9/21. - */ - - -class MyInputValidator : InputValidator { - override fun checkInput(inputString: String): Boolean { - try { - val jsonElement = JsonParser().parse(inputString) - - return jsonElement.isJsonObject || jsonElement.isJsonArray - } catch (e: JsonSyntaxException) { - return false - } - - } - - override fun canClose(inputString: String): Boolean { - return true - } -} - -/** - * Json input Dialog - */ -class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please input the Json Data", "Input Json", Messages.getInformationIcon(), "", MyInputValidator()) { - - override fun createMessagePanel(): JPanel { - val messagePanel = JPanel(BorderLayout()) - if (myMessage != null) { - val textComponent = createTextComponent() - messagePanel.add(textComponent, BorderLayout.NORTH) - } - - myField = createTextFieldComponent() - - messagePanel.add(createScrollableTextComponent(), BorderLayout.CENTER) - val settingButton = JButton("Config Settings") - settingButton.addActionListener(object : AbstractAction() { - override fun actionPerformed(e: ActionEvent) { - ConfigSettingDialog(false).show() - } - }) - val settingContainer = JPanel() - val boxLayout = BoxLayout(settingContainer, BoxLayout.LINE_AXIS) - settingContainer.layout = boxLayout - settingButton.horizontalAlignment = SwingConstants.RIGHT - settingContainer.add(settingButton) - messagePanel.add(settingContainer, BorderLayout.SOUTH) - return messagePanel - } - - override fun createTextFieldComponent(): JTextComponent { - val jTextArea = JTextArea(15, 100) - jTextArea.minimumSize = JBDimension(800, 500) - jTextArea.maximumSize = JBDimension(1000, 700) - jTextArea.lineWrap = true - jTextArea.wrapStyleWord = true - jTextArea.autoscrolls = true - return jTextArea - } - - - protected fun createScrollableTextComponent(): JComponent { - return JBScrollPane(myField) - } -} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt b/src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt index ceda10b9..bd0e06b6 100644 --- a/src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt +++ b/src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt @@ -10,7 +10,7 @@ import wu.seal.jsontokotlin.statistics.sendHistoryActionInfo * Created by Seal.wu on 2017/8/21. */ -const val PLUGIN_VERSION = "1.4.1" +const val PLUGIN_VERSION = "1.5" class JsonToKotlinApplication : ApplicationComponent { diff --git a/src/wu/seal/jsontokotlin/KPropertyKeyword.kt b/src/wu/seal/jsontokotlin/KPropertyKeyword.kt deleted file mode 100644 index 8e9148cd..00000000 --- a/src/wu/seal/jsontokotlin/KPropertyKeyword.kt +++ /dev/null @@ -1,22 +0,0 @@ -package wu.seal.jsontokotlin - -/** - * keyword relative - * Created by Seal.Wu on 2017/9/13. - */ - -interface IPropertyKeyword { - - val varProperty: String - get() = "var" - val valProperty: String - get() = "val" - - fun get(): String - -} - -object PropertyKeyword : IPropertyKeyword { - override fun get() = if (ConfigManager.isPropertiesVar) varProperty else valProperty - -} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/KotlinMaker.kt b/src/wu/seal/jsontokotlin/KotlinMaker.kt index 207782de..9781530c 100644 --- a/src/wu/seal/jsontokotlin/KotlinMaker.kt +++ b/src/wu/seal/jsontokotlin/KotlinMaker.kt @@ -1,6 +1,8 @@ package wu.seal.jsontokotlin import com.google.gson.* +import wu.seal.jsontokotlin.codeelements.KClassAnnotation +import wu.seal.jsontokotlin.codeelements.KProperty import java.util.HashSet @@ -50,6 +52,9 @@ class KotlinMaker { } private fun appClassName(stringBuilder: StringBuilder) { + val classAnnotation = KClassAnnotation.getClassAnnotation() + stringBuilder.append(classAnnotation) + if (classAnnotation.isNotBlank()) stringBuilder.append("\n") stringBuilder.append("data class ").append(className).append("(\n") } diff --git a/src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt b/src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt index f534e0d9..a7508c86 100644 --- a/src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt +++ b/src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt @@ -12,6 +12,7 @@ import wu.seal.jsontokotlin.statistics.StartAction import wu.seal.jsontokotlin.statistics.SuccessCompleteAction import wu.seal.jsontokotlin.statistics.getUncaughtExceptionHandler import wu.seal.jsontokotlin.statistics.sendActionInfo +import wu.seal.jsontokotlin.ui.JsonInputDialog import java.util.IllegalFormatFlagsException @@ -36,12 +37,10 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") { Messages.showWarningDialog("Please open a file in editor state for insert Kotlin code!", "No Editor File") return } - val className = Messages.showInputDialog(project, "Please input the Class Name for Insert", "Input ClassName", Messages.getInformationIcon()) - if (className == null || className.isEmpty()) { - return - } + val inputDialog = JsonInputDialog(project!!) inputDialog.show() + val className = inputDialog.getClassName() val json = inputDialog.inputString if (json == null || json.isEmpty()) { return @@ -74,14 +73,16 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") { document.insertString(Math.max(offset, 0), maker.makeKotlinData()) } - Messages.showMessageDialog(project, "Kotlin Code insert successfully!", "Information", Messages.getInformationIcon()) +// Messages.showMessageDialog(project, "Kotlin Code insert successfully!", "Information", Messages.getInformationIcon()) Thread { sendActionInfo(gson.toJson(SuccessCompleteAction())) }.start() } catch(e: Exception) { getUncaughtExceptionHandler(jsonString) { - Messages.showErrorDialog("I am sorry,JsonToKotlin may occur a RuntimeException,You could try again later or recover to the old version", "Occur a fatal error") + Messages.showErrorDialog("I am sorry,JsonToKotlinClass may occur a RuntimeException,\nYou could try again later or recover to the old version,\nOr you could post an issue here:\nhttps://github.com/wuseal/JsonToKotlinClass", "Occur a fatal error") }.uncaughtException(Thread.currentThread(), e) + + throw e } } } diff --git a/src/wu/seal/jsontokotlin/Panel.kt b/src/wu/seal/jsontokotlin/Panel.kt deleted file mode 100644 index d0478900..00000000 --- a/src/wu/seal/jsontokotlin/Panel.kt +++ /dev/null @@ -1,205 +0,0 @@ -package wu.seal.jsontokotlin - -import com.intellij.util.ui.JBDimension -import com.intellij.util.ui.JBUI -import com.intellij.util.xml.ui.TextPanel -import java.awt.* -import javax.swing.* -import javax.swing.border.EmptyBorder - -/** - * todo //beautify interface - * property config panel - * Created by seal wu on 2017/9/18. - */ -class PropertyPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) { - - constructor(layout: LayoutManager?) : this(layout, false) - - constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered) - - - init { - - - val boxLayout = BoxLayout(this, BoxLayout.PAGE_AXIS) - setLayout(boxLayout) - val bordWidth = JBUI.scale(10) - border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth) - - val keywordLable = JLabel("Keyword") - - val radioButtonVal = JRadioButton("Val") - - radioButtonVal.addActionListener { - ConfigManager.isPropertiesVar = false - } - val radioButtonVar = JRadioButton("Var") - - radioButtonVar.addActionListener { - ConfigManager.isPropertiesVar = true - } - - if (ConfigManager.isPropertiesVar) { - - radioButtonVar.isSelected = true - } else { - radioButtonVal.isSelected = true - } - val buttonGroupProperty = ButtonGroup() - buttonGroupProperty.add(radioButtonVal) - buttonGroupProperty.add(radioButtonVar) - - addComponentIntoVerticalBoxAlignmentLeft(keywordLable) - add(Box.createVerticalStrut(20)) - addComponentIntoVerticalBoxAlignmentLeft(radioButtonVal) - add(Box.createVerticalStrut(20)) - - addComponentIntoVerticalBoxAlignmentLeft(radioButtonVar) - - add(Box.createVerticalStrut(20)) - - - val line = TextPanel() - line.maximumSize = JBDimension(480, 1) - line.minimumSize = JBDimension(480, 1) - line.background = Color.GRAY - - add(line) - - val nullAbleCheck = JCheckBox("Property type be Nullable(?)") - if (ConfigManager.isPropertyNullable) { - nullAbleCheck.isSelected = true - } - - val initWithDefaultValueCheck = JCheckBox("Init with default value (avoid null)") - initWithDefaultValueCheck.isSelected = ConfigManager.initWithDefaultValue - - initWithDefaultValueCheck.addActionListener { - ConfigManager.initWithDefaultValue = initWithDefaultValueCheck.isSelected - } - - nullAbleCheck.addActionListener { - ConfigManager.isPropertyNullable = nullAbleCheck.isSelected - } - - add(Box.createVerticalStrut(20)) - - addComponentIntoVerticalBoxAlignmentLeft(initWithDefaultValueCheck) - add(Box.createVerticalStrut(20)) - - addComponentIntoVerticalBoxAlignmentLeft(nullAbleCheck) - - - } -} - - -/** - * Comment Config Panel - */ -class CommentConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) { - - constructor(layout: LayoutManager?) : this(layout, false) - - constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered) - - init { - - val bordWidth = JBUI.scale(10) - border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth) - setLayout(GridLayout(6, 1, 10, 10)) - - val radioButtonOpen = JRadioButton("On") - - radioButtonOpen.addActionListener { - ConfigManager.isCommentOff = false - } - val radioButtonOff = JRadioButton("Off") - radioButtonOff.addActionListener { - ConfigManager.isCommentOff = true - } - - if (ConfigManager.isCommentOff) { - - radioButtonOff.isSelected = true - } else { - radioButtonOpen.isSelected = true - } - - val buttonGroupProperty = ButtonGroup() - buttonGroupProperty.add(radioButtonOpen) - buttonGroupProperty.add(radioButtonOff) - - add(radioButtonOpen) - add(radioButtonOff) - } - -} - -/** - * Target JsonLib ConfigPanel - */ -class TargetJsonLibConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) { - - constructor(layout: LayoutManager?) : this(layout, false) - - constructor(isDoubleBuffered: Boolean) : this(FlowLayout(), isDoubleBuffered) - - init { - - val bordWidth = JBUI.scale(10) - border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth) - - setLayout(GridLayout(6, 1, 10, 10)) - - val radioButtonNone = JRadioButton("None") - val radioButtonGson = JRadioButton("Gson") - val radioButtonJackson = JRadioButton("Jackson") - val radioButtonFastjson = JRadioButton("Fastjson") - - radioButtonNone.addActionListener { - ConfigManager.targetJsonConverterLib = TargetJsonConverter.None - } - radioButtonGson.addActionListener { - ConfigManager.targetJsonConverterLib = TargetJsonConverter.Gson - } - radioButtonJackson.addActionListener { - - ConfigManager.targetJsonConverterLib = TargetJsonConverter.Jackson - } - radioButtonFastjson.addActionListener { - - ConfigManager.targetJsonConverterLib = TargetJsonConverter.FastJson - } - - if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.None) { - - radioButtonNone.isSelected = true - - } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Gson) { - - radioButtonGson.isSelected = true - - } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Jackson) { - - radioButtonJackson.isSelected = true - } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.FastJson) { - - radioButtonFastjson.isSelected = true - } - - val buttonGroupProperty = ButtonGroup() - buttonGroupProperty.add(radioButtonNone) - buttonGroupProperty.add(radioButtonGson) - buttonGroupProperty.add(radioButtonJackson) - buttonGroupProperty.add(radioButtonFastjson) - - add(radioButtonNone) - add(radioButtonGson) - add(radioButtonJackson) - add(radioButtonFastjson) - } - -} - diff --git a/src/wu/seal/jsontokotlin/TypeHelper.kt b/src/wu/seal/jsontokotlin/TypeHelper.kt index 2c208a5d..2b00e05a 100644 --- a/src/wu/seal/jsontokotlin/TypeHelper.kt +++ b/src/wu/seal/jsontokotlin/TypeHelper.kt @@ -2,6 +2,7 @@ package wu.seal.jsontokotlin import com.google.gson.JsonArray import com.google.gson.JsonPrimitive +import wu.seal.jsontokotlin.codeelements.KClassName import java.util.* /** diff --git a/src/wu/seal/jsontokotlin/DefaultValue.kt b/src/wu/seal/jsontokotlin/codeelements/DefaultValue.kt similarity index 93% rename from src/wu/seal/jsontokotlin/DefaultValue.kt rename to src/wu/seal/jsontokotlin/codeelements/DefaultValue.kt index 3e9cd1bb..6497067b 100644 --- a/src/wu/seal/jsontokotlin/DefaultValue.kt +++ b/src/wu/seal/jsontokotlin/codeelements/DefaultValue.kt @@ -1,7 +1,8 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.codeelements import com.google.gson.Gson import com.google.gson.JsonParser +import wu.seal.jsontokotlin.* /** * Default Value relative diff --git a/src/wu/seal/jsontokotlin/codeelements/KClassAnnotation.kt b/src/wu/seal/jsontokotlin/codeelements/KClassAnnotation.kt new file mode 100644 index 00000000..6292c46e --- /dev/null +++ b/src/wu/seal/jsontokotlin/codeelements/KClassAnnotation.kt @@ -0,0 +1,28 @@ +package wu.seal.jsontokotlin.codeelements + +/** + * class annotation for json lib + * Created by Seal.Wu on 2017/11/1. + */ + +/** + * class annotation for json lib + */ +interface IClassAnnotation { + /** + * get the annotation string be in append before the class name + */ + fun getClassAnnotation():String +} + + +object KClassAnnotation : wu.seal.jsontokotlin.codeelements.IClassAnnotation { + override fun getClassAnnotation(): String { + if (wu.seal.jsontokotlin.ConfigManager.targetJsonConverterLib == wu.seal.jsontokotlin.TargetJsonConverter.LoganSquare) { + return wu.seal.jsontokotlin.supporter.LoganSquareSupporter.classAnnotation + } + + return "" + } + +} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/KClassName.kt b/src/wu/seal/jsontokotlin/codeelements/KClassName.kt similarity index 53% rename from src/wu/seal/jsontokotlin/KClassName.kt rename to src/wu/seal/jsontokotlin/codeelements/KClassName.kt index d10e9388..df55aeda 100644 --- a/src/wu/seal/jsontokotlin/KClassName.kt +++ b/src/wu/seal/jsontokotlin/codeelements/KClassName.kt @@ -1,4 +1,4 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.codeelements /** * Transform to legal Class name @@ -15,31 +15,31 @@ fun main(args: Array) { !@3214 12#$%n^&*(-a)_+-=m12335_e43{}|[]\\;':1",./<>?/*-+` """ println("orginal name is |$name1|") - println("Name1 is : |${KClassName.getName(name1)}|") + println("Name1 is : |${wu.seal.jsontokotlin.codeelements.KClassName.getName(name1)}|") } -object KClassName : KName(), IKClassName { +object KClassName : KName(), wu.seal.jsontokotlin.codeelements.IKClassName { override fun getLegalClassName(rawClassName: String): String { - return getUpperCamelCaseLegalName(rawClassName) + return wu.seal.jsontokotlin.codeelements.KClassName.getUpperCamelCaseLegalName(rawClassName) } private fun getUpperCamelCaseLegalName(rawClassName: String): String { /** * keep " " character */ - val pattern = "$illegalCharacter".replace(Regex(nameSeparator.toString()), "") + val pattern = "${wu.seal.jsontokotlin.codeelements.KClassName.illegalCharacter}".replace(Regex(wu.seal.jsontokotlin.codeelements.KClassName.nameSeparator.toString()), "") val temp = rawClassName.replace(Regex(pattern), "").let { - return@let removeStartNumberAndIllegalCharacter(it) + return@let wu.seal.jsontokotlin.codeelements.KClassName.removeStartNumberAndIllegalCharacter(it) } - val upperCamelCase = toUpperCamelCase(temp) + val upperCamelCase = wu.seal.jsontokotlin.codeelements.KClassName.toUpperCamelCase(temp) - val legalName = toBeLegalName(upperCamelCase) + val legalName = wu.seal.jsontokotlin.codeelements.KClassName.toBeLegalName(upperCamelCase) return legalName } @@ -47,7 +47,7 @@ object KClassName : KName(), IKClassName { override fun getName(rawName: String): String { - return getLegalClassName(rawName) + return wu.seal.jsontokotlin.codeelements.KClassName.getLegalClassName(rawName) } @@ -58,7 +58,7 @@ object KClassName : KName(), IKClassName { val stringBuilder = StringBuilder() - temp.split(Regex(nameSeparator.toString())).forEach { + temp.split(Regex(wu.seal.jsontokotlin.codeelements.KClassName.nameSeparator.toString())).forEach { if (it.isNotBlank()) { stringBuilder.append(it.substring(0, 1).toUpperCase().plus(it.substring(1))) } diff --git a/src/wu/seal/jsontokotlin/KName.kt b/src/wu/seal/jsontokotlin/codeelements/KName.kt similarity index 94% rename from src/wu/seal/jsontokotlin/KName.kt rename to src/wu/seal/jsontokotlin/codeelements/KName.kt index b7fed8aa..986dfe07 100644 --- a/src/wu/seal/jsontokotlin/KName.kt +++ b/src/wu/seal/jsontokotlin/codeelements/KName.kt @@ -1,4 +1,4 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.codeelements /** * Name class @@ -12,7 +12,7 @@ interface IKName { } -abstract class KName : IKName { +abstract class KName : wu.seal.jsontokotlin.codeelements.IKName { private val suffix = "X" diff --git a/src/wu/seal/jsontokotlin/KProperty.kt b/src/wu/seal/jsontokotlin/codeelements/KProperty.kt similarity index 63% rename from src/wu/seal/jsontokotlin/KProperty.kt rename to src/wu/seal/jsontokotlin/codeelements/KProperty.kt index 3548cbcd..5c00373a 100644 --- a/src/wu/seal/jsontokotlin/KProperty.kt +++ b/src/wu/seal/jsontokotlin/codeelements/KProperty.kt @@ -1,4 +1,8 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.codeelements + +import wu.seal.jsontokotlin.ConfigManager +import wu.seal.jsontokotlin.TargetJsonConverter +import wu.seal.jsontokotlin.supporter.* /** * Created by Seal.Wu on 2017/9/18. @@ -12,14 +16,6 @@ interface IProperty { } -fun main(args: Array) { - isTestModel = true - - val property = KProperty("seal is a *() good_man", "Boolean", "true") - - println("getPropertyStringBlock:\n${property.getPropertyStringBlock()}") -} - class KProperty(private val rawPropertyName: String, private val propertyType: String, private val propertyValue: String) : IProperty { @@ -45,6 +41,18 @@ class KProperty(private val rawPropertyName: String, private val propertyType: S blockBulder.append(FastjsonSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, propertyType)) + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.MoShi) { + + blockBulder.append(MoShiSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, propertyType)) + + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.LoganSquare) { + + blockBulder.append(LoganSquareSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, propertyType)) + + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) { + + blockBulder.append(CustomJsonLibSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, propertyType)) + } if (!ConfigManager.isCommentOff && propertyValue.isNotBlank()) { diff --git a/src/wu/seal/jsontokotlin/codeelements/KPropertyKeyword.kt b/src/wu/seal/jsontokotlin/codeelements/KPropertyKeyword.kt new file mode 100644 index 00000000..0fb6c478 --- /dev/null +++ b/src/wu/seal/jsontokotlin/codeelements/KPropertyKeyword.kt @@ -0,0 +1,22 @@ +package wu.seal.jsontokotlin.codeelements + +/** + * keyword relative + * Created by Seal.Wu on 2017/9/13. + */ + +interface IPropertyKeyword { + + val varProperty: String + get() = "var" + val valProperty: String + get() = "val" + + fun get(): String + +} + +object KPropertyKeyword : wu.seal.jsontokotlin.codeelements.IPropertyKeyword { + override fun get() = if (wu.seal.jsontokotlin.ConfigManager.isPropertiesVar) wu.seal.jsontokotlin.codeelements.KPropertyKeyword.varProperty else wu.seal.jsontokotlin.codeelements.KPropertyKeyword.valProperty + +} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/KPropertyName.kt b/src/wu/seal/jsontokotlin/codeelements/KPropertyName.kt similarity index 62% rename from src/wu/seal/jsontokotlin/KPropertyName.kt rename to src/wu/seal/jsontokotlin/codeelements/KPropertyName.kt index 98a8dda2..9bab57ca 100644 --- a/src/wu/seal/jsontokotlin/KPropertyName.kt +++ b/src/wu/seal/jsontokotlin/codeelements/KPropertyName.kt @@ -1,4 +1,4 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.codeelements /** * make name to be camel case @@ -27,16 +27,16 @@ fun main(args: Array) { """ println("orginal name is |$name1|") - println("Name1 is : |${KPropertyName.getName(name1)}|") + println("Name1 is : |${wu.seal.jsontokotlin.codeelements.KPropertyName.getName(name1)}|") } -object KPropertyName : KName(), IPropertyNameMaker { +object KPropertyName : KName(), wu.seal.jsontokotlin.codeelements.IPropertyNameMaker { override fun getName(rawName: String): String { - return makePropertyName(rawName, true) + return wu.seal.jsontokotlin.codeelements.KPropertyName.makePropertyName(rawName, true) } override fun makePropertyName(rawString: String): String { @@ -48,7 +48,7 @@ object KPropertyName : KName(), IPropertyNameMaker { if (needTransformToLegalName) { - return makeCamelCaseLegalName(rawString) + return wu.seal.jsontokotlin.codeelements.KPropertyName.makeCamelCaseLegalName(rawString) } else { return rawString @@ -60,17 +60,17 @@ object KPropertyName : KName(), IPropertyNameMaker { /** * keep nameSeparator character */ - val pattern = "$illegalCharacter".replace(Regex(nameSeparator.toString()), "") + val pattern = "${wu.seal.jsontokotlin.codeelements.KPropertyName.illegalCharacter}".replace(Regex(wu.seal.jsontokotlin.codeelements.KPropertyName.nameSeparator.toString()), "") val temp = rawString.replace(Regex(pattern), "").let { - return@let removeStartNumberAndIllegalCharacter(it) + return@let wu.seal.jsontokotlin.codeelements.KPropertyName.removeStartNumberAndIllegalCharacter(it) } - val lowerCamelCaseName = toLowerCamelCase(temp) + val lowerCamelCaseName = wu.seal.jsontokotlin.codeelements.KPropertyName.toLowerCamelCase(temp) - val legalName = toBeLegalName(lowerCamelCaseName) + val legalName = wu.seal.jsontokotlin.codeelements.KPropertyName.toBeLegalName(lowerCamelCaseName) return legalName } @@ -83,7 +83,7 @@ object KPropertyName : KName(), IPropertyNameMaker { val stringBuilder = StringBuilder() - temp.split(Regex(nameSeparator.toString())).forEach { + temp.split(Regex(wu.seal.jsontokotlin.codeelements.KPropertyName.nameSeparator.toString())).forEach { if (it.isNotBlank()) { stringBuilder.append(it.substring(0, 1).toUpperCase().plus(it.substring(1))) } diff --git a/src/wu/seal/jsontokotlin/supporter/CustomJsonLibSupporter.kt b/src/wu/seal/jsontokotlin/supporter/CustomJsonLibSupporter.kt new file mode 100644 index 00000000..a5954b6e --- /dev/null +++ b/src/wu/seal/jsontokotlin/supporter/CustomJsonLibSupporter.kt @@ -0,0 +1,46 @@ +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.KPropertyName +import wu.seal.jsontokotlin.codeelements.getDefaultValue + +/** + * others json lib supporter by custom + * Created by Seal.Wu on 2017/11/1. + */ + +object CustomJsonLibSupporter : IJsonLibSupporter { + + private val propertyAnnotation = wu.seal.jsontokotlin.ConfigManager.customAnnotaionFormatString + + + override val annotationImportClassString: String + get() = wu.seal.jsontokotlin.ConfigManager.customAnnotaionImportClassString + + override fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String): String { + + val customJsonLibSupportPropertyBuilder = StringBuilder() + + customJsonLibSupportPropertyBuilder.append(wu.seal.jsontokotlin.supporter.CustomJsonLibSupporter.propertyAnnotation.format(rawPropertyName)) + + customJsonLibSupportPropertyBuilder.append(" ") + + customJsonLibSupportPropertyBuilder.append(KPropertyKeyword.get()) + + customJsonLibSupportPropertyBuilder.append(" ") + + customJsonLibSupportPropertyBuilder.append(KPropertyName.getName(rawPropertyName)) + + customJsonLibSupportPropertyBuilder.append(": ") + + customJsonLibSupportPropertyBuilder.append(propertyType) + + if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) { + customJsonLibSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType)) + } + + customJsonLibSupportPropertyBuilder.append(",") + + return customJsonLibSupportPropertyBuilder.toString() + } +} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/FastjsonSupporter.kt b/src/wu/seal/jsontokotlin/supporter/FastjsonSupporter.kt similarity index 63% rename from src/wu/seal/jsontokotlin/FastjsonSupporter.kt rename to src/wu/seal/jsontokotlin/supporter/FastjsonSupporter.kt index 650bc81c..b52362bc 100644 --- a/src/wu/seal/jsontokotlin/FastjsonSupporter.kt +++ b/src/wu/seal/jsontokotlin/supporter/FastjsonSupporter.kt @@ -1,4 +1,8 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.KPropertyName +import wu.seal.jsontokotlin.codeelements.getDefaultValue /** * supporter for alibaba fastjson @@ -6,22 +10,23 @@ package wu.seal.jsontokotlin */ -object FastjsonSupporter:IJsonLibSupporter { +object FastjsonSupporter : IJsonLibSupporter { - internal val annotationImportClassString = "import com.alibaba.fastjson.annotation.JSONField" + override val annotationImportClassString: String + get() = "import com.alibaba.fastjson.annotation.JSONField" - private val propertyAnnotation ="@JSONField(name = \"%s\")" + private val propertyAnnotation = "@JSONField(name = \"%s\")" - override fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String):String { + override fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String): String { val fastjsonSupportPropertyBuilder = StringBuilder() - fastjsonSupportPropertyBuilder.append(propertyAnnotation.format(rawPropertyName)) + fastjsonSupportPropertyBuilder.append(wu.seal.jsontokotlin.supporter.FastjsonSupporter.propertyAnnotation.format(rawPropertyName)) fastjsonSupportPropertyBuilder.append(" ") - fastjsonSupportPropertyBuilder.append(PropertyKeyword.get()) + fastjsonSupportPropertyBuilder.append(KPropertyKeyword.get()) fastjsonSupportPropertyBuilder.append(" ") @@ -34,7 +39,7 @@ object FastjsonSupporter:IJsonLibSupporter { fastjsonSupportPropertyBuilder.append(propertyType) - if (ConfigManager.initWithDefaultValue) { + if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) { fastjsonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType)) } diff --git a/src/wu/seal/jsontokotlin/GsonSupporter.kt b/src/wu/seal/jsontokotlin/supporter/GsonSupporter.kt similarity index 60% rename from src/wu/seal/jsontokotlin/GsonSupporter.kt rename to src/wu/seal/jsontokotlin/supporter/GsonSupporter.kt index 818b5eb9..e58532a4 100644 --- a/src/wu/seal/jsontokotlin/GsonSupporter.kt +++ b/src/wu/seal/jsontokotlin/supporter/GsonSupporter.kt @@ -1,4 +1,8 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.KPropertyName +import wu.seal.jsontokotlin.codeelements.getDefaultValue /** * Gson Support about @@ -18,28 +22,28 @@ interface IGsonSupporter { fun main(args: Array) { - isTestModel = true - println("getGsonSupporterProperty:\n ${GsonSupporter.getGsonSupporterProperty("seal is **() good_man ","Boy")}") + wu.seal.jsontokotlin.isTestModel = true + println("getGsonSupporterProperty:\n ${wu.seal.jsontokotlin.supporter.GsonSupporter.getGsonSupporterProperty("seal is **() good_man ", "Boy")}") } -object GsonSupporter : IGsonSupporter { +object GsonSupporter : wu.seal.jsontokotlin.supporter.IGsonSupporter { /** * When adapter Gson lib at most ,We should import the Anotation Class */ val annotationImportClassString = "import com.google.gson.annotations.SerializedName" - private val anotaionOnProperty = "@SerializedName(\"%s\")" + internal val anotaionOnProperty = "@SerializedName(\"%s\")" override fun getGsonSupporterProperty(rawPropertyName: String, propertyType: String): String { val gsonSupportPropertyBuilder = StringBuilder() - gsonSupportPropertyBuilder.append(anotaionOnProperty.format(rawPropertyName)) + gsonSupportPropertyBuilder.append(wu.seal.jsontokotlin.supporter.GsonSupporter.anotaionOnProperty.format(rawPropertyName)) gsonSupportPropertyBuilder.append(" ") - gsonSupportPropertyBuilder.append(PropertyKeyword.get()) + gsonSupportPropertyBuilder.append(KPropertyKeyword.get()) gsonSupportPropertyBuilder.append(" ") @@ -49,7 +53,7 @@ object GsonSupporter : IGsonSupporter { gsonSupportPropertyBuilder.append(propertyType) - if (ConfigManager.initWithDefaultValue) { + if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) { gsonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType)) } diff --git a/src/wu/seal/jsontokotlin/IJsonLibSupporter.kt b/src/wu/seal/jsontokotlin/supporter/IJsonLibSupporter.kt similarity index 68% rename from src/wu/seal/jsontokotlin/IJsonLibSupporter.kt rename to src/wu/seal/jsontokotlin/supporter/IJsonLibSupporter.kt index eb395fa4..0659edf7 100644 --- a/src/wu/seal/jsontokotlin/IJsonLibSupporter.kt +++ b/src/wu/seal/jsontokotlin/supporter/IJsonLibSupporter.kt @@ -1,9 +1,10 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.supporter /** * * Created by Seal.Wu on 2017/9/28. */ interface IJsonLibSupporter { + val annotationImportClassString: String fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String): String } \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/JacksonSupporter.kt b/src/wu/seal/jsontokotlin/supporter/JacksonSupporter.kt similarity index 60% rename from src/wu/seal/jsontokotlin/JacksonSupporter.kt rename to src/wu/seal/jsontokotlin/supporter/JacksonSupporter.kt index 749ea096..839167f0 100644 --- a/src/wu/seal/jsontokotlin/JacksonSupporter.kt +++ b/src/wu/seal/jsontokotlin/supporter/JacksonSupporter.kt @@ -1,4 +1,8 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.KPropertyName +import wu.seal.jsontokotlin.codeelements.getDefaultValue /** * Jackson json lib supporter @@ -17,11 +21,11 @@ interface IJacksonSupporter { } fun main(args: Array) { - isTestModel = true - println("getGsonSupporterProperty:\n ${JacksonSupporter.getJacksonSupporterProperty("seal is **() good_man ","Boy")}") + wu.seal.jsontokotlin.isTestModel = true + println("getGsonSupporterProperty:\n ${wu.seal.jsontokotlin.supporter.JacksonSupporter.getJacksonSupporterProperty("seal is **() good_man ", "Boy")}") } -object JacksonSupporter : IJacksonSupporter { +object JacksonSupporter : wu.seal.jsontokotlin.supporter.IJacksonSupporter { private val anotaionOnProperty = "@JsonProperty(\"%s\")" @@ -29,11 +33,11 @@ object JacksonSupporter : IJacksonSupporter { val jacksonSupportPropertyBuilder = StringBuilder() - jacksonSupportPropertyBuilder.append(anotaionOnProperty.format(rawPropertyName)) + jacksonSupportPropertyBuilder.append(wu.seal.jsontokotlin.supporter.JacksonSupporter.anotaionOnProperty.format(rawPropertyName)) jacksonSupportPropertyBuilder.append(" ") - jacksonSupportPropertyBuilder.append(PropertyKeyword.get()) + jacksonSupportPropertyBuilder.append(KPropertyKeyword.get()) jacksonSupportPropertyBuilder.append(" ") @@ -43,7 +47,7 @@ object JacksonSupporter : IJacksonSupporter { jacksonSupportPropertyBuilder.append(propertyType) - if (ConfigManager.initWithDefaultValue) { + if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) { jacksonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType)) } diff --git a/src/wu/seal/jsontokotlin/supporter/LoganSquareSupporter.kt b/src/wu/seal/jsontokotlin/supporter/LoganSquareSupporter.kt new file mode 100644 index 00000000..76a166e4 --- /dev/null +++ b/src/wu/seal/jsontokotlin/supporter/LoganSquareSupporter.kt @@ -0,0 +1,49 @@ +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.KPropertyName +import wu.seal.jsontokotlin.codeelements.getDefaultValue + +/** + * LoganSquare Json Lib supporter file + * Created by Seal.Wu on 2017/11/1. + */ + +object LoganSquareSupporter : IJsonLibSupporter { + + val classAnnotation = "@JsonObject" + private val propertyAnnotation = "@JsonField(name = arrayOf(\"%s\"))" + + override val annotationImportClassString: String + get() = "import com.bluelinelabs.logansquare.annotation.JsonField\nimport com.bluelinelabs.logansquare.annotation.JsonObject" + + + override fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String): String { + + val loganSquareSupportPropertyBuilder = StringBuilder() + + loganSquareSupportPropertyBuilder.append(wu.seal.jsontokotlin.supporter.LoganSquareSupporter.propertyAnnotation.format(rawPropertyName)) + + loganSquareSupportPropertyBuilder.append(" ") + + loganSquareSupportPropertyBuilder.append(KPropertyKeyword.get()) + + loganSquareSupportPropertyBuilder.append(" ") + + loganSquareSupportPropertyBuilder.append(KPropertyName.getName(rawPropertyName)) + + loganSquareSupportPropertyBuilder.append(": ") + + loganSquareSupportPropertyBuilder.append(propertyType) + + if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) { + loganSquareSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType)) + } + + loganSquareSupportPropertyBuilder.append(",") + + return loganSquareSupportPropertyBuilder.toString() + + } + +} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/supporter/MoShiSupporter.kt b/src/wu/seal/jsontokotlin/supporter/MoShiSupporter.kt new file mode 100644 index 00000000..a73015d6 --- /dev/null +++ b/src/wu/seal/jsontokotlin/supporter/MoShiSupporter.kt @@ -0,0 +1,46 @@ +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.KPropertyName +import wu.seal.jsontokotlin.codeelements.getDefaultValue + +/** + * MoShiSupporter File + * Created by Seal.Wu on 2017/10/31. + */ + +object MoShiSupporter : IJsonLibSupporter { + override val annotationImportClassString: String + get() = "import com.squareup.moshi.Json" + + + private val propertyAnnotation = "@Json(name = \"%s\")" + + + override fun getJsonLibSupportPropertyBlockString(rawPropertyName: String, propertyType: String): String { + val moShijsonSupportPropertyBuilder = StringBuilder() + + moShijsonSupportPropertyBuilder.append(wu.seal.jsontokotlin.supporter.MoShiSupporter.propertyAnnotation.format(rawPropertyName)) + + moShijsonSupportPropertyBuilder.append(" ") + + moShijsonSupportPropertyBuilder.append(KPropertyKeyword.get()) + + moShijsonSupportPropertyBuilder.append(" ") + + moShijsonSupportPropertyBuilder.append(KPropertyName.getName(rawPropertyName)) + + moShijsonSupportPropertyBuilder.append(": ") + + moShijsonSupportPropertyBuilder.append(propertyType) + + if (wu.seal.jsontokotlin.ConfigManager.initWithDefaultValue) { + moShijsonSupportPropertyBuilder.append(" = ").append(getDefaultValue(propertyType)) + } + + moShijsonSupportPropertyBuilder.append(",") + + return moShijsonSupportPropertyBuilder.toString() + } + +} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/NoneSupporter.kt b/src/wu/seal/jsontokotlin/supporter/NoneSupporter.kt similarity index 79% rename from src/wu/seal/jsontokotlin/NoneSupporter.kt rename to src/wu/seal/jsontokotlin/supporter/NoneSupporter.kt index 8b0b6de7..881c31f0 100644 --- a/src/wu/seal/jsontokotlin/NoneSupporter.kt +++ b/src/wu/seal/jsontokotlin/supporter/NoneSupporter.kt @@ -1,4 +1,9 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.supporter + +import wu.seal.jsontokotlin.ConfigManager +import wu.seal.jsontokotlin.codeelements.KPropertyKeyword +import wu.seal.jsontokotlin.codeelements.getDefaultValue +import wu.seal.jsontokotlin.isTestModel /** * @@ -33,7 +38,7 @@ object NoneSupporter : INoneLibSupporter { val blockBuilder = StringBuilder() - blockBuilder.append(PropertyKeyword.get()) + blockBuilder.append(KPropertyKeyword.get()) blockBuilder.append(" ") blockBuilder.append(rawPropertyName) blockBuilder.append(": ").append(propertyType) diff --git a/src/wu/seal/jsontokotlin/ConfigSettingDialog.kt b/src/wu/seal/jsontokotlin/ui/ConfigSettingDialog.kt similarity index 96% rename from src/wu/seal/jsontokotlin/ConfigSettingDialog.kt rename to src/wu/seal/jsontokotlin/ui/ConfigSettingDialog.kt index a7ef3d48..40ed9c1c 100644 --- a/src/wu/seal/jsontokotlin/ConfigSettingDialog.kt +++ b/src/wu/seal/jsontokotlin/ui/ConfigSettingDialog.kt @@ -1,4 +1,4 @@ -package wu.seal.jsontokotlin +package wu.seal.jsontokotlin.ui import com.intellij.openapi.ui.DialogWrapper import com.intellij.util.ui.JBDimension @@ -49,7 +49,7 @@ class ConfigSettingDialog(canBeParent: Boolean) : DialogWrapper(canBeParent), IC return tabbedPane } - private fun createTargetJsonLibConfigPanel() = TargetJsonLibConfigPanel(true) + private fun createTargetJsonLibConfigPanel() = TargetJsonLibConfigPanelContainer(true) private fun createCommentConfigPanel() = CommentConfigPanel(true) diff --git a/src/wu/seal/jsontokotlin/ui/JsonInputDialog.kt b/src/wu/seal/jsontokotlin/ui/JsonInputDialog.kt new file mode 100644 index 00000000..daf1c2d7 --- /dev/null +++ b/src/wu/seal/jsontokotlin/ui/JsonInputDialog.kt @@ -0,0 +1,142 @@ +package wu.seal.jsontokotlin.ui + +import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.Messages +import com.intellij.ui.DocumentAdapter +import com.intellij.ui.components.JBLabel +import com.intellij.util.ui.JBDimension +import com.intellij.util.ui.JBEmptyBorder +import wu.seal.jsontokotlin.addComponentIntoVerticalBoxAlignmentLeft +import java.awt.event.ActionEvent +import javax.swing.* +import javax.swing.event.DocumentEvent + +/** + * Dialog widget relative + * Created by Seal.wu on 2017/9/21. + */ + + +class MyInputValidator : com.intellij.openapi.ui.InputValidator { + + lateinit var classNameField: JTextField + override fun checkInput(inputString: String): Boolean { + try { + val classNameLegal = classNameField.text.trim().isNotBlank() + val jsonElement = com.google.gson.JsonParser().parse(inputString) + + return (jsonElement.isJsonObject || jsonElement.isJsonArray) && classNameLegal + } catch (e: com.google.gson.JsonSyntaxException) { + return false + } + + } + + override fun canClose(inputString: String): Boolean { + return true + } +} + +val myInputValidator = MyInputValidator() + +/** + * Json input Dialog + */ +class JsonInputDialog(project: Project) : Messages.InputDialog(project, "Please input the class name and JSON data for generating Kotlin data class", "Make Kotlin Data Class", Messages.getInformationIcon(), "", myInputValidator) { + + private lateinit var classNameInput: JTextField + + init { + setOKButtonText("Make") + } + override fun createMessagePanel(): javax.swing.JPanel { + val messagePanel = javax.swing.JPanel(java.awt.BorderLayout()) + if (myMessage != null) { + val textComponent = createTextComponent() + messagePanel.add(textComponent, java.awt.BorderLayout.NORTH) + } + myField = createTextFieldComponent() + + + val classNameInputContainer = createLinearLayoutVertical() + val classNameTitle = JBLabel("Class Name: ") + classNameTitle.border = JBEmptyBorder(5, 0, 5, 0) + classNameInputContainer.addComponentIntoVerticalBoxAlignmentLeft(classNameTitle) + classNameInput = JTextField() + classNameInput.preferredSize = JBDimension(400, 30) + myInputValidator.classNameField = classNameInput + + classNameInput.document.addDocumentListener(object : DocumentAdapter() { + override fun textChanged(e: DocumentEvent?) { + okAction.isEnabled = myInputValidator.checkInput(myField.text) + } + }) + + classNameInputContainer.addComponentIntoVerticalBoxAlignmentLeft(classNameInput) + classNameInputContainer.preferredSize = JBDimension(500, 50) + + + val createScrollableTextComponent = createScrollableTextComponent() + val jsonInputContainer = createLinearLayoutVertical() + val jsonTitle = JBLabel("JSON Text:") + jsonTitle.border = JBEmptyBorder(5, 0, 5, 0) + jsonInputContainer.addComponentIntoVerticalBoxAlignmentLeft(jsonTitle) + jsonInputContainer.addComponentIntoVerticalBoxAlignmentLeft(createScrollableTextComponent) + + + val centerContainer = JPanel() + val centerBoxLayout = BoxLayout(centerContainer, BoxLayout.PAGE_AXIS) + centerContainer.layout = centerBoxLayout + centerContainer.addComponentIntoVerticalBoxAlignmentLeft(classNameInputContainer) + centerContainer.addComponentIntoVerticalBoxAlignmentLeft(jsonInputContainer) + messagePanel.add(centerContainer, java.awt.BorderLayout.CENTER) + val settingButton = javax.swing.JButton("Config Settings") + settingButton.addActionListener(object : AbstractAction() { + override fun actionPerformed(e: ActionEvent) { + ConfigSettingDialog(false).show() + } + }) + val settingContainer = javax.swing.JPanel() + val boxLayout = javax.swing.BoxLayout(settingContainer, BoxLayout.LINE_AXIS) + settingContainer.layout = boxLayout + settingButton.horizontalAlignment = javax.swing.SwingConstants.RIGHT + settingContainer.add(settingButton) + messagePanel.add(settingContainer, java.awt.BorderLayout.SOUTH) + + return messagePanel + } + + override fun createTextFieldComponent(): javax.swing.text.JTextComponent { + val jTextArea = javax.swing.JTextArea(15, 100) + jTextArea.minimumSize = JBDimension(800, 450) + jTextArea.maximumSize = JBDimension(1000, 700) + jTextArea.lineWrap = true + jTextArea.wrapStyleWord = true + jTextArea.autoscrolls = true + return jTextArea + } + + + protected fun createScrollableTextComponent(): javax.swing.JComponent { + return com.intellij.ui.components.JBScrollPane(myField) + } + + fun getClassName(): String { + if (exitCode == 0) { + return classNameInput.text.trim(); + } + return ""; + } + + override fun getPreferredFocusedComponent(): JComponent? { + return classNameInput + } +} + + +fun createLinearLayoutVertical(): JPanel { + val container = JPanel() + val boxLayout = BoxLayout(container, BoxLayout.PAGE_AXIS) + container.layout = boxLayout + return container +} \ No newline at end of file diff --git a/src/wu/seal/jsontokotlin/ui/Panel.kt b/src/wu/seal/jsontokotlin/ui/Panel.kt new file mode 100644 index 00000000..d08f56bd --- /dev/null +++ b/src/wu/seal/jsontokotlin/ui/Panel.kt @@ -0,0 +1,315 @@ +package wu.seal.jsontokotlin.ui + +import com.intellij.ui.components.JBLabel +import com.intellij.util.ui.JBDimension +import com.intellij.util.ui.JBEmptyBorder +import com.intellij.util.ui.JBUI +import wu.seal.jsontokotlin.ConfigManager +import wu.seal.jsontokotlin.TargetJsonConverter +import wu.seal.jsontokotlin.addComponentIntoVerticalBoxAlignmentLeft +import javax.swing.* +import javax.swing.border.EmptyBorder + + +/** + * todo //beautify interface + * property config panel + * Created by seal wu on 2017/9/18. + */ +class PropertyPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) { + + constructor(layout: java.awt.LayoutManager?) : this(layout, false) + + constructor(isDoubleBuffered: Boolean) : this(java.awt.FlowLayout(), isDoubleBuffered) + + + init { + + + val boxLayout = BoxLayout(this, BoxLayout.PAGE_AXIS) + setLayout(boxLayout) + val bordWidth = JBUI.scale(10) + border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth) + + val keywordLable = JLabel("Keyword") + + val radioButtonVal = JRadioButton("Val") + + radioButtonVal.addActionListener { + ConfigManager.isPropertiesVar = false + } + val radioButtonVar = JRadioButton("Var") + + radioButtonVar.addActionListener { + ConfigManager.isPropertiesVar = true + } + + if (ConfigManager.isPropertiesVar) { + + radioButtonVar.isSelected = true + } else { + radioButtonVal.isSelected = true + } + val buttonGroupProperty = ButtonGroup() + buttonGroupProperty.add(radioButtonVal) + buttonGroupProperty.add(radioButtonVar) + + addComponentIntoVerticalBoxAlignmentLeft(keywordLable) + add(Box.createVerticalStrut(20)) + addComponentIntoVerticalBoxAlignmentLeft(radioButtonVal) + add(Box.createVerticalStrut(20)) + + addComponentIntoVerticalBoxAlignmentLeft(radioButtonVar) + + add(Box.createVerticalStrut(20)) + + + val line = com.intellij.util.xml.ui.TextPanel() + line.maximumSize = JBDimension(480, 1) + line.minimumSize = JBDimension(480, 1) + line.background = java.awt.Color.GRAY + + add(line) + + val nullAbleCheck = JCheckBox("Property type be Nullable(?)") + if (ConfigManager.isPropertyNullable) { + nullAbleCheck.isSelected = true + } + + val initWithDefaultValueCheck = JCheckBox("Init with default value (avoid null)") + initWithDefaultValueCheck.isSelected = ConfigManager.initWithDefaultValue + + initWithDefaultValueCheck.addActionListener { + ConfigManager.initWithDefaultValue = initWithDefaultValueCheck.isSelected + } + + nullAbleCheck.addActionListener { + ConfigManager.isPropertyNullable = nullAbleCheck.isSelected + } + + add(Box.createVerticalStrut(20)) + + addComponentIntoVerticalBoxAlignmentLeft(initWithDefaultValueCheck) + add(Box.createVerticalStrut(20)) + + addComponentIntoVerticalBoxAlignmentLeft(nullAbleCheck) + + + } +} + + +/** + * Comment Config Panel + */ +class CommentConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) { + + constructor(layout: java.awt.LayoutManager?) : this(layout, false) + + constructor(isDoubleBuffered: Boolean) : this(java.awt.FlowLayout(), isDoubleBuffered) + + init { + + val bordWidth = JBUI.scale(10) + border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth) + setLayout(java.awt.GridLayout(6, 1, 10, 10)) + + val radioButtonOpen = JRadioButton("On") + + radioButtonOpen.addActionListener { + ConfigManager.isCommentOff = false + } + val radioButtonOff = JRadioButton("Off") + radioButtonOff.addActionListener { + ConfigManager.isCommentOff = true + } + + if (ConfigManager.isCommentOff) { + + radioButtonOff.isSelected = true + } else { + radioButtonOpen.isSelected = true + } + + val buttonGroupProperty = ButtonGroup() + buttonGroupProperty.add(radioButtonOpen) + buttonGroupProperty.add(radioButtonOff) + + add(radioButtonOpen) + add(radioButtonOff) + } + +} + +/** + * Target JsonLib ConfigPanel container + */ +class TargetJsonLibConfigPanelContainer(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean) : JPanel(layout, isDoubleBuffered) { + + constructor(layout: java.awt.LayoutManager?) : this(layout, false) + + constructor(isDoubleBuffered: Boolean) : this(java.awt.FlowLayout(), isDoubleBuffered) + + init { + val boxLayout = BoxLayout(this, BoxLayout.PAGE_AXIS) + setLayout(boxLayout) + val bordWidth = JBUI.scale(10) + border = EmptyBorder(bordWidth, bordWidth, bordWidth, bordWidth) + + val subBoxPanel = JPanel() + subBoxPanel.preferredSize = JBDimension(480, 90) + subBoxPanel.maximumSize = JBDimension(480, 90) + val subBoxLayout = BoxLayout(subBoxPanel, BoxLayout.PAGE_AXIS) + + subBoxPanel.layout = subBoxLayout + val annotationStringPanel = JPanel(true) + annotationStringPanel.maximumSize = JBDimension(480, 30) + annotationStringPanel.layout = java.awt.FlowLayout(java.awt.FlowLayout.LEFT) + annotationStringPanel.add(JBLabel("Property Annotation Format: ")) + val annotationFormatField = JTextField(ConfigManager.customAnnotaionFormatString) + val fieldDefaultFont = annotationFormatField.font + annotationFormatField.addFocusListener(object : java.awt.event.FocusListener { + override fun focusLost(e: java.awt.event.FocusEvent?) { + ConfigManager.customAnnotaionFormatString = annotationFormatField.text + } + + override fun focusGained(e: java.awt.event.FocusEvent?) { + } + + }) + annotationStringPanel.add(annotationFormatField, java.awt.FlowLayout.CENTER) + subBoxPanel.addComponentIntoVerticalBoxAlignmentLeft(annotationStringPanel) + + val annotationImportClass = JPanel(true) + annotationImportClass.layout = java.awt.FlowLayout(java.awt.FlowLayout.LEFT) + val importClassLable = JBLabel("Property Annotation Import Class : ") + importClassLable.border = JBEmptyBorder(3,0,3,0) + annotationImportClass.add(importClassLable) + val annotationImportClassTextArea = JTextArea(ConfigManager.customAnnotaionImportClassString) + annotationImportClassTextArea.font = fieldDefaultFont + annotationImportClassTextArea.preferredSize = JBDimension(480, 30) + annotationImportClassTextArea.addFocusListener(object : java.awt.event.FocusListener { + override fun focusLost(e: java.awt.event.FocusEvent?) { + ConfigManager.customAnnotaionImportClassString = annotationImportClassTextArea.text + + } + + override fun focusGained(e: java.awt.event.FocusEvent?) { + } + + }) + annotationImportClass.add(annotationImportClassTextArea) + subBoxPanel.addComponentIntoVerticalBoxAlignmentLeft(annotationImportClass) + + addComponentIntoVerticalBoxAlignmentLeft(TargetJsonLibConfigPanel(true, { + annotationStringPanel.isVisible = it + annotationImportClass.isVisible = it + })) + + addComponentIntoVerticalBoxAlignmentLeft(subBoxPanel) + + annotationStringPanel.isVisible = ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom + annotationImportClass.isVisible = ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom + + } +} + +/** + * Target JsonLib ConfigPanel + */ +class TargetJsonLibConfigPanel(layout: java.awt.LayoutManager?, isDoubleBuffered: Boolean, callBack: (selected: Boolean) -> Unit) : JPanel(layout, isDoubleBuffered) { + + constructor(layout: java.awt.LayoutManager?, callBack: (selected: Boolean) -> Unit) : this(layout, false, callBack) + + constructor(isDoubleBuffered: Boolean, callBack: (selected: Boolean) -> Unit) : this(java.awt.FlowLayout(), isDoubleBuffered, callBack) + + init { + setLayout(java.awt.GridLayout(4, 2, 10, 10)) + + val radioButtonNone = JRadioButton("None") + val radioButtonGson = JRadioButton("Gson") + val radioButtonJackson = JRadioButton("Jackson") + val radioButtonFastjson = JRadioButton("Fastjson") + val radioButtonMoShi = JRadioButton("MoShi") + val radioButtonLoganSquare = JRadioButton("LoganSquare") + val radioButtonCustom = JRadioButton("Others by customize") + + radioButtonNone.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.None + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + + } + radioButtonGson.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.Gson + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + + } + radioButtonJackson.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.Jackson + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + } + radioButtonFastjson.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.FastJson + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + } + + radioButtonMoShi.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.MoShi + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + } + + radioButtonLoganSquare.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.LoganSquare + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + } + radioButtonCustom.addActionListener { + ConfigManager.targetJsonConverterLib = TargetJsonConverter.Custom + callBack(ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) + } + + if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.None) { + + radioButtonNone.isSelected = true + + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Gson) { + + radioButtonGson.isSelected = true + + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Jackson) { + + radioButtonJackson.isSelected = true + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.FastJson) { + + radioButtonFastjson.isSelected = true + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.LoganSquare) { + + radioButtonLoganSquare.isSelected = true + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.MoShi) { + + radioButtonMoShi.isSelected = true + } else if (ConfigManager.targetJsonConverterLib == TargetJsonConverter.Custom) { + + radioButtonCustom.isSelected = true + + } + + val buttonGroupProperty = ButtonGroup() + buttonGroupProperty.add(radioButtonNone) + buttonGroupProperty.add(radioButtonGson) + buttonGroupProperty.add(radioButtonJackson) + buttonGroupProperty.add(radioButtonFastjson) + buttonGroupProperty.add(radioButtonMoShi) + buttonGroupProperty.add(radioButtonLoganSquare) + buttonGroupProperty.add(radioButtonCustom) + + add(radioButtonNone) + add(radioButtonGson) + add(radioButtonJackson) + add(radioButtonFastjson) + add(radioButtonMoShi) + add(radioButtonLoganSquare) + add(radioButtonCustom) + } + +} + diff --git a/test/wu/seal/jsontokotlin/CustomJsonLibSupporterTest.kt b/test/wu/seal/jsontokotlin/CustomJsonLibSupporterTest.kt new file mode 100644 index 00000000..edb9f19c --- /dev/null +++ b/test/wu/seal/jsontokotlin/CustomJsonLibSupporterTest.kt @@ -0,0 +1,30 @@ +package wu.seal.jsontokotlin + +import org.junit.Test +import wu.seal.jsontokotlin.supporter.CustomJsonLibSupporter + +/** + * Created by Seal.Wu on 2017/11/1. + */ +class CustomJsonLibSupporterTest { + + @Test + fun testAnnotationImportClass() { + isTestModel = true + println(ConfigManager.customAnnotaionImportClassString) + } + + @org.junit.Test + fun getJsonLibSupportPropertyBlockString() { + + isTestModel =true + val rawPropertyName = "seal_ wu" + val propertyBlockString = CustomJsonLibSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, "String") + + println(propertyBlockString) + + assert(propertyBlockString.contains(String.format(ConfigManager.customAnnotaionFormatString,rawPropertyName))) + + } + +} \ No newline at end of file diff --git a/test/wu/seal/jsontokotlin/FastjsonSupporterTest.kt b/test/wu/seal/jsontokotlin/FastjsonSupporterTest.kt index 2f51ac34..b0f3fbda 100644 --- a/test/wu/seal/jsontokotlin/FastjsonSupporterTest.kt +++ b/test/wu/seal/jsontokotlin/FastjsonSupporterTest.kt @@ -1,8 +1,7 @@ package wu.seal.jsontokotlin -import org.junit.* - import org.junit.Assert.* +import wu.seal.jsontokotlin.supporter.FastjsonSupporter /** * Created by Seal.Wu on 2017/10/19. diff --git a/test/wu/seal/jsontokotlin/KPropertyTest.kt b/test/wu/seal/jsontokotlin/KPropertyTest.kt new file mode 100644 index 00000000..0be2ea3b --- /dev/null +++ b/test/wu/seal/jsontokotlin/KPropertyTest.kt @@ -0,0 +1,44 @@ +package wu.seal.jsontokotlin + +import org.junit.* + +import wu.seal.jsontokotlin.codeelements.KProperty + +/** + * Created by Seal.Wu on 2017/11/1. + */ +class KPropertyTest { + @Before + fun setUp() { + isTestModel = true + } + + /** + * test the config and choose respective supporter logic is OK + */ + @org.junit.Test + fun getPropertyStringBlock() { + + TargetJsonConverter.values().forEach { + ConfigManager.targetJsonConverterLib = it + val property = KProperty("seal is a *() good_man", "Boolean", "true") + + + val propertyStringBlock = property.getPropertyStringBlock() + + when(ConfigManager.targetJsonConverterLib){ + TargetJsonConverter.None-> assert(propertyStringBlock.contains("@").not()) + TargetJsonConverter.Gson-> assert(propertyStringBlock.contains("@SerializedName")) + TargetJsonConverter.Jackson-> assert(propertyStringBlock.contains("@JsonProperty")) + TargetJsonConverter.FastJson-> assert(propertyStringBlock.contains("@JSONField(name")) + TargetJsonConverter.MoShi-> assert(propertyStringBlock.contains("@Json(name")) + TargetJsonConverter.LoganSquare-> assert(propertyStringBlock.contains("@JsonField(name = arrayOf")) + TargetJsonConverter.Custom-> assert(propertyStringBlock.contains(ConfigManager.customAnnotaionFormatString.substring(0..1))) + } + + println("getPropertyStringBlock:\n$propertyStringBlock") + } + + } + +} \ No newline at end of file diff --git a/test/wu/seal/jsontokotlin/LoganSquareSupporterTest.kt b/test/wu/seal/jsontokotlin/LoganSquareSupporterTest.kt new file mode 100644 index 00000000..2df3d560 --- /dev/null +++ b/test/wu/seal/jsontokotlin/LoganSquareSupporterTest.kt @@ -0,0 +1,41 @@ +package wu.seal.jsontokotlin + +import org.junit.* +import org.junit.Test + +import wu.seal.jsontokotlin.supporter.LoganSquareSupporter + +/** + * Created by Seal.Wu on 2017/11/1. + */ +class LoganSquareSupporterTest { + @Before + fun setUp() { + isTestModel = true + } + + @After + fun tearDown() { + } + + @org.junit.Test + fun getClassAnnotation() { + assert(LoganSquareSupporter.classAnnotation.isNotEmpty()) + } + + @Test + fun getAnnotationImportClassString() { + assert(LoganSquareSupporter.annotationImportClassString.isNotEmpty()) + assert(LoganSquareSupporter.annotationImportClassString.split("\n").size == 2) + } + + @Test + fun getJsonLibSupportPropertyBlockString() { + val rawPropertyName = "seal is **() good_man " + val type = "Boy" + val block = LoganSquareSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, type) + assert(block.contains(rawPropertyName)) + assert(block.contains(type)) + } + +} diff --git a/test/wu/seal/jsontokotlin/MoShiSupporterTest.kt b/test/wu/seal/jsontokotlin/MoShiSupporterTest.kt new file mode 100644 index 00000000..b4472c54 --- /dev/null +++ b/test/wu/seal/jsontokotlin/MoShiSupporterTest.kt @@ -0,0 +1,32 @@ +package wu.seal.jsontokotlin + +import org.junit.* +import org.junit.Test + +import wu.seal.jsontokotlin.supporter.MoShiSupporter + +/** + * Created by Seal.Wu on 2017/11/1. + */ +class MoShiSupporterTest { + @Before + fun setUp() { + isTestModel = true + } + + @org.junit.Test + fun getAnnotationImportClassString() { + assert(MoShiSupporter.annotationImportClassString.isNotEmpty()) + assert(MoShiSupporter.annotationImportClassString.split("\n").size ==1) + } + + @Test + fun getJsonLibSupportPropertyBlockString() { + val rawPropertyName = "seal is **() good_man " + val type = "Boy" + val block = MoShiSupporter.getJsonLibSupportPropertyBlockString(rawPropertyName, type) + assert(block.contains(rawPropertyName)) + assert(block.contains(type)) + } + +} \ No newline at end of file