Skip to content

Commit 0333f43

Browse files
ran the sample project
1 parent 71a6d68 commit 0333f43

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

deeplinkdispatch-processor/src/main/java/com/airbnb/deeplinkdispatch/BaseProcessor.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ abstract class BaseProcessor(val symbolProcessorEnvironment: SymbolProcessorEnvi
5151
symbolProcessorEnvironment,
5252
resolver,
5353
)
54-
process(null, environment, XRoundEnv.create(environment))
54+
process(null, environment, XRoundEnv.create(environment), resolver)
5555
return emptyList()
5656
}
5757

5858
abstract fun process(
5959
annotations: Set<XTypeElement>?,
6060
environment: XProcessingEnv,
61-
round: XRoundEnv
61+
round: XRoundEnv,
62+
resolver: Resolver? = null,
6263
)
6364
}

deeplinkdispatch-processor/src/main/java/com/airbnb/deeplinkdispatch/DeepLinkProcessor.kt

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.airbnb.deeplinkdispatch.base.Utils.isConfigurablePathSegment
3535
import com.airbnb.deeplinkdispatch.handler.DeepLinkParamType
3636
import com.airbnb.deeplinkdispatch.handler.DeeplinkParam
3737
import com.airbnb.deeplinkdispatch.handler.TypeConverters
38+
import com.google.devtools.ksp.processing.Resolver
3839
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
3940
import com.squareup.javapoet.ClassName
4041
import com.squareup.javapoet.CodeBlock
@@ -124,19 +125,31 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
124125
override fun process(
125126
annotations: Set<XTypeElement>?,
126127
environment: XProcessingEnv,
127-
round: XRoundEnv
128+
round: XRoundEnv,
129+
resolver: Resolver?,
128130
) {
129131
try {
132+
// source -> https://github.com/google/ksp/issues/2225
133+
val customAnnotations = resolver
134+
?.getSymbolsWithAnnotation(DEEP_LINK_SPEC_CLASS.simpleName ?: "")
135+
?.filterIsInstance<XTypeElement>()
136+
?.toList() ?: emptyList()
137+
138+
val prefixes = customAnnotationPrefixes(customAnnotations)
139+
140+
130141
// If we run KSP or this is configured to be incremental we need to rely on the
131142
// incrementalMetadata for custom annotations. If not filter them out of the
132143
// set of annotations we were given.
133-
val customAnnotations = if (incrementalMetadata.incremental ||
144+
/*val customAnnotations = if (incrementalMetadata.incremental ||
134145
environment.backend == XProcessingEnv.Backend.KSP
135146
) {
136147
incrementalMetadata.customAnnotations
137148
} else {
138149
annotations?.filterAnnotatedAnnotations(DeepLinkSpec::class) ?: emptySet()
139150
}
151+
*/
152+
140153
val allDeepLinkAnnotatedElements =
141154
customAnnotations.flatMap { round.getElementsAnnotatedWith(it.qualifiedName) } +
142155
round.getElementsAnnotatedWith(DEEP_LINK_CLASS)
@@ -169,7 +182,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
169182
annotatedMethodElements = annotatedMethodElements,
170183
annotatedObjectElements = annotatedObjectElements,
171184
deepLinkElements = collectDeepLinkElements(
172-
prefixes = customAnnotationPrefixes(customAnnotations),
185+
prefixes = prefixes,
173186
classElementsToProcess = annotatedClassElements,
174187
objectElementsToProcess = annotatedObjectElements,
175188
methodElementsToProcess = annotatedMethodElements,
@@ -223,18 +236,21 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
223236
uri = uri,
224237
element = element
225238
)
239+
226240
element is XTypeElement && element.isActivity() ->
227241
DeepLinkAnnotatedElement.ActivityAnnotatedElement(
228242
uri = uri,
229243
element = element
230244
)
245+
231246
element is XTypeElement && element.isHandler() -> {
232247
verifyHandlerMatchArgs(element, uri)
233248
DeepLinkAnnotatedElement.HandlerAnnotatedElement(
234249
uri = uri,
235250
element = element
236251
)
237252
}
253+
238254
else -> error(
239255
"Internal error: Elements can only be 'MethodAnnotatedElement', " +
240256
"'ActivityAnnotatedElement' or 'HandlerAnnotatedElement'"
@@ -256,7 +272,8 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
256272
return getAllDeeplinkUrIsFromCustomDeepLinksOnElement(
257273
element = element,
258274
prefixesMap = prefixes
259-
) + (element.getAnnotation(DEEP_LINK_CLASS)?.getAsStringList("value")?.toList() ?: emptyList())
275+
) + (element.getAnnotation(DEEP_LINK_CLASS)?.getAsStringList("value")?.toList()
276+
?: emptyList())
260277
}
261278

262279
private fun verifyCass(classElement: XTypeElement) {
@@ -332,6 +349,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
332349
)
333350
}
334351
val allArgParameters = argsConstructor.parameters
352+
335353
val allPathParameters = allArgParameters.filterAnnotationType(DeepLinkParamType.Path)
336354
val allQueryParameters = allArgParameters.filterAnnotationType(DeepLinkParamType.Query)
337355
if (allPathParameters.size + allQueryParameters.size != allArgParameters.size) {
@@ -361,14 +379,23 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
361379

362380
private fun List<XExecutableParameterElement>.filterAnnotationType(
363381
deepLinkParamType: DeepLinkParamType
364-
) =
365-
filter { argParameter ->
366-
argParameter.getAllAnnotations().find { annotation ->
367-
annotation.qualifiedName == DeeplinkParam::class.qualifiedName
368-
}?.annotationValues?.any { annotationValue ->
369-
annotationValue.value.toString() == deepLinkParamType.toString()
370-
} ?: false
371-
}
382+
) = filter { param ->
383+
val deeplinkAnn = param.getAllAnnotations()
384+
.firstOrNull { it.qualifiedName == DeeplinkParam::class.qualifiedName }
385+
?: return@filter false
386+
387+
val enumArgValue = deeplinkAnn.annotationValues
388+
.firstOrNull { it.name == "type" }
389+
?.value
390+
?.let { v ->
391+
when (v) {
392+
is Enum<*> -> v.name
393+
else -> v.toString().substringAfterLast('.')
394+
}
395+
}
396+
397+
enumArgValue == deepLinkParamType.name
398+
}
372399

373400
private fun verifyObjectElement(element: XTypeElement) {
374401
if (!element.isHandler()) {
@@ -390,17 +417,20 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
390417
}
391418
}
392419

393-
private fun customAnnotationPrefixes(customAnnotations: Set<XTypeElement>): Map<XType, Array<String>> {
394-
return customAnnotations.map { customAnnotationTypeElement ->
420+
private fun customAnnotationPrefixes(customAnnotations: List<XTypeElement>): Map<XType, Array<String>> {
421+
return customAnnotations.associate { customAnnotationTypeElement ->
395422
if (!customAnnotationTypeElement.isAnnotationClass()) {
396423
logError(
397424
element = customAnnotationTypeElement,
398425
message = "Only annotation types can be annotated with @${DEEP_LINK_SPEC_CLASS.simpleName}"
399426
)
400427
}
401-
val prefix: Array<String> =
402-
customAnnotationTypeElement.getAnnotation(DEEP_LINK_SPEC_CLASS)
403-
?.getAsStringList("prefix")?.toTypedArray() ?: emptyArray()
428+
val prefix: Array<String> = customAnnotationTypeElement
429+
.getAnnotation(DEEP_LINK_SPEC_CLASS)
430+
?.getAsStringList("prefix")
431+
?.toTypedArray()
432+
?: emptyArray()
433+
404434
if (prefix.hasEmptyOrNullString()) {
405435
logError(
406436
element = customAnnotationTypeElement,
@@ -412,7 +442,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
412442
message = "Prefix property cannot be empty"
413443
)
414444
customAnnotationTypeElement.type to prefix
415-
}.toMap()
445+
}
416446
}
417447

418448
private fun verifyAnnotatedType(
@@ -444,7 +474,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
444474
logError(
445475
element = it.value.first().enclosingTypeElement,
446476
message = "Only one @DeepLinkHandler annotated element allowed per package!" +
447-
" ${it.key} has ${it.value.joinToString { it.qualifiedName }}.",
477+
" ${it.key} has ${it.value.joinToString { it.qualifiedName }}.",
448478
)
449479
}
450480
return false
@@ -728,6 +758,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
728758
?: ""
729759
)
730760
)
761+
731762
is DeepLinkAnnotatedElement.MethodAnnotatedElement ->
732763
urisTrie.addToTrie(
733764
DeepLinkEntry.MethodDeeplinkEntry(
@@ -737,6 +768,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
737768
method = element.method
738769
)
739770
)
771+
740772
is DeepLinkAnnotatedElement.HandlerAnnotatedElement ->
741773
urisTrie.addToTrie(
742774
DeepLinkEntry.HandlerDeepLinkEntry(
@@ -871,7 +903,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
871903
val prefixes = prefixesMap[customAnnotation.type]
872904
?: throw DeepLinkProcessorException(
873905
"Unable to find annotation '${customAnnotation.qualifiedName}' you must " +
874-
"update 'deepLink.customAnnotations' within the build.gradle"
906+
"update 'deepLink.customAnnotations' within the build.gradle"
875907
)
876908
prefixes.flatMap { prefix -> suffixes.map { suffix -> prefix + suffix.asString() } }
877909
}

0 commit comments

Comments
 (0)