Skip to content

Commit

Permalink
Fix #8554: silently ignore missing annotations like javac
Browse files Browse the repository at this point in the history
  • Loading branch information
smarter committed Jun 1, 2020
1 parent e5e489e commit 76e16d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@ class ClassfileParser(
*/
def parseAnnotation(attrNameIndex: Char, skip: Boolean = false)(implicit ctx: Context): Option[Annotation] = try {
val attrType = pool.getType(attrNameIndex)
attrType match {
case tp: TypeRef =>
// Silently ignore missing annotation classes like javac
if tp.denot.infoOrCompleter.isInstanceOf[StubInfo] then
if (ctx.debug)
ctx.warning(i"Error while parsing annotations in ${in.file}: annotation class $tp not present on classpath")
return None
case _ =>
}
val nargs = in.nextChar
val argbuf = new ListBuffer[untpd.Tree]
var hasError = false
Expand Down
17 changes: 17 additions & 0 deletions compiler/test/dotty/tools/AnnotationsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,20 @@ class AnnotationsTest:
}
}
}

@Test def surviveMissingAnnot: Unit =
withJavaCompiled(
VirtualJavaSource("Annot.java",
"public @interface Annot {}"),
VirtualJavaSource("A.java",
"@Annot() public class A {}")) { javaOutputDir =>
Files.delete(javaOutputDir.resolve("Annot.class"))
inCompilerContext(javaOutputDir.toString + File.pathSeparator + TestConfiguration.basicClasspath) {
val cls = ctx.requiredClass("A")
val annots = cls.annotations.map(_.tree)
assert(annots == Nil,
s"class A should have no visible annotations since Annot is not on the classpath, but found: $annots")
assert(!ctx.reporter.hasErrors && !ctx.reporter.hasWarnings,
s"A missing annotation while parsing a Java class should be silently ignored but: ${ctx.reporter.summary}")
}
}

0 comments on commit 76e16d3

Please sign in to comment.