Skip to content

Commit

Permalink
fix annotation default class reference value for annotation declared …
Browse files Browse the repository at this point in the history
…in java source.
  • Loading branch information
neetopia committed Jun 14, 2023
1 parent 6ab92a1 commit 5ef0d6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.devtools.ksp.symbol.impl.kotlin.getKSTypeCached
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiAnnotation
import com.intellij.psi.PsiAnnotationMethod
import com.intellij.psi.PsiType
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
Expand Down Expand Up @@ -339,7 +340,11 @@ fun ValueParameterDescriptor.getDefaultValue(ownerAnnotation: KSAnnotation): Any
when (psi.defaultValue) {
is PsiAnnotation -> KSAnnotationJavaImpl.getCached(psi.defaultValue as PsiAnnotation)
else -> JavaPsiFacade.getInstance(psi.project).constantEvaluationHelper
.computeConstantExpression((psi).defaultValue)
.computeConstantExpression((psi).defaultValue).let {
if (it is PsiType) {
ResolverImpl.instance!!.resolveJavaTypeInAnnotations(it)
} else it
}
}
}
else -> throw IllegalStateException("Unexpected psi ${psi.javaClass}, $ExceptionMessage")
Expand Down
6 changes: 4 additions & 2 deletions test-utils/testData/api/annotationWithDefaultValues.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
// TEST PROCESSOR: AnnotationDefaultValuesProcessor
// EXPECTED:
// KotlinAnnotation -> b:default,kClassValue:Array<Array<InnerObj>>,topLevelProp:foo,companionProp:companion
// JavaAnnotation -> withDefaultValue:OK,nested:@Nested
// JavaAnnotation -> withDefaultValue:OK,typeVal:HashMap<*, *>,nested:@Nested
// JavaAnnotation2 -> x:x-default,y:y-default,z:z-default
// KotlinAnnotation2 -> y:y-default,z:z-default,kotlinEnumVal:VALUE_1
// KotlinAnnotationLib -> b:defaultInLib,kClassValue:OtherKotlinAnnotation,topLevelProp:bar
// JavaAnnotationWithDefaults -> stringVal:foo,stringArrayVal:[x, y],typeVal:HashMap<*, *>,typeArrayVal:[LinkedHashMap<*, *>],intVal:3,intArrayVal:[1, 3, 5],enumVal:JavaEnum.DEFAULT,enumArrayVal:[JavaEnum.VAL1, JavaEnum.VAL2],localEnumVal:JavaAnnotationWithDefaults.LocalEnum.LOCAL1,otherAnnotationVal:@OtherAnnotation,otherAnnotationArrayVal:[@OtherAnnotation],kotlinAnnotationLibVal:@OtherKotlinAnnotation
// KotlinAnnotationWithDefaults -> stringVal:foo,stringArrayVal:[x, y],typeVal:HashMap<*, *>,typeArrayVal:[LinkedHashMap<*, *>],intVal:3,intArrayVal:[1, 3, 5],enumVal:JavaEnum.DEFAULT,enumArrayVal:[JavaEnum.VAL1, JavaEnum.VAL2],otherAnnotationVal:@OtherAnnotation,otherAnnotationArrayVal:[@OtherAnnotation],kotlinAnnotationLibVal:@OtherKotlinAnnotation
// KotlinAnnotation -> b:default,kClassValue:Array<Array<InnerObj>>,topLevelProp:foo,companionProp:companion
// JavaAnnotation -> withDefaultValue:OK,nested:@Nested
// JavaAnnotation -> withDefaultValue:OK,typeVal:HashMap<*, *>,nested:@Nested
// JavaAnnotation2 -> x:x-default,y:y-default,z:z-default
// KotlinAnnotation2 -> y:y-default,z:z-default,kotlinEnumVal:VALUE_1
// END
Expand Down Expand Up @@ -128,9 +128,11 @@ enum class KotlinEnum {
}

// FILE: JavaAnnotation.java
import java.util.HashMap;
public @interface JavaAnnotation {
String debug();
String withDefaultValue() default "OK";
Class<?> typeVal() default HashMap.class;
@interface Nested {
String nestedX() default "nested";
}
Expand Down

0 comments on commit 5ef0d6d

Please sign in to comment.