@@ -38,50 +38,19 @@ class CommunicationKspSymbolProcessor(
3838 private val logger : KSPLogger
3939) : SymbolProcessor {
4040 override fun process (resolver : Resolver ): List <KSAnnotated > {
41- val ret1 = processImplementClass(resolver)
41+ val symbols =
42+ resolver.getSymbolsWithAnnotation(ImplementClass ::class .qualifiedName!! )
43+ val ret1 = processImplementClass(symbols)
44+ val ret3 = processExposeInterface(resolver,symbols)
4245 val ret2 = processExposeBean(resolver)
43- val ret3 = processExposeInterface(resolver)
4446 val ret = arrayListOf<KSAnnotated >()
4547 ret.addAll(ret1)
4648 ret.addAll(ret2)
4749 ret.addAll(ret3)
4850 return ret
4951 }
5052
51- // private fun processImplementClass(resolver: Resolver): List<KSAnnotated> {
52- // val symbols =
53- // resolver.getSymbolsWithAnnotation(ImplementClass::class.qualifiedName!!)
54- // for (symbol in symbols) {
55- // val annotationMap = getAnnotation(symbol)
56- // val classMethodMap: MutableMap<String, Any?> =
57- // annotationMap["@ImplementClass"] ?: continue
58- //
59- // val value: KSType? =
60- // if (classMethodMap["clazz"] != null) classMethodMap["clazz"] as KSType else null
61- // // val targetClassName: String =
62- // // (if (value != null) value.declaration.packageName.asString() + "." + value.toString() else null)
63- // // ?: continue
64- // val className = (symbol as KSClassDeclaration).packageName.asString() + "." + symbol
65- // val fileName = "${value.toString()}\$\$BindClass";
66- // val typeBuilder = TypeSpec.classBuilder(
67- // fileName
68- // ).addModifiers(KModifier.FINAL).addAnnotation(
69- // AnnotationSpec.builder(KeepClass::class)
70- // .addMember(
71- // "clazzName = %S",
72- // "@$className"
73- // )
74- // .build()
75- // )
76- //
77- // writeToFile(typeBuilder, fileName, symbol)
78- // }
79- // return symbols.filter { !it.validate() }.toList()
80- // }
81-
82- private fun processImplementClass (resolver : Resolver ): List <KSAnnotated > {
83- val symbols =
84- resolver.getSymbolsWithAnnotation(ImplementClass ::class .qualifiedName!! )
53+ private fun processImplementClass (symbols : Sequence <KSAnnotated >): List <KSAnnotated > {
8554 for (symbol in symbols) {
8655 val annotationMap = getAnnotation(symbol)
8756 val classMethodMap: MutableMap <String , Any ?> =
@@ -120,27 +89,6 @@ class CommunicationKspSymbolProcessor(
12089 }
12190 return symbols.filter { ! it.validate() }.toList()
12291 }
123- // private fun processExposeBean(resolver: Resolver): List<KSAnnotated> {
124- // val symbols =
125- // resolver.getSymbolsWithAnnotation(ExposeBean::class.qualifiedName!!)
126- // for (symbol in symbols) {
127- // if (symbol is KSClassDeclaration) {
128- // val filePath = (symbol.location as FileLocation).filePath;
129- // val encodePath = encodeURLComponent(filePath)
130- //
131- // val typeBuilder = TypeSpec.classBuilder(
132- // encodePath
133- // ).addModifiers(KModifier.FINAL)
134- // writeToFile2(typeBuilder,encodePath, symbol.packageName.asString(), symbol)
135- // }
136- //
137- // }
138- // return symbols.filter { !it.validate() }.toList()
139- // }
140- //
141- // private fun encodeURLComponent(url: String?): String {
142- // return URLEncoder.encode(url, "UTF-8")
143- // }
14492 private fun processExposeBean (resolver : Resolver ): List <KSAnnotated > {
14593 val symbols =
14694 resolver.getSymbolsWithAnnotation(ExposeBean ::class .qualifiedName!! )
@@ -159,11 +107,17 @@ class CommunicationKspSymbolProcessor(
159107 return symbols.filter { ! it.validate() }.toList()
160108 }
161109
162- private fun processExposeInterface (resolver : Resolver ): List <KSAnnotated > {
110+ private fun processExposeInterface (resolver : Resolver , implementSymbols : Sequence < KSAnnotated > ): List <KSAnnotated > {
163111 val symbols =
164112 resolver.getSymbolsWithAnnotation(ExposeInterface ::class .qualifiedName!! )
165113 for (symbol in symbols) {
166114 if (symbol is KSClassDeclaration ) {
115+ val className = symbol.packageName.asString() + " ." + symbol
116+ val isContain = isContainImplementClass(implementSymbols,className)
117+
118+ if (! isContain){
119+ throw IllegalArgumentException (" 注意: $symbol 没有相应的实现类" )
120+ }
167121
168122 val file = File ((symbol.location as FileLocation ).filePath)
169123
@@ -176,41 +130,28 @@ class CommunicationKspSymbolProcessor(
176130 }
177131 return symbols.filter { ! it.validate() }.toList()
178132 }
179- // private fun processExposeClass(resolver: Resolver): List<KSAnnotated> {
180- // val symbols =
181- // resolver.getSymbolsWithAnnotation(ExposeClass::class.qualifiedName!!)
182- // val implementClassSymbols =
183- // resolver.getSymbolsWithAnnotation(ImplementClass::class.qualifiedName!!)
184- // for (symbol in symbols) {
185- // val annotationMap = getAnnotation(symbol)
186- // val classMethodMap: MutableMap<String, Any?> =
187- // annotationMap["@ExposeClass"] ?: continue
188- //
189- // if (symbol is KSClassDeclaration) {
190- // val className = symbol.packageName.asString() + "." + symbol
191- // val implementClassName = classMethodMap["clazzName"] as String
192- // if (implementClassName.isNotEmpty()) {
193- // val contain = isContainImplementClass(
194- // implementClassSymbols,
195- // implementClassName,
196- // className
197- // )
198- // if (!contain) {
199- // throw IllegalArgumentException("注意:请给 $symbol 设置的实现类 $implementClassName,设置 @ImplementClass 注解")
200- // }
201- // }
202- //
203- // val file = File((symbol.location as FileLocation).filePath)
204- //
205- // val fileName =
206- // "${symbol}${file.absolutePath.substring(file.absolutePath.lastIndexOf("."))}";
207- //
208- // writeToFile(fileName, symbol, symbol.packageName.asString(), file)
209- // }
210- //
211- // }
212- // return symbols.filter { !it.validate() }.toList()
213- // }
133+
134+ private fun isContainImplementClass (
135+ symbols : Sequence <KSAnnotated >,
136+ className : String
137+ ): Boolean {
138+ // logger.error("className = $className ,symbols= ${symbols}")
139+ var isContainImplementClass = false
140+ for (symbol in symbols) {
141+ val annotationMap = getAnnotation(symbol)
142+ val classMethodMap: MutableMap <String , Any ?> =
143+ annotationMap[" @ImplementClass" ] ? : continue
144+
145+ val value: KSType ? =
146+ if (classMethodMap[" clazz" ] != null ) classMethodMap[" clazz" ] as KSType else null
147+ val targetClassName: String? =
148+ (if (value != null ) value.declaration.packageName.asString() + " ." + value.toString() else null )
149+ if (targetClassName == className){
150+ isContainImplementClass = true
151+ }
152+ }
153+ return isContainImplementClass
154+ }
214155
215156 private fun writeToFile (
216157 fileName : String , symbol : KSAnnotated , packageName : String , file : File
@@ -227,34 +168,6 @@ class CommunicationKspSymbolProcessor(
227168 }
228169
229170 }
230- private fun isContainImplementClass (
231- symbols : Sequence <KSAnnotated >,
232- implementClassName : String ,
233- className : String
234- ): Boolean {
235- var isContainImplementClass = false
236- for (symbol in symbols) {
237- if (symbol is KSClassDeclaration ) {
238- val thisName = symbol.packageName.asString() + " ." + symbol
239- if (thisName == implementClassName) {
240-
241- val typeList = symbol.superTypes.toList()
242- for (ksTypeReference in typeList) {
243- val superClassName =
244- ksTypeReference.resolve().declaration.packageName.asString() + " ." + ksTypeReference
245- if (superClassName == className) {
246- isContainImplementClass = true
247- break
248- }
249- }
250- if (! isContainImplementClass){
251- throw IllegalArgumentException (" 注意:实现类 $implementClassName ,没有继承 $className " )
252- }
253- }
254- }
255- }
256- return isContainImplementClass
257- }
258171
259172 private fun isImplementClass (
260173 symbol : KSAnnotated ,
@@ -299,27 +212,6 @@ class CommunicationKspSymbolProcessor(
299212 private fun whatsMyName (name : String ): FunSpec .Builder {
300213 return FunSpec .builder(name).addModifiers(KModifier .FINAL )
301214 }
302- private fun writeToFile2 (
303- typeBuilder : TypeSpec .Builder ,
304- fileName : String ,
305- packageName : String ,
306- symbol : KSAnnotated
307- ) {
308- val typeSpec = typeBuilder.build()
309- val kotlinFile = FileSpec .builder(CommunicationPackage .BIND_CLASS_PACKAGE , fileName).addType(typeSpec)
310- .build()
311- codeGenerator
312- .createNewFile(
313- Dependencies (false , symbol.containingFile!! ),
314- packageName,
315- fileName,
316- " api"
317- )
318- .writer()
319- .use {
320- kotlinFile.writeTo(it)
321- }
322- }
323215 private fun writeToFile (
324216 typeBuilder : TypeSpec .Builder ,
325217 fileName : String ,
0 commit comments