File tree Expand file tree Collapse file tree 3 files changed +103
-2
lines changed
test/kotlin/extensions/chen.biao Expand file tree Collapse file tree 3 files changed +103
-2
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ package extensions
22
33import extensions.wu.seal.PropertyPrefixSupport
44import extensions.wu.seal.PropertySuffixSupport
5-
5+ import extensions.chen.biao.KeepAnnotationSupport
66/* *
77 * extension collect, all extensions will be hold by this class's extensions property
88 */
@@ -12,6 +12,7 @@ object ExtensionsCollector {
1212 */
1313 val extensions = listOf (
1414 PropertyPrefixSupport ,
15- PropertySuffixSupport
15+ PropertySuffixSupport ,
16+ KeepAnnotationSupport
1617 )
1718}
Original file line number Diff line number Diff line change 1+ package extensions.chen.biao
2+
3+ import com.intellij.ui.layout.panel
4+ import extensions.Extension
5+ import wu.seal.jsontokotlin.classscodestruct.Annotation
6+ import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
7+ import javax.swing.JCheckBox
8+ import javax.swing.JPanel
9+
10+ /* *
11+ * @author chenbiao
12+ * create at 2019/5/16
13+ * description:
14+ */
15+ object KeepAnnotationSupport : Extension() {
16+
17+
18+ val configKey = " chen.biao.add_keep_annotation_enable"
19+
20+ override fun createUI (): JPanel {
21+
22+ val checkBox = JCheckBox (" Enable Add a Keep Annotation To The Class" ).apply {
23+ isSelected = getConfig(configKey).toBoolean()
24+ addActionListener {
25+ setConfig(configKey, isSelected.toString())
26+ }
27+ }
28+
29+ return panel {
30+ row {
31+ checkBox()
32+ }
33+ }
34+ }
35+
36+ override fun intercept (kotlinDataClass : KotlinDataClass ): KotlinDataClass {
37+
38+ return if (getConfig(configKey).toBoolean()) {
39+
40+ val classAnnotationString = " @Keep"
41+
42+ val classAnnotation = Annotation .fromAnnotationString(classAnnotationString)
43+
44+ return kotlinDataClass.copy(annotations = listOf (classAnnotation))
45+ } else {
46+ kotlinDataClass
47+ }
48+
49+ }
50+
51+ override fun intercept (originClassImportDeclaration : String ): String {
52+
53+ val classAnnotationImportClassString = " import android.support.annotation.Keep"
54+
55+ return if (getConfig(configKey).toBoolean()) {
56+ originClassImportDeclaration.append(classAnnotationImportClassString)
57+ } else {
58+ originClassImportDeclaration
59+ }
60+ }
61+ }
Original file line number Diff line number Diff line change 1+ package extensions.chen.biao
2+
3+ import com.winterbe.expekt.should
4+ import org.junit.Before
5+ import org.junit.Test
6+ import wu.seal.jsontokotlin.generateKotlinDataClass
7+ import wu.seal.jsontokotlin.test.TestConfig
8+
9+ /* *
10+ * @author chenbiao
11+ * create at 2019/5/17
12+ * description:
13+ */
14+ class KeepAnnotationSupportTest {
15+
16+ val json = """ {"name":"chenbiao"}"""
17+
18+
19+ var expectResult = """ @Keep
20+ data class Test(
21+ val name: String // chenbiao
22+ )"""
23+
24+ @Before
25+ fun setUp () {
26+ TestConfig .setToTestInitState()
27+ }
28+
29+ @Test
30+ fun interceptTest (){
31+ val kotlinDataClass = json.generateKotlinDataClass()
32+ KeepAnnotationSupport .getTestHelper().setConfig(" chen.biao.add_keep_annotation_enable" ," true" )
33+ val generatedCode = kotlinDataClass.applyInterceptors(listOf (KeepAnnotationSupport )).getCode()
34+ print (generatedCode)
35+ generatedCode.trimMargin().should.equal(expectResult.trimMargin())
36+
37+ }
38+
39+ }
You can’t perform that action at this time.
0 commit comments