Skip to content

Commit cb0f0f2

Browse files
smilechenbiaowuseal
authored andcommitted
增加了一项可以添加Keep注解的功能。 (#177)
* 增加了一项可以添加Keep注解的功能。 * Add unit test of keep annotation
1 parent f30e762 commit cb0f0f2

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

src/main/kotlin/extensions/ExtensionsCollector.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package extensions
22

33
import extensions.wu.seal.PropertyPrefixSupport
44
import 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
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)