Skip to content

Commit d6e6b34

Browse files
authored
Merge pull request #2 from PratyushSingh07/k2_working
enhancement: resolved CE errors & ran the sample project
2 parents d20cb09 + 0333f43 commit d6e6b34

File tree

6 files changed

+73
-40
lines changed

6 files changed

+73
-40
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: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.airbnb.deeplinkdispatch
1717

1818
import androidx.room.compiler.processing.XAnnotation
19+
import androidx.room.compiler.processing.XAnnotationValue
1920
import androidx.room.compiler.processing.XElement
2021
import androidx.room.compiler.processing.XExecutableParameterElement
2122
import androidx.room.compiler.processing.XFiler
@@ -34,6 +35,7 @@ import com.airbnb.deeplinkdispatch.base.Utils.isConfigurablePathSegment
3435
import com.airbnb.deeplinkdispatch.handler.DeepLinkParamType
3536
import com.airbnb.deeplinkdispatch.handler.DeeplinkParam
3637
import com.airbnb.deeplinkdispatch.handler.TypeConverters
38+
import com.google.devtools.ksp.processing.Resolver
3739
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
3840
import com.squareup.javapoet.ClassName
3941
import com.squareup.javapoet.CodeBlock
@@ -123,19 +125,31 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
123125
override fun process(
124126
annotations: Set<XTypeElement>?,
125127
environment: XProcessingEnv,
126-
round: XRoundEnv
128+
round: XRoundEnv,
129+
resolver: Resolver?,
127130
) {
128131
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+
129141
// If we run KSP or this is configured to be incremental we need to rely on the
130142
// incrementalMetadata for custom annotations. If not filter them out of the
131143
// set of annotations we were given.
132-
val customAnnotations = if (incrementalMetadata.incremental ||
144+
/*val customAnnotations = if (incrementalMetadata.incremental ||
133145
environment.backend == XProcessingEnv.Backend.KSP
134146
) {
135147
incrementalMetadata.customAnnotations
136148
} else {
137149
annotations?.filterAnnotatedAnnotations(DeepLinkSpec::class) ?: emptySet()
138150
}
151+
*/
152+
139153
val allDeepLinkAnnotatedElements =
140154
customAnnotations.flatMap { round.getElementsAnnotatedWith(it.qualifiedName) } +
141155
round.getElementsAnnotatedWith(DEEP_LINK_CLASS)
@@ -168,7 +182,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
168182
annotatedMethodElements = annotatedMethodElements,
169183
annotatedObjectElements = annotatedObjectElements,
170184
deepLinkElements = collectDeepLinkElements(
171-
prefixes = customAnnotationPrefixes(customAnnotations),
185+
prefixes = prefixes,
172186
classElementsToProcess = annotatedClassElements,
173187
objectElementsToProcess = annotatedObjectElements,
174188
methodElementsToProcess = annotatedMethodElements,
@@ -222,18 +236,21 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
222236
uri = uri,
223237
element = element
224238
)
239+
225240
element is XTypeElement && element.isActivity() ->
226241
DeepLinkAnnotatedElement.ActivityAnnotatedElement(
227242
uri = uri,
228243
element = element
229244
)
245+
230246
element is XTypeElement && element.isHandler() -> {
231247
verifyHandlerMatchArgs(element, uri)
232248
DeepLinkAnnotatedElement.HandlerAnnotatedElement(
233249
uri = uri,
234250
element = element
235251
)
236252
}
253+
237254
else -> error(
238255
"Internal error: Elements can only be 'MethodAnnotatedElement', " +
239256
"'ActivityAnnotatedElement' or 'HandlerAnnotatedElement'"
@@ -255,7 +272,8 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
255272
return getAllDeeplinkUrIsFromCustomDeepLinksOnElement(
256273
element = element,
257274
prefixesMap = prefixes
258-
) + (element.getAnnotation(DEEP_LINK_CLASS)?.value?.value?.toList() ?: emptyList())
275+
) + (element.getAnnotation(DEEP_LINK_CLASS)?.getAsStringList("value")?.toList()
276+
?: emptyList())
259277
}
260278

261279
private fun verifyCass(classElement: XTypeElement) {
@@ -331,6 +349,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
331349
)
332350
}
333351
val allArgParameters = argsConstructor.parameters
352+
334353
val allPathParameters = allArgParameters.filterAnnotationType(DeepLinkParamType.Path)
335354
val allQueryParameters = allArgParameters.filterAnnotationType(DeepLinkParamType.Query)
336355
if (allPathParameters.size + allQueryParameters.size != allArgParameters.size) {
@@ -344,7 +363,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
344363
val deepLinkUriTemplate = DeepLinkUri.parseTemplate(uriTemplate)
345364
val templateHostPathSchemePlaceholders = deepLinkUriTemplate.schemeHostPathPlaceholders
346365
val annotatedPathParameterNames = allPathParameters.mapNotNull {
347-
it.getAnnotation(DeeplinkParam::class)?.value?.name
366+
it.getAnnotation(DeeplinkParam::class)?.getAsString("name")
348367
}.toSet()
349368
val annotatedPathParametersThatAreNotInUrlTemplate =
350369
annotatedPathParameterNames.filter { !templateHostPathSchemePlaceholders.contains(it) }
@@ -360,14 +379,23 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
360379

361380
private fun List<XExecutableParameterElement>.filterAnnotationType(
362381
deepLinkParamType: DeepLinkParamType
363-
) =
364-
filter { argParameter ->
365-
argParameter.getAllAnnotations().find { annotation ->
366-
annotation.qualifiedName == DeeplinkParam::class.qualifiedName
367-
}?.annotationValues?.any { annotationValue ->
368-
annotationValue.value.toString() == deepLinkParamType.toString()
369-
} ?: false
370-
}
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+
}
371399

372400
private fun verifyObjectElement(element: XTypeElement) {
373401
if (!element.isHandler()) {
@@ -389,17 +417,20 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
389417
}
390418
}
391419

392-
private fun customAnnotationPrefixes(customAnnotations: Set<XTypeElement>): Map<XType, Array<String>> {
393-
return customAnnotations.map { customAnnotationTypeElement ->
420+
private fun customAnnotationPrefixes(customAnnotations: List<XTypeElement>): Map<XType, Array<String>> {
421+
return customAnnotations.associate { customAnnotationTypeElement ->
394422
if (!customAnnotationTypeElement.isAnnotationClass()) {
395423
logError(
396424
element = customAnnotationTypeElement,
397425
message = "Only annotation types can be annotated with @${DEEP_LINK_SPEC_CLASS.simpleName}"
398426
)
399427
}
400-
val prefix: Array<String> =
401-
customAnnotationTypeElement.getAnnotation(DEEP_LINK_SPEC_CLASS)
402-
?.let { it.value.prefix } ?: emptyArray()
428+
val prefix: Array<String> = customAnnotationTypeElement
429+
.getAnnotation(DEEP_LINK_SPEC_CLASS)
430+
?.getAsStringList("prefix")
431+
?.toTypedArray()
432+
?: emptyArray()
433+
403434
if (prefix.hasEmptyOrNullString()) {
404435
logError(
405436
element = customAnnotationTypeElement,
@@ -411,7 +442,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
411442
message = "Prefix property cannot be empty"
412443
)
413444
customAnnotationTypeElement.type to prefix
414-
}.toMap()
445+
}
415446
}
416447

417448
private fun verifyAnnotatedType(
@@ -443,7 +474,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
443474
logError(
444475
element = it.value.first().enclosingTypeElement,
445476
message = "Only one @DeepLinkHandler annotated element allowed per package!" +
446-
" ${it.key} has ${it.value.joinToString { it.qualifiedName }}.",
477+
" ${it.key} has ${it.value.joinToString { it.qualifiedName }}.",
447478
)
448479
}
449480
return false
@@ -727,6 +758,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
727758
?: ""
728759
)
729760
)
761+
730762
is DeepLinkAnnotatedElement.MethodAnnotatedElement ->
731763
urisTrie.addToTrie(
732764
DeepLinkEntry.MethodDeeplinkEntry(
@@ -736,6 +768,7 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
736768
method = element.method
737769
)
738770
)
771+
739772
is DeepLinkAnnotatedElement.HandlerAnnotatedElement ->
740773
urisTrie.addToTrie(
741774
DeepLinkEntry.HandlerDeepLinkEntry(
@@ -866,13 +899,13 @@ class DeepLinkProcessor(symbolProcessorEnvironment: SymbolProcessorEnvironment?
866899
prefixesMap: Map<XType, Array<String>>
867900
): List<String> {
868901
return element.findAnnotatedAnnotation<DeepLinkSpec>().flatMap { customAnnotation ->
869-
val suffixes = customAnnotation.getAsList<String>("value")
902+
val suffixes = customAnnotation.getAsList<XAnnotationValue>("value")
870903
val prefixes = prefixesMap[customAnnotation.type]
871904
?: throw DeepLinkProcessorException(
872905
"Unable to find annotation '${customAnnotation.qualifiedName}' you must " +
873-
"update 'deepLink.customAnnotations' within the build.gradle"
906+
"update 'deepLink.customAnnotations' within the build.gradle"
874907
)
875-
prefixes.flatMap { prefix -> suffixes.map { suffix -> prefix + suffix } }
908+
prefixes.flatMap { prefix -> suffixes.map { suffix -> prefix + suffix.asString() } }
876909
}
877910
}
878911

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,11 @@ fun XTypeElement.directlyImplementsInterfaces(fqnList: List<String>): Boolean {
5555
}
5656

5757
inline fun <reified T> XAnnotation.getAsList(method: String): List<T> {
58-
val originalList = get<List<T>>(method)
59-
// In new XProcessing versions List values are wrapped in XAnnotationValue but in old versions
60-
// they are the raw type.
61-
return if (originalList.firstOrNull() is XAnnotationValue) {
62-
// TODO: In the next full release of xprocessing we should be able to safely assume
63-
// the list type is always XAnnotationValue and can remove this if/else.
64-
(originalList as List<XAnnotationValue>).map { xAnnotationValue ->
65-
check(xAnnotationValue.value is T) {
66-
"Expected type ${T::class} but got ${xAnnotationValue.value?.javaClass}"
67-
}
68-
xAnnotationValue.value as T
69-
}
58+
val annotationValue = get(method)
59+
return if (annotationValue != null) {
60+
@Suppress("UNCHECKED_CAST")
61+
(annotationValue.value as? List<T>) ?: emptyList()
7062
} else {
71-
return originalList
63+
emptyList()
7264
}
7365
}

deeplinkdispatch/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,12 @@ android {
3535
testOptions {
3636
unitTests.returnDefaultValues = true
3737
}
38+
compileOptions {
39+
sourceCompatibility JavaVersion.VERSION_11
40+
targetCompatibility JavaVersion.VERSION_11
41+
}
42+
kotlinOptions {
43+
jvmTarget = '11'
44+
}
3845
namespace 'com.airbnb.android.deeplinkdispatch'
3946
}

dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def versions = [
1414

1515
ext.versions = versions
1616
ext.androidConfig = [
17-
agpVersion : '8.8.1',
17+
agpVersion : '8.11.1',
1818
compileSdkVersion : 32,
1919
minSdkVersion : 16,
2020
targetSdkVersion : 30

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)