1

I have Test class which has written purely with kotlin in the library project.

class Test{

  @Deprecated(message = "Use other function")
  fun testFunction(id: String): Test {
      this.testId = id
      return this
   }
}

I've deprecated testFunction() with Deprecated annotation. Btw Deprecated class is under the kotlin package. When i test this deprecated function in kotlin project works as expected(ide shows deprecated warning and strikethrough)

Example: Test(). testFunction("test")

But in the java project it doesn't show warning or strikethrough to function bye ide. When I open the declaration of deprecated function it's like below

  @Deprecated(
     message = "Use other function"
   )
  @NotNull
  public final Test testFunction(@NotNull String var1) {
     Intrinsics.checkParameterIsNotNull(var1, "id");
     this.testId = var1;
     return this;
   }

any help would be appreciated

gokhan
  • 627
  • 1
  • 6
  • 16
  • So you use the `@Deprecated`-annotation of Kotlin in Java? Which IDE and version do you use? – Roland Oct 22 '19 at 12:04
  • I use `android studio 3.5.0`, my kotlin version is `1.3.31` – gokhan Oct 22 '19 at 12:05
  • you may want to use `java.lang.Deprecated` for the Java variant... however there seems to be a problem there nonetheless... if the function is within the same file, the strike-through only appears while using code completion, but not for the written code... it works however for deprecated functions of other files... maybe you want to open an appropriate issue, if there isn't one already... – Roland Oct 22 '19 at 12:11
  • I found workaround, i imported java's Depracted annotation as alias ```import java.lang.Deprecated as JavaDeprecated``` and added over the deprecated function and now it works . ``` @JavaDeprecated @Deprecated( message = "Use other function" ) @NotNull public final Test testFunction(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "id"); this.testId = var1; return this; } ``` – gokhan Oct 22 '19 at 14:06

1 Answers1

0

In Kotlin, the functions, properties, and classes marked with kotlin.Deprecated annotation also get the Deprecated attribute in the resulting JVM bytecode. This allows Java compiler to see them as deprecated and emit the corresponding warning.

Take for example this function that is deprecated in Kotlin: https://github.com/JetBrains/kotlin/blob/v1.3.50/libraries/stdlib/common/src/generated/_Ranges.kt#L171-L175 If you try to call it from java as

kotlin.ranges.RangesKt.intRangeContains(null, 1.0);

javac compiler will report the following warning:

Warning:(55, 31) java: intRangeContains(kotlin.ranges.ClosedRange<java.lang.Integer>,double) in kotlin.ranges.RangesKt___RangesKt has been deprecated

and both IDEA and Android Studio will mark it as deprecated as well: Deprecated inspection in IDE

Ilya
  • 21,871
  • 8
  • 73
  • 92