1818package com.udfsoft.screenfactorygenerator.processor.visitor
1919
2020import com.google.devtools.ksp.processing.KSPLogger
21- import com.google.devtools.ksp.symbol.ClassKind
22- import com.google.devtools.ksp.symbol.KSAnnotation
23- import com.google.devtools.ksp.symbol.KSClassDeclaration
24- import com.google.devtools.ksp.symbol.KSVisitorVoid
25- import com.udfsoft.screenfactorygenerator.annotation.JParam
26- import com.udfsoft.screenfactorygenerator.annotation.JScreen
21+ import com.google.devtools.ksp.symbol.KSPropertyDeclaration
22+ import com.google.devtools.ksp.symbol.KSValueArgument
2723import com.udfsoft.screenfactorygenerator.processor.mapper.KSPropertyDeclarationToIntentPutExtraString
2824import com.udfsoft.screenfactorygenerator.processor.mapper.KsPropertyDeclarationToGetExtraString
29- import com.udfsoft.screenfactorygenerator.utils.IoUtils.plusAssign
30- import com.udfsoft.screenfactorygenerator.utils.Utils.findAnnotation
31- import com.udfsoft.screenfactorygenerator.utils.Utils.findArgument
32- import com.udfsoft.screenfactorygenerator.utils.Utils.getDeclaredPropertiesWithAnnotation
33- import com.udfsoft.screenfactorygenerator.utils.Utils.isChildForClass
34- import com.udfsoft.screenfactorygenerator.utils.Utils.replaceFirst
3525import java.io.OutputStream
3626
3727class ActivityVisitor (
38- private val file : OutputStream ,
39- private val className : String ,
28+ file : OutputStream ,
29+ className : String ,
4030 private val logger : KSPLogger ,
41- private val screenManagerClassStringBuilder : StringBuilder
42- ) : KSVisitorVoid( ) {
31+ screenManagerClassStringBuilder : StringBuilder
32+ ) : BaseVisitor(file, className, logger, screenManagerClassStringBuilder ) {
4333
44- override fun visitClassDeclaration (classDeclaration : KSClassDeclaration , data : Unit ) {
45- if (classDeclaration.classKind != ClassKind .CLASS ) {
46- logger.error(" Only class can be annotated with @JScreen" , classDeclaration)
47- return
48- }
49-
50- val isActivity = classDeclaration.isChildForClass(" Activity" )
51-
52- if (! isActivity) {
53- logger.error(" Class must be inherited from Activity" , classDeclaration)
54- return
55- }
34+ override fun getParentClassString () = ACTIVITY_CLASS_NAME
5635
57- val annotation: KSAnnotation = classDeclaration.findAnnotation(JScreen ::class .simpleName!! )
58-
59- val generateScreenMethodArgument = annotation.arguments.findArgument(" generateScreenMethod" )
60-
61- val params =
62- classDeclaration.getDeclaredPropertiesWithAnnotation(JParam ::class .simpleName!! )
63-
64- if (generateScreenMethodArgument.value == true ) {
65- file + = " import com.github.terrakok.cicerone.androidx.ActivityScreen\n "
66- }
36+ override fun getImportForGenericClass (generateScreenMethodArgument : KSValueArgument ) =
37+ buildString {
38+ if (generateScreenMethodArgument.value == true ) {
39+ appendLine(" import com.github.terrakok.cicerone.androidx.ActivityScreen" )
40+ }
6741
68- file + = " import android.content.Context\n "
69- file + = " import android.content.Intent\n "
70- file + = " import androidx.core.os.bundleOf\n "
71- file + = " import androidx.fragment.app.Fragment\n "
72- file + = " import com.udfsoft.screenfactorygenerator.BaseScreen\n\n "
73-
74- file + = " object ${className} Screen : BaseScreen {\n\n "
75-
76- file + = if (params.toList().isEmpty()) {
77- " fun newIntent(context: Context) = Intent(context, ${className} ::class.java)\n\n "
78- } else {
79- val paramsForSetArguments =
80- params.map(KSPropertyDeclarationToIntentPutExtraString ()::transform)
81- .joinToString(" \n " )
82-
83- val paramsInString = params.map {
84- " $it : ${it.type} "
85- }.joinToString()
86-
87- " fun newIntent(context: Context, $paramsInString ): Intent { \n " +
88- " val intent = Intent(context, ${className} ::class.java)\n " +
89- " $paramsForSetArguments \n " +
90- " return intent\n " +
91- " }\n\n "
42+ appendLine(" import android.content.Context" )
43+ appendLine(" import android.content.Intent" )
44+ appendLine(" import androidx.core.os.bundleOf" )
45+ appendLine(" import androidx.fragment.app.Fragment" )
46+ appendLine(" import com.udfsoft.screenfactorygenerator.BaseScreen\n " )
9247 }
9348
94- file + = " fun ${className} .initArguments() {\n "
95-
96- val paramsForGetArguments = params.map(KsPropertyDeclarationToGetExtraString ()::transform)
97- .joinToString(" \n " )
98-
99- file + = " $paramsForGetArguments \n "
100- file + = " }\n "
101-
102- if (generateScreenMethodArgument.value == true ) {
103- file + = " \n "
104- file + = if (params.toList().isEmpty()) {
105- " fun get${className} Screen() = ActivityScreen { newIntent(it) }\n "
49+ override fun getClassBody (
50+ params : Sequence <KSPropertyDeclaration >,
51+ generateScreenMethodArgument : KSValueArgument ,
52+ className : String
53+ ): String = buildString {
54+ appendLine(
55+ if (params.toList().isEmpty()) {
56+ " fun newIntent(context: Context) = Intent(context, ${className} ::class.java)\n "
10657 } else {
107-
108- val paramsWithTypeInString = params.map {
109- " $it : ${it.type} "
110- }.joinToString()
58+ val paramsForSetArguments =
59+ params.map(KSPropertyDeclarationToIntentPutExtraString ()::transform)
60+ .joinToString(" \n " )
11161
11262 val paramsInString = params.map {
113- " $it "
63+ " $it : ${it.type} "
11464 }.joinToString()
11565
116- " fun get${className} Screen($paramsWithTypeInString ) = ActivityScreen {\n " +
117- " newIntent(it, $paramsInString )\n " +
118- " }\n "
66+ " fun newIntent(context: Context, $paramsInString ): Intent { \n " +
67+ " val intent = Intent(context, ${className} ::class.java)\n " +
68+ " $paramsForSetArguments \n " +
69+ " return intent\n " +
70+ " }\n "
11971 }
120- }
72+ )
12173
122- file + = " } \n "
74+ appendLine( " fun ${className} .initArguments() { " )
12375
124- val packageName = classDeclaration.containingFile!! .packageName.asString()
125- val importIndex = screenManagerClassStringBuilder.indexOf(" import" )
126- screenManagerClassStringBuilder.insert(
127- importIndex,
128- " import ${packageName} .${className} Screen.initArguments\n "
129- )
130- screenManagerClassStringBuilder.insert(importIndex, " import ${packageName} .$className \n " )
76+ val paramsForGetArguments = params.map(KsPropertyDeclarationToGetExtraString ()::transform)
77+ .joinToString(" \n " )
13178
132- if (screenManagerClassStringBuilder.contains(" // initArguments(Activities)" )) {
133- val body = buildString {
134- appendLine(" \r when (activity) {" )
135- append(" is $className -> activity.initArguments()" )
136- append(" }" )
137- }
138- screenManagerClassStringBuilder.replaceFirst(" // initArguments(Activities)" , body)
139- } else {
140- val body = buildString {
141- appendLine(" when (activity) {" )
142- append(" is $className -> activity.initArguments()" )
143- }
144- screenManagerClassStringBuilder.replaceFirst(" when (activity) {" , body)
79+ appendLine(paramsForGetArguments)
80+ appendLine(" }" )
81+
82+ if (generateScreenMethodArgument.value == true ) {
83+ appendLine()
84+ appendLine(
85+ if (params.toList().isEmpty()) {
86+ " fun get${className} Screen() = ActivityScreen { newIntent(it) }"
87+ } else {
88+
89+ val paramsWithTypeInString = params.map {
90+ " $it : ${it.type} "
91+ }.joinToString()
92+
93+ val paramsInString = params.map {
94+ " $it "
95+ }.joinToString()
96+
97+ " fun get${className} Screen($paramsWithTypeInString ) = ActivityScreen {\n " +
98+ " newIntent(it, $paramsInString )\n " +
99+ " }"
100+ }
101+ )
145102 }
146103 }
147104}
0 commit comments