Skip to content

Commit 5ec6977

Browse files
authored
Merge pull request #207 from hanshengjian/3.4.0
Enable Parcelable Support
2 parents af171c4 + c8e3630 commit 5ec6977

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

src/main/kotlin/extensions/ExtensionsCollector.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import extensions.ted.zeng.PropertyAnnotationLineSupport
55
import extensions.wu.seal.ClassNameSuffixSupport
66
import extensions.wu.seal.PropertyPrefixSupport
77
import extensions.wu.seal.PropertySuffixSupport
8+
import extensions.jose.han.ParcelableAnnotationSupport
89

910
/**
1011
* extension collect, all extensions will be hold by this class's extensions property
@@ -18,6 +19,7 @@ object ExtensionsCollector {
1819
PropertySuffixSupport,
1920
KeepAnnotationSupport,
2021
PropertyAnnotationLineSupport,
21-
ClassNameSuffixSupport
22+
ClassNameSuffixSupport,
23+
ParcelableAnnotationSupport
2224
)
2325
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package extensions.jose.han;
2+
3+
import com.intellij.ui.layout.panel
4+
import com.intellij.util.ui.JBDimension
5+
import extensions.Extension
6+
import extensions.wu.seal.PropertySuffixSupport
7+
import wu.seal.jsontokotlin.classscodestruct.Annotation
8+
import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
9+
import java.awt.Color
10+
import java.awt.event.FocusEvent
11+
import java.awt.event.FocusListener
12+
import javax.swing.JCheckBox
13+
import javax.swing.JPanel
14+
import javax.swing.JTextArea
15+
import javax.swing.JTextField
16+
17+
/**
18+
* @author jose.han
19+
* @Date 2019/7/27
20+
*/
21+
object ParcelableAnnotationSupport : Extension() {
22+
23+
val configKey = "jose.han.add_parcelable_annotatioin_enable"
24+
25+
override fun createUI(): JPanel {
26+
val labelJField = JTextArea().apply {
27+
text = " android {\n" +
28+
" ...\n" +
29+
" androidExtensions {\n" +
30+
" experimental = true\n" +
31+
" }\n" +
32+
" }"
33+
isEnabled = false
34+
minimumSize = JBDimension(200, 200)
35+
background = Color.BLACK
36+
}
37+
38+
39+
val checkBox = JCheckBox("Enable Parcelable Support").apply {
40+
isSelected = ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()
41+
addActionListener {
42+
ParcelableAnnotationSupport.setConfig(ParcelableAnnotationSupport.configKey, isSelected.toString())
43+
}
44+
}
45+
46+
return panel {
47+
row {
48+
checkBox()
49+
labelJField()
50+
}
51+
}
52+
}
53+
54+
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
55+
return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) {
56+
57+
val classAnnotationString1 = "@SuppressLint(\"ParcelCreator\")"
58+
val classAnnotationString2 = "@Parcelize"
59+
60+
val classAnnotation1 = Annotation.fromAnnotationString(classAnnotationString1)
61+
val classAnnotation2 = Annotation.fromAnnotationString(classAnnotationString2)
62+
63+
return kotlinDataClass.copy(annotations = listOf(classAnnotation1,classAnnotation2),parentClassTemplate = "Parcelable")
64+
} else {
65+
kotlinDataClass
66+
}
67+
}
68+
69+
override fun intercept(originClassImportDeclaration: String): String {
70+
71+
val classAnnotationImportClassString = "import kotlinx.android.parcel.Parcelize".append("import android.os.Parcelable")
72+
73+
return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) {
74+
originClassImportDeclaration.append(classAnnotationImportClassString)
75+
} else {
76+
originClassImportDeclaration
77+
}
78+
}
79+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package extensions.jose.han
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 jose.han
11+
* @Date 2019/04/27
12+
*/
13+
class ParcelableAnnotationSupportTest{
14+
val json = """{"name":"jose.han","age":18,"height":18.7, "face":true}"""
15+
16+
17+
var expectResult = """@SuppressLint("ParcelCreator")
18+
@Parcelize
19+
data class Test(
20+
val name: String, // jose.han
21+
val age: Int, // 18
22+
val height: Double, // 18.7
23+
val face: Boolean // true
24+
) : Parcelable
25+
"""
26+
27+
@Before
28+
fun setUp() {
29+
TestConfig.setToTestInitState()
30+
}
31+
32+
@Test
33+
fun interceptTest(){
34+
val kotlinDataClass = json.generateKotlinDataClass()
35+
ParcelableAnnotationSupport.getTestHelper().setConfig("jose.han.add_parcelable_annotatioin_enable","true")
36+
val generatedCode = kotlinDataClass.applyInterceptors(listOf(ParcelableAnnotationSupport)).getCode()
37+
print(generatedCode)
38+
generatedCode.trimMargin().should.equal(expectResult.trimMargin())
39+
40+
}
41+
42+
43+
}

0 commit comments

Comments
 (0)