-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify
TypeAliasTag
API between metadata and KSP (#1152)
* Extract TypeAliasTag for reuse and simplify API in KSP * Update TestProcessor implementation for new alias API This is based on the implementation for metadata in Moshi, and possibly worth promoting as a util to KotlinPoet itself * I am once again asking IntelliJ to actually rename imports * Spotless
- Loading branch information
Showing
8 changed files
with
144 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/tags.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ocessor/src/main/kotlin/com/squareup/kotlinpoet/ksp/test/processor/typeAliasUnwrapping.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.squareup.kotlinpoet.ksp.test.processor | ||
|
||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.LambdaTypeName | ||
import com.squareup.kotlinpoet.ParameterizedTypeName | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeVariableName | ||
import com.squareup.kotlinpoet.WildcardTypeName | ||
import com.squareup.kotlinpoet.tag | ||
import com.squareup.kotlinpoet.tags.TypeAliasTag | ||
import java.util.TreeSet | ||
|
||
/* | ||
* Example implementation of how to unwrap a typealias from TypeNameAliasTag | ||
*/ | ||
|
||
internal fun TypeName.unwrapTypeAliasReal(): TypeName { | ||
return tag<TypeAliasTag>()?.abbreviatedType?.let { unwrappedType -> | ||
// If any type is nullable, then the whole thing is nullable | ||
var isAnyNullable = isNullable | ||
// Keep track of all annotations across type levels. Sort them too for consistency. | ||
val runningAnnotations = TreeSet<AnnotationSpec>(compareBy { it.toString() }).apply { | ||
addAll(annotations) | ||
} | ||
val nestedUnwrappedType = unwrappedType.unwrapTypeAlias() | ||
runningAnnotations.addAll(nestedUnwrappedType.annotations) | ||
isAnyNullable = isAnyNullable || nestedUnwrappedType.isNullable | ||
nestedUnwrappedType.copy(nullable = isAnyNullable, annotations = runningAnnotations.toList()) | ||
} ?: this | ||
} | ||
|
||
// TypeVariableName gets a special overload because these usually need to be kept in a type-safe | ||
// manner. | ||
internal fun TypeVariableName.unwrapTypeAlias(): TypeVariableName { | ||
return TypeVariableName( | ||
name = name, | ||
bounds = bounds.map { it.unwrapTypeAlias() }, | ||
variance = variance | ||
) | ||
.copy(nullable = isNullable, annotations = annotations, tags = tags) | ||
} | ||
|
||
internal fun TypeName.unwrapTypeAlias(): TypeName { | ||
return when (this) { | ||
is ClassName -> unwrapTypeAliasReal() | ||
is ParameterizedTypeName -> unwrapTypeAliasReal() | ||
is TypeVariableName -> unwrapTypeAlias() | ||
is WildcardTypeName -> unwrapTypeAliasReal() | ||
is LambdaTypeName -> unwrapTypeAliasReal() | ||
else -> throw UnsupportedOperationException("Type '${javaClass.simpleName}' is illegal. Only classes, parameterized types, wildcard types, or type variables are allowed.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
kotlinpoet/src/main/java/com/squareup/kotlinpoet/tags/TypeAliasTag.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.squareup.kotlinpoet.tags | ||
|
||
import com.squareup.kotlinpoet.TypeName | ||
|
||
/** | ||
* This tag indicates that this [TypeName] represents a `typealias` type. | ||
* | ||
* @property [abbreviatedType] the underlying type for this alias. | ||
*/ | ||
public class TypeAliasTag(public val abbreviatedType: TypeName) |