binary-compatibility-validator icon indicating copy to clipboard operation
binary-compatibility-validator copied to clipboard

Companion object 'const val' properties generate false positive if they have a non-public marker

Open martinbonnin opened this issue 3 years ago • 2 comments

The following:

@Target(AnnotationTarget.PROPERTY)
annotation class HiddenProperty

public class Foo {
    companion object {
        @HiddenProperty
        const val bar = "barValue"
    }
}

Generates the following pseudo Java code:

public final class Foo {
   @NotNull
   public static final String bar = "barValue";
   @NotNull
   public static final Companion Companion = new Companion((DefaultConstructorMarker)null);

   public static final class Companion {
      // $FF: synthetic method
      @HiddenProperty
      public static void getBar$annotations() {
      }

      private Companion() {
      }
   }
}

Because the synthetic method carrying the annotations is not in the same class as the field, it is ignored and the field is marked as public API even though it shouldn't.

martinbonnin avatar Jun 22 '22 19:06 martinbonnin

I wonder how this translates to Java interop stability. But I guess if it's annotated and the documentation is straightforward, it shouldn't surprise anyone

qwwdfsad avatar Jun 22 '22 19:06 qwwdfsad

Indeed, the Java story, without @OptIn annotations sounds more dangerous. I guess it's all going through documentation...

martinbonnin avatar Jun 22 '22 19:06 martinbonnin