Skip to content

Commit abe09e6

Browse files
jaschdocKSP Auto Pick
authored andcommitted
Refactor errors into InternalKSPException
(cherry picked from commit 8f17650)
1 parent 96655c5 commit abe09e6

1 file changed

Lines changed: 164 additions & 39 deletions

File tree

kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/common/impl/PsiResolutionStrategy.kt

Lines changed: 164 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.google.devtools.ksp.common.impl
1919

20+
import com.google.devtools.ksp.InternalKSPException
2021
import com.google.devtools.ksp.common.visitor.CollectAnnotatedSymbolsPsiVisitor
2122
import com.google.devtools.ksp.containingFile
2223
import com.google.devtools.ksp.impl.FileCache
@@ -209,14 +210,22 @@ class PsiResolutionStrategy(
209210
this[anno.shortName] = lazy {
210211
mutableSetOf(
211212
anno.qualifiedName
212-
?: error("Unexpected unqualified name at ${anno.toLocation()}: ${anno.javaClass}")
213+
?: throw InternalKSPException(
214+
"Unexpected unqualified name",
215+
anno.toLocation(),
216+
anno.javaClass
217+
)
213218
)
214219
}
215220
} else {
216221
this[anno.shortName] = lazy {
217222
lazyFullyQualifiedNames.value.add(
218223
anno.qualifiedName
219-
?: error("Unexpected unqualified name at ${anno.toLocation()}: ${anno.javaClass}")
224+
?: throw InternalKSPException(
225+
"Unexpected unqualified name",
226+
anno.toLocation(),
227+
anno.javaClass
228+
)
220229
)
221230
lazyFullyQualifiedNames.value
222231
}
@@ -369,7 +378,11 @@ class PsiResolutionStrategy(
369378
// Kotlin sources
370379
is KtDeclaration -> {
371380
if (annotation == null) {
372-
error("Unexpected null annotation at ${toLocation()} : $javaClass")
381+
throw InternalKSPException(
382+
"Unexpected null annotation",
383+
toLocation(),
384+
javaClass
385+
)
373386
}
374387
element.resolve(annotation)
375388
}
@@ -397,7 +410,11 @@ class PsiResolutionStrategy(
397410
listOf(element.resolve())
398411

399412
else ->
400-
error("Unreachable: ${element.javaClass}")
413+
throw InternalKSPException(
414+
"Unreachable branch",
415+
element.toLocation(),
416+
element.javaClass
417+
)
401418
}
402419

403420
/**
@@ -420,10 +437,10 @@ class PsiResolutionStrategy(
420437
*/
421438
private fun PsiParameter.resolve(): KSValueParameter {
422439
val functionDecl = callableSymbol.toKSFunctionDeclaration()
423-
?: error(
424-
"Failed to convert callable symbol to KSFunctionDeclaration at " +
425-
"${toLocation()}: " +
426-
"${callableSymbol.javaClass}"
440+
?: throw InternalKSPException(
441+
"Failed to convert callable symbol to KSFunctionDeclaration",
442+
toLocation(),
443+
callableSymbol.javaClass
427444
)
428445
return functionDecl.parameters[parameterIndex()]
429446
}
@@ -439,15 +456,23 @@ class PsiResolutionStrategy(
439456
resolveTypeParameterOfClass(decl)
440457

441458
else ->
442-
error("Unexpected Java declaration at ${decl.toLocation()}: ${decl.javaClass}")
459+
throw InternalKSPException(
460+
"Unexpected Java declaration",
461+
decl.toLocation(),
462+
decl.javaClass
463+
)
443464
}
444465

445466
/**
446467
* Resolves the [KSClassDeclarationImpl] corresponding to this [PsiClass].
447468
*/
448469
private fun PsiClass.resolve(): KSClassDeclarationImpl {
449470
val sym = analyze { namedClassSymbol }
450-
?: error("Unexpected null named class symbol at ${toLocation()}: $javaClass")
471+
?: throw InternalKSPException(
472+
"Unexpected null named class symbol",
473+
toLocation(),
474+
javaClass
475+
)
451476
return sym.toKSClassDeclaration()
452477
}
453478

@@ -456,19 +481,35 @@ class PsiResolutionStrategy(
456481
*/
457482
private fun PsiField.resolve(): KSPropertyDeclaration {
458483
val sym = analyze { this@resolve.callableSymbol }
459-
?: error("Unexpected null callable symbol at ${toLocation()}: $javaClass")
484+
?: throw InternalKSPException(
485+
"Unexpected null callable symbol",
486+
toLocation(),
487+
javaClass
488+
)
460489
return sym.toKSPropertyDeclaration()
461-
?: error("Unexpected null KSPropertyDeclaration at ${toLocation()}: $javaClass")
490+
?: throw InternalKSPException(
491+
"Unexpected null KSPropertyDeclaration",
492+
toLocation(),
493+
javaClass
494+
)
462495
}
463496

464497
/**
465498
* Resolves the [KSFunctionDeclarationImpl] corresponding to this [PsiMethod].
466499
*/
467500
private fun PsiMethod.resolve(): KSFunctionDeclarationImpl {
468501
val sym = analyze { this@resolve.callableSymbol }
469-
?: error("Unexpected null callable symbol at ${toLocation()}: $javaClass")
502+
?: throw InternalKSPException(
503+
"Unexpected null callable symbol",
504+
toLocation(),
505+
javaClass
506+
)
470507
return sym.toKSFunctionDeclaration()
471-
?: error("Unexpected null KSFunctionDeclaration at ${toLocation()}: $javaClass")
508+
?: throw InternalKSPException(
509+
"Unexpected null KSFunctionDeclaration",
510+
toLocation(),
511+
javaClass
512+
)
472513
}
473514

474515
/**
@@ -484,12 +525,20 @@ class PsiResolutionStrategy(
484525
is KSFunctionDeclarationImpl -> when {
485526
this.isGetter -> listOf(
486527
this.parentDeclaration?.getter()
487-
?: error("Missing getter $location: $javaClass")
528+
?: throw InternalKSPException(
529+
"Unexpected missing getter",
530+
location,
531+
javaClass
532+
)
488533
)
489534

490535
this.isSetter -> listOf(
491536
this.parentDeclaration?.setter()
492-
?: error("Missing setter $location: $javaClass")
537+
?: throw InternalKSPException(
538+
"Unexpected missing setter",
539+
location,
540+
javaClass
541+
)
493542
)
494543

495544
else -> listOf(this)
@@ -502,21 +551,33 @@ class PsiResolutionStrategy(
502551
is KSFile -> listOf(this)
503552
else -> listOf(
504553
this.containingFile
505-
?: error("Missing file at $location: $javaClass")
554+
?: throw InternalKSPException(
555+
"Unexpected missing file",
556+
location,
557+
javaClass
558+
)
506559
)
507560
}
508561

509562
AnnotationUseSiteTarget.PROPERTY -> when (this) {
510563
is KSValueParameter -> listOf(
511-
this.getGeneratedProperty() ?: error("Missing property for parameter at $location: $javaClass")
564+
this.getGeneratedProperty() ?: throw InternalKSPException(
565+
"Unexpected missing property for parameter",
566+
location,
567+
javaClass
568+
)
512569
)
513570

514571
else -> listOf(this)
515572
}
516573

517574
AnnotationUseSiteTarget.FIELD -> when (this) {
518575
is KSValueParameter -> listOf(
519-
this.getGeneratedProperty() ?: error("Missing property for parameter at $location: $javaClass")
576+
this.getGeneratedProperty() ?: throw InternalKSPException(
577+
"Unexpected missing property for parameter",
578+
location,
579+
javaClass
580+
)
520581
)
521582

522583
else -> listOf(this)
@@ -525,48 +586,84 @@ class PsiResolutionStrategy(
525586
AnnotationUseSiteTarget.GET ->
526587
listOf(
527588
this.getter()
528-
?: error("Missing getter at $location: $javaClass")
589+
?: throw InternalKSPException(
590+
"Unexpected missing getter",
591+
location,
592+
javaClass
593+
)
529594
)
530595

531596
AnnotationUseSiteTarget.SET ->
532597
listOf(
533598
this.setter()
534-
?: error("Missing setter at $location: $javaClass")
599+
?: throw InternalKSPException(
600+
"Unexpected missing setter",
601+
location,
602+
javaClass
603+
)
535604
)
536605

537606
AnnotationUseSiteTarget.RECEIVER -> when (this) {
538607
is KSFunctionDeclarationImpl -> listOf(
539608
extensionReceiver
540-
?: error("Missing extension receiver at $location: $javaClass")
609+
?: throw InternalKSPException(
610+
"Unexpected missing extension receiver",
611+
location,
612+
javaClass
613+
)
541614
)
542615

543616
is KSPropertyDeclaration -> listOf(
544617
extensionReceiver
545-
?: error("Missing extension receiver at $location: $javaClass")
618+
?: throw InternalKSPException(
619+
"Unexpected missing extension receiver",
620+
location,
621+
javaClass
622+
)
546623
)
547624

548-
else -> error("Unexpected declaration at $location: $javaClass")
625+
else -> throw InternalKSPException(
626+
"Unexpected declaration",
627+
location,
628+
javaClass
629+
)
549630
}
550631

551632
AnnotationUseSiteTarget.PARAM -> when (this) {
552633
is KSValueParameter -> listOf(this)
553-
else -> error("Unexpected annotated symbol with param use-site target at $location: $javaClass")
634+
else -> throw InternalKSPException(
635+
"Unexpected annotated symbol with param use-site target",
636+
location,
637+
javaClass
638+
)
554639
}
555640

556641
AnnotationUseSiteTarget.SETPARAM -> when (this) {
557642
is KSValueParameter -> {
558643
listOf(
559644
(this.parent as? KSFunctionDeclaration)?.parameters?.singleOrNull()
560-
?: error("Missing setter parameter at $location: $javaClass")
645+
?: throw InternalKSPException(
646+
"Unexpected missing setter parameter",
647+
location,
648+
javaClass
649+
)
561650
)
562651
}
563652

564653
is KSPropertyDeclaration -> listOf(
565654
setter?.parameter
566-
?: error("Missing setter parameter at $location: $javaClass")
655+
?: throw InternalKSPException(
656+
"Unexpected missing setter parameter",
657+
location,
658+
javaClass
659+
)
567660
)
568661

569-
else -> error("Unexpected annotated symbol with 'setparam' use-site target at $location: $javaClass")
662+
else -> throw InternalKSPException(
663+
"Unexpected annotated symbol with 'setparam' use-site target",
664+
location,
665+
javaClass
666+
)
570667
}
571668

572669
AnnotationUseSiteTarget.DELEGATE ->
@@ -594,7 +691,11 @@ class PsiResolutionStrategy(
594691
addIfNotNull(this@findTargetedSymbol.setter?.parameter)
595692
}
596693

597-
else -> error("Unexpected annotated symbol with 'all' use-site target at $location: $javaClass")
694+
else -> throw InternalKSPException(
695+
"Unexpected annotated symbol with 'all' use-site target",
696+
location,
697+
javaClass
698+
)
598699
}
599700
}
600701

@@ -603,12 +704,16 @@ class PsiResolutionStrategy(
603704
*/
604705
private fun PsiTypeParameter.resolveTypeParameterOfMethod(method: PsiMethod): KSTypeParameter {
605706
val callableSym = analyze { method.callableSymbol }
606-
?: error("Unexpected null callable symbol at ${method.toLocation()}: ${method.javaClass}")
707+
?: throw InternalKSPException(
708+
"Unexpected null callable symbol",
709+
method.toLocation(),
710+
method.javaClass
711+
)
607712
val functionDecl = callableSym.toKSFunctionDeclaration()
608-
?: error(
609-
"Failed to convert callable symbol to KSFunctionDeclaration at " +
610-
"${method.toLocation()}: " +
611-
"${callableSym.javaClass}"
713+
?: throw InternalKSPException(
714+
"Failed to convert callable symbol to KSFunctionDeclaration",
715+
method.toLocation(),
716+
callableSym.javaClass
612717
)
613718
return functionDecl.typeParameters[parameterIndex]
614719
}
@@ -618,7 +723,11 @@ class PsiResolutionStrategy(
618723
*/
619724
private fun PsiTypeParameter.resolveTypeParameterOfClass(clazz: PsiClass): List<KSTypeParameter> {
620725
val classSym = analyze { clazz.namedClassSymbol }
621-
?: error("Unexpected named class symbol at ${clazz.toLocation()}: ${clazz.javaClass}")
726+
?: throw InternalKSPException(
727+
"Unexpected named class symbol",
728+
clazz.toLocation(),
729+
clazz.javaClass
730+
)
622731
val typeParamSym = classSym.toKSClassDeclaration().typeParameters[parameterIndex]
623732
// Type parameters for classes return two symbols with Analysis API,
624733
// one as a Psi-based KaFirTypeParameter and one as a "regular" KaFirTypeParameter,
@@ -635,7 +744,11 @@ class PsiResolutionStrategy(
635744
*/
636745
private val PsiAnnotation.shortName: String
637746
get() = this.nameReferenceElement?.referenceName
638-
?: error("Unexpected nullable annotation short name at ${toLocation()}: $javaClass")
747+
?: throw InternalKSPException(
748+
"Unexpected nullable annotation short name",
749+
toLocation(),
750+
javaClass
751+
)
639752

640753
/**
641754
* Returns the callable symbol, i.e., the method symbol for the Java parameter `this`.
@@ -644,10 +757,18 @@ class PsiResolutionStrategy(
644757
private val PsiParameter.callableSymbol: KaCallableSymbol
645758
get() {
646759
val decl = this.parent.parent as? PsiMember
647-
?: error("Unexpected PsiParameter at ${toLocation()}: $javaClass")
760+
?: throw InternalKSPException(
761+
"Unexpected PsiParameter",
762+
toLocation(),
763+
javaClass
764+
)
648765
return analyze {
649766
decl.callableSymbol
650-
?: error("Unexpected null callable symbol at ${decl.toLocation()}: ${decl.javaClass}")
767+
?: throw InternalKSPException(
768+
"Unexpected null callable symbol",
769+
decl.toLocation(),
770+
decl.javaClass
771+
)
651772
}
652773
}
653774

@@ -661,7 +782,11 @@ class PsiResolutionStrategy(
661782
return parent.getTypeParameterIndex(this)
662783

663784
else ->
664-
error("Unexpected parent of PsiTypeParameter at ${parent.toLocation()}: ${parent.javaClass}")
785+
throw InternalKSPException(
786+
"Unexpected parent of PsiTypeParameter",
787+
parent.toLocation(),
788+
parent.javaClass
789+
)
665790
}
666791
}
667792

0 commit comments

Comments
 (0)