@@ -5,9 +5,11 @@ import me.eugeniomarletti.kotlin.metadata.KotlinClassMetadata
5
5
import me.eugeniomarletti.kotlin.metadata.KotlinMetadata
6
6
import me.eugeniomarletti.kotlin.metadata.KotlinMetadataUtils
7
7
import me.eugeniomarletti.kotlin.metadata.KotlinPackageMetadata
8
- import me.eugeniomarletti.kotlin.metadata.getJvmMethodSignature
8
+ import me.eugeniomarletti.kotlin.metadata.extractFullName
9
+ import me.eugeniomarletti.kotlin.metadata.getPropertyOrNull
10
+ import me.eugeniomarletti.kotlin.metadata.getValueParameterOrNull
11
+ import me.eugeniomarletti.kotlin.metadata.jvm.getJvmMethodSignature
9
12
import me.eugeniomarletti.kotlin.metadata.kotlinMetadata
10
- import me.eugeniomarletti.kotlin.metadata.kotlinPropertyAnnotationsFunPostfix
11
13
import org.jetbrains.kotlin.serialization.ProtoBuf
12
14
import javax.lang.model.element.Element
13
15
import javax.lang.model.element.ExecutableElement
@@ -31,71 +33,33 @@ interface ProcessorUtils : KotlinMetadataUtils {
31
33
}
32
34
33
35
fun ClassOrPackageDataWrapper.getFunction (methodElement : ExecutableElement ) =
34
- methodElement.jvmMethodSignature.let { methodSignature ->
35
- functionList
36
- .firstOrNull { methodSignature == it.getJvmMethodSignature(nameResolver) }
37
- ? : knownError(" Can't find annotated method $methodSignature " )
38
- }
36
+ getFunctionOrNull(methodElement, nameResolver, functionList)
37
+ ? : knownError(" Can't find annotated method ${methodElement.jvmMethodSignature} " )
39
38
}
40
39
41
40
fun knownError (message : String , element : Element ? = null): Nothing = throw KnownException (message, element)
42
41
43
- fun String.plusIfNotBlank (
44
- postfix : String = "",
45
- prefix : String = ""
46
- ) = if (this .isNotBlank()) prefix + this + postfix else this
47
-
48
- val String .escapedClassName
49
- get() = split(' /' , ' .' ).joinToString(" `.`" ).plusIfNotBlank(prefix = " `" , postfix = " `" )
50
-
51
42
val ProtoBuf .Class .Kind .isCompanionOrObject get() = when (this ) {
52
43
ProtoBuf .Class .Kind .OBJECT ,
53
44
ProtoBuf .Class .Kind .COMPANION_OBJECT -> true
54
45
else -> false
55
46
}
56
47
57
48
fun ClassOrPackageDataWrapper.getParameter (function : ProtoBuf .Function , parameterElement : VariableElement ) =
58
- parameterElement.simpleName.toString().let { parameterName ->
59
- function.valueParameterList
60
- .firstOrNull { parameterName == nameResolver.getString(it.name) }
61
- ? : knownError(" Can't find annotated parameter $parameterName in ${function.getJvmMethodSignature(nameResolver)} " )
62
- }
49
+ getValueParameterOrNull(nameResolver, function, parameterElement)
50
+ ? : knownError(" Can't find annotated parameter ${parameterElement.simpleName} in ${function.getJvmMethodSignature(nameResolver)} " )
63
51
64
52
fun ClassOrPackageDataWrapper.getPropertyOrNull (methodElement : ExecutableElement ) =
65
- methodElement.simpleName.toString()
66
- .takeIf { it.endsWith(kotlinPropertyAnnotationsFunPostfix) }
67
- ?.substringBefore(kotlinPropertyAnnotationsFunPostfix)
68
- ?.let { propertyName -> propertyList.firstOrNull { propertyName == nameResolver.getString(it.name) } }
53
+ getPropertyOrNull(methodElement, nameResolver, this ::propertyList)
69
54
70
55
fun ProtoBuf.Type.extractFullName (
71
56
classData : ClassOrPackageDataWrapper ,
72
57
outputTypeAlias : Boolean = true,
73
58
failOnGeneric : Boolean = true
74
- ): String {
75
- val nameResolver = classData.nameResolver
76
-
77
- if (failOnGeneric && ! hasClassName()) knownError(" Generic $implicitAnnotationName types are not yet supported" )
78
-
79
- val name = when {
80
- hasTypeParameter() -> classData.getTypeParameter(typeParameter)!! .name
81
- hasTypeParameterName() -> typeParameterName
82
- outputTypeAlias && hasAbbreviatedType() -> abbreviatedType.typeAliasName
83
- else -> className
84
- }.let { nameResolver.getString(it).escapedClassName }
85
-
86
- val argumentList = if (outputTypeAlias && hasAbbreviatedType()) abbreviatedType.argumentList else argumentList
87
- val arguments = argumentList
88
- .takeIf { it.isNotEmpty() }
89
- ?.joinToString(prefix = " <" , postfix = " >" ) {
90
- when {
91
- it.hasType() -> it.type.extractFullName(classData, outputTypeAlias, failOnGeneric)
92
- ! failOnGeneric -> " *"
93
- else -> knownError(" Wildcard $implicitAnnotationName types are not yet supported" )
94
- }
95
- }
96
- ? : " "
97
-
98
- val nullability = if (nullable) " ?" else " "
99
-
100
- return name + arguments + nullability
101
- }
59
+ ): String =
60
+ extractFullName(
61
+ nameResolver = classData.nameResolver,
62
+ getTypeParameter = { classData.getTypeParameter(it)!! },
63
+ outputTypeAlias = outputTypeAlias,
64
+ throwOnGeneric = if (! failOnGeneric) null else KnownException (" Generic $implicitAnnotationName types are not yet supported" , null )
65
+ )
0 commit comments