Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrd committed Nov 30, 2023
1 parent 1aca135 commit fcbb3a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
30 changes: 30 additions & 0 deletions tasty-query/shared/src/main/scala/tastyquery/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import tastyquery.Types.*
object Annotations:
final class Annotation(val tree: TermTree):
private var mySymbol: ClassSymbol | Null = null
private var mySafeSymbol: Option[ClassSymbol] | Null = null
private var myArguments: List[TermTree] | Null = null

/** The annotation class symbol. */
Expand All @@ -22,9 +23,30 @@ object Annotations:
else
val computed = computeAnnotSymbol(tree)
mySymbol = computed
mySafeSymbol = Some(computed)
computed
end symbol

/** Tests whether the annotations has the given class symbol.
*
* If the class of this annotation cannot be successfully resolved,
* returns `false`.
*/
private[tastyquery] def safeHasSymbol(cls: ClassSymbol)(using Context): Boolean =
symbol

val safeSymbol =
val local = mySafeSymbol
if local != null then local
else
val local = computeSafeAnnotSymbol(tree)
local.foreach(mySymbol = _)
mySafeSymbol = local
local

safeSymbol.contains(cls)
end safeHasSymbol

/** The symbol of the constructor used in the annotation.
*
* This operation is not supported for annotations read from Java or Scala 2.
Expand Down Expand Up @@ -138,6 +160,14 @@ object Annotations:
}
end computeAnnotSymbol

private def computeSafeAnnotSymbol(tree: TermTree)(using Context): Option[ClassSymbol] =
// This is not a very efficient way to deal with this, but I don't really see a good solution
try Some(computeAnnotSymbol(tree))
catch
case _: MemberNotFoundException =>
None
end computeSafeAnnotSymbol

private def findNewAnnotTypeTree(tree: TermTree): TypeTree =
def invalid(): Nothing =
throw InvalidProgramStructureException(s"Cannot find annotation class in $tree")
Expand Down
6 changes: 3 additions & 3 deletions tasty-query/shared/src/main/scala/tastyquery/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ object Symbols {
final def asPackage: PackageSymbol = this.asInstanceOf[PackageSymbol]

final def hasAnnotation(annotClass: ClassSymbol)(using Context): Boolean =
annotations.exists(_.symbol == annotClass)
annotations.exists(_.safeHasSymbol(annotClass))

final def getAnnotations(annotClass: ClassSymbol)(using Context): List[Annotation] =
annotations.filter(_.symbol == annotClass)
annotations.filter(_.safeHasSymbol(annotClass))

final def getAnnotation(annotClass: ClassSymbol)(using Context): Option[Annotation] =
annotations.find(_.symbol == annotClass)
annotations.find(_.safeHasSymbol(annotClass))

override def toString: String = {
val ownerPrefix = owner match
Expand Down
14 changes: 14 additions & 0 deletions test-sources/src/main/scala/simple_trees/MissingAnnotations.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package simple_trees

import javax.annotation.processing.SupportedAnnotationTypes

class MissingAnnotations:
@SupportedAnnotationTypes(Array("foo.bar"))
def foo(): String = "hello"
end MissingAnnotations

object MissingAnnotationsTest:
def test(): Unit =
new MissingAnnotations().foo()
end test
end MissingAnnotationsTest

0 comments on commit fcbb3a7

Please sign in to comment.