Skip to content

Commit

Permalink
Check if already has Deprecated, also on module
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jul 2, 2023
1 parent 94af990 commit 54ebd41
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,9 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {

case tpnme.DeprecatedATTR =>
in.skip(attrLen)
sym.addAnnotation(JavaDeprecatedAttr)
if (sym == clazz)
if (!sym.hasAnnotation(JavaDeprecatedAttr))
sym.addAnnotation(JavaDeprecatedAttr)
if (sym == clazz && !staticModule.hasAnnotation(JavaDeprecatedAttr))
staticModule.addAnnotation(JavaDeprecatedAttr)

case tpnme.ConstantValueATTR =>
Expand Down Expand Up @@ -1509,13 +1510,21 @@ abstract class ClassfileParser(reader: ReusableInstance[ReusableDataReader]) {

// Append annotation. For Java deprecation, prefer an annotation with values (since, etc).
private def addUniqueAnnotation(symbol: Symbol, annot: AnnotationInfo): symbol.type =
if (annot.atp.typeSymbol == JavaDeprecatedAttr && symbol.hasAnnotation(JavaDeprecatedAttr))
if (List(0, 1).exists(annot.constantAtIndex(_).isDefined))
symbol.setAnnotations {
def drop(cur: AnnotationInfo): Boolean = cur.atp.typeSymbol == JavaDeprecatedAttr
symbol.annotations.foldRight(annot :: Nil)((a, all) => if (drop(a)) all else a :: all)
}
else symbol
if (annot.atp.typeSymbol == JavaDeprecatedAttr) {
def ensureDepr(sym: Symbol): sym.type = {
if (sym.hasAnnotation(JavaDeprecatedAttr))
if (List(0, 1).exists(annot.constantAtIndex(_).isDefined))
sym.setAnnotations {
def drop(cur: AnnotationInfo): Boolean = cur.atp.typeSymbol == JavaDeprecatedAttr
sym.annotations.foldRight(annot :: Nil)((a, all) => if (drop(a)) all else a :: all)
}
else sym
else sym.addAnnotation(annot)
}
if (symbol == clazz)
ensureDepr(staticModule)
ensureDepr(symbol)
}
else symbol.addAnnotation(annot)
}
object ClassfileParser {
Expand Down
16 changes: 12 additions & 4 deletions test/files/run/t12799.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ Test_2.scala:7: warning: class B in package example is deprecated (since 1.0): b
val deprecatedMethod = classOf[B].getMethod("a")
^
Test_2.scala:9: warning: method a in trait A is deprecated
new B().a
^
println(new B().a)
^
Test_2.scala:9: warning: class B in package example is deprecated (since 1.0): beeless
new B().a
^
println(new B().a)
^
Test_2.scala:10: warning: method answer in class C is deprecated (since beginning)
println(C.answer())
^
Test_2.scala:10: warning: class C in package example is deprecated (since you like it)
println(C.answer())
^
a
42
11 changes: 11 additions & 0 deletions test/files/run/t12799/C.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

package example;

@Deprecated(since="you like it")
public class C {

@Deprecated(since="beginning")
public static int answer() {
return 42;
}
}
3 changes: 2 additions & 1 deletion test/files/run/t12799/Test_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import example._
object Test extends App {
val deprecatedMethod = classOf[B].getMethod("a")
assert(deprecatedMethod.isAnnotationPresent(classOf[Deprecated]))
new B().a
println(new B().a)
println(C.answer())
}
//java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface java.lang.Deprecated: @java.lang.Deprecated(forRemoval=false, since="")

0 comments on commit 54ebd41

Please sign in to comment.