Skip to content

Commit

Permalink
Adjust type descriptors in annotation methods, which are implicitly n…
Browse files Browse the repository at this point in the history
…on-nullable.

PiperOrigin-RevId: 713384689
  • Loading branch information
Googler authored and copybara-github committed Jan 8, 2025
1 parent 05f2c39 commit 2a5febb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,10 @@ private static boolean isEnumValueOfMethod(IMethodBinding methodBinding) {
&& methodBinding.getParameterTypes()[0].getQualifiedName().equals("java.lang.String");
}

private static boolean isAnnotationMethod(IMethodBinding methodBinding) {
return methodBinding.getDeclaringClass().isAnnotation();
}

/**
* Returns true if instances of this type capture its outer instances; i.e. if it is an non static
* member class, or an anonymous or local class defined in an instance context.
Expand Down Expand Up @@ -856,7 +860,7 @@ public MethodDescriptor createMethodDescriptor(IMethodBinding methodBinding) {
TypeDescriptor returnTypeDescriptor =
isConstructor
? enclosingTypeDescriptor.toNonNullable()
: adjustForSyntheticEnumMethod(
: adjustForSyntheticEnumOrAnnotationMethod(
methodBinding,
createTypeDescriptorWithNullability(
methodBinding.getReturnType(),
Expand Down Expand Up @@ -941,7 +945,7 @@ private ImmutableList<ParameterDescriptor> convertParameterDescriptors(

for (int i = firstNonSyntheticParameter; i < parameterTypes.length; i++) {
TypeDescriptor parameterTypeDescriptor =
adjustForSyntheticEnumMethod(
adjustForSyntheticEnumOrAnnotationMethod(
methodBinding,
createTypeDescriptorWithNullability(
parameterTypes[i], methodBinding.getParameterAnnotations(i), inNullMarkedScope));
Expand All @@ -959,14 +963,14 @@ private ImmutableList<ParameterDescriptor> convertParameterDescriptors(

/**
* Makes parameters and returns of the synthetic enum methods ({@code Enum.valueOf} and {@code
* Enum.values}) non-nullable.
* Enum.values}) and all annotation methods non-nullable.
*
* <p>Note that non-nullability is also applied to the component of array types to cover the
* return of {@code Enum.values}.
*/
private TypeDescriptor adjustForSyntheticEnumMethod(
private TypeDescriptor adjustForSyntheticEnumOrAnnotationMethod(
IMethodBinding methodBinding, TypeDescriptor typeDescriptor) {
if (!isEnumSyntheticMethod(methodBinding)) {
if (!isEnumSyntheticMethod(methodBinding) && !isAnnotationMethod(methodBinding)) {
return typeDescriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ class Foo {
m_booleanValue__boolean() {}
/** @abstract @nodts @return {number} */
m_intValue__int() {}
/** @abstract @nodts @return {?string} */
/** @abstract @nodts @return {string} */
m_stringValue__java_lang_String() {}
/** @abstract @nodts @return {Class<?>} */
/** @abstract @nodts @return {!Class<?>} */
m_classValue__java_lang_Class() {}
/** @abstract @nodts @return {SomeEnum} */
/** @abstract @nodts @return {!SomeEnum} */
m_enumValue__annotation_Main_SomeEnum() {}
/** @abstract @nodts @return {Zoo} */
/** @abstract @nodts @return {!Zoo} */
m_annotationValue__annotation_Main_Zoo() {}
/** @abstract @nodts @return {Array<boolean>} */
/** @abstract @nodts @return {!Array<boolean>} */
m_booleanArray__arrayOf_boolean() {}
/** @abstract @nodts @return {Array<number>} */
/** @abstract @nodts @return {!Array<number>} */
m_intArray__arrayOf_int() {}
/** @abstract @nodts @return {Array<?string>} */
/** @abstract @nodts @return {!Array<string>} */
m_stringArray__arrayOf_java_lang_String() {}
/** @abstract @nodts @return {Array<Class<?>>} */
/** @abstract @nodts @return {!Array<!Class<?>>} */
m_classArray__arrayOf_java_lang_Class() {}
/** @abstract @nodts @return {Array<SomeEnum>} */
/** @abstract @nodts @return {!Array<!SomeEnum>} */
m_enumArray__arrayOf_annotation_Main_SomeEnum() {}
/** @abstract @nodts @return {Array<Zoo>} */
/** @abstract @nodts @return {!Array<!Zoo>} */
m_annotationArray__arrayOf_annotation_Main_Zoo() {}
/** @nodts */
static $clinit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ open class Main {
val annotationValue: Main.Zoo? = foo!!.annotationValue()
val booleanArray: BooleanArray? = foo!!.booleanArray()
val intArray: IntArray? = foo!!.intArray()
val stringArray: Array<String?>? = foo!!.stringArray()
val classArray: Array<Class<*>?>? = foo!!.classArray()
val enumArray: Array<Main.SomeEnum?>? = foo!!.enumArray()
val annotationArray: Array<Main.Zoo?>? = foo!!.annotationArray()
val stringArray: Array<String?>? = foo!!.stringArray() as Array<String?>?
val classArray: Array<Class<*>?>? = foo!!.classArray() as Array<Class<*>?>?
val enumArray: Array<Main.SomeEnum?>? = foo!!.enumArray() as Array<Main.SomeEnum?>?
val annotationArray: Array<Main.Zoo?>? = foo!!.annotationArray() as Array<Main.Zoo?>?
}
}

Expand All @@ -66,34 +66,34 @@ open class Main {
fun intValue(): Int

@ObjCName("stringValue")
fun stringValue(): String?
fun stringValue(): String

@ObjCName("classValue")
fun classValue(): Class<*>?
fun classValue(): Class<*>

@ObjCName("enumValue")
fun enumValue(): Main.SomeEnum?
fun enumValue(): Main.SomeEnum

@ObjCName("annotationValue")
fun annotationValue(): Main.Zoo?
fun annotationValue(): Main.Zoo

@ObjCName("booleanArray")
fun booleanArray(): BooleanArray?
fun booleanArray(): BooleanArray

@ObjCName("intArray")
fun intArray(): IntArray?
fun intArray(): IntArray

@ObjCName("stringArray")
fun stringArray(): Array<String?>?
fun stringArray(): Array<String>

@ObjCName("classArray")
fun classArray(): Array<Class<*>?>?
fun classArray(): Array<Class<*>>

@ObjCName("enumArray")
fun enumArray(): Array<Main.SomeEnum?>?
fun enumArray(): Array<Main.SomeEnum>

@ObjCName("annotationArray")
fun annotationArray(): Array<Main.Zoo?>?
fun annotationArray(): Array<Main.Zoo>

@ObjCName("J2ktAnnotationMain_FooCompanion", exact = true)
companion object {
Expand Down

0 comments on commit 2a5febb

Please sign in to comment.