From d898589ea9a4a9d95f671936d94ca70d0c8e576e Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 18 Sep 2017 17:51:33 +0200 Subject: [PATCH] Don't fail for deprecated lazy vals, fixes #85. This somewhat fixes #85 so that genjavadoc does at least not fail. However, like in the case for deprecated lazy vals with preexisting scaladoc, the preexisting comment might be lost under some Scala versions. The reason seems to be that starting from Scala 2.12 positions are reported in a way that puts the original position on the private `$lzycompute` method instead of on the accessor. To fix that additional considerations are necessary to somehow reconcile position information from the lzycompute with the actual accessor so that existing and generated scaladoc can be merged. --- .../scala/com/typesafe/genjavadoc/AST.scala | 19 ++++++++++++++++--- .../rk/buh/is/it/DeprecatedNoComment.java | 5 +++++ .../src/test/resources/input/basic/test.scala | 3 +++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala b/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala index 3e2210b..26b0e0b 100644 --- a/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala +++ b/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala @@ -82,9 +82,22 @@ trait AST { this: TransformCake ⇒ def maybeSinceDot = if (since.endsWith(".")) " " else ". " def render = s" * @deprecated ${msg}${maybeDot}Since $since${maybeSinceDot}" - def appendToComment(comment: Seq[String]): Seq[String] = comment match { - case Nil => List("/**", render, "*/") - case c => c.dropRight(1) ++ List(" *", render, "*/") + def fullComment = List("/**", render, "*/") + + def appendToComment(comment: Seq[String]): Seq[String] = { + comment match { + case Nil => fullComment + case c :: Nil if c.trim.startsWith("//") => + // sometimes `comment` is just a single line of `// not preceding`. In that case, we just keep that comment line + // and add the deprecation as the complete thing + c +: fullComment + case cs => + if (!cs.lastOption.exists(_.trim == "*/")) + (cs :+ "// cannot join comments, what happened here? Report at https://github.com/typesafehub/genjavadoc.") ++ + fullComment + else + cs.dropRight(1) ++ List(" *", render, "*/") + } } } diff --git a/plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/DeprecatedNoComment.java b/plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/DeprecatedNoComment.java index 728544f..1a61f07 100644 --- a/plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/DeprecatedNoComment.java +++ b/plugin/src/test/resources/expected_output/basic/akka/rk/buh/is/it/DeprecatedNoComment.java @@ -8,4 +8,9 @@ public class DeprecatedNoComment { * @deprecated This is replaced by theShinyNewMethod. Since now. */ public void oldMethod () { throw new RuntimeException(); } + private void oldLazyVal$lzycompute () { throw new RuntimeException(); } + /** + * @deprecated This is replaced by theShinyNewLazyVal. Since now. + */ + public void oldLazyVal () { throw new RuntimeException(); } } diff --git a/plugin/src/test/resources/input/basic/test.scala b/plugin/src/test/resources/input/basic/test.scala index c6f3ec8..fc49233 100644 --- a/plugin/src/test/resources/input/basic/test.scala +++ b/plugin/src/test/resources/input/basic/test.scala @@ -273,6 +273,9 @@ private[it] class DontTouchThis { class DeprecatedNoComment { @deprecated("This is replaced by theShinyNewMethod", since = "now") def oldMethod = () + + @deprecated("This is replaced by theShinyNewLazyVal", since = "now") + lazy val oldLazyVal= () }