Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declared Annotations Issue #10061

Closed
scabug opened this issue Nov 15, 2016 · 3 comments
Closed

Declared Annotations Issue #10061

scabug opened this issue Nov 15, 2016 · 3 comments

Comments

@scabug
Copy link

scabug commented Nov 15, 2016

m.getDeclaredAnnotations is an empty array and should not be. This would seem to be a bug as I can't imagine this is the correct behaviour.

import scala.annotation.StaticAnnotation

case class ComplexAnnotation(p1: String, p2: Int, p3: Boolean, p4: (_) => Boolean) extends StaticAnnotation

class TestModel {

  @ComplexAnnotation("test", 4, false, (any: Any) => {
    true
  })
  var var1 = 4

  def reflect() = {
    this.getClass.getDeclaredMethods.foreach(m => {
      println("Method: -")
      println(m)
      m.getDeclaredAnnotations.foreach(a => {
        println("Annotations: -")
        println(a)
      })
    })
  }

}
@scabug
Copy link
Author

scabug commented Nov 15, 2016

Imported From: https://issues.scala-lang.org/browse/SI-10061?orig=1
Reporter: Chris Beck (cwbeck)
Affected Versions: 2.12.0
See #7041

@scabug
Copy link
Author

scabug commented Nov 15, 2016

Chris Beck (cwbeck) said:
Also worth adding: -

Trying to reflect like this also fails using - import scala.annotation.Annotation, import scala.reflect.runtime.{universe => ru}

    ru.typeOf[TestModel].decls.collect {
      case m: Any => {
        println("method: -")
        println(m)
        m
      }
    }.withFilter {
      _.annotations.nonEmpty
    }.map { m => {
      println("method with annotations: -")
      println(m)
      m

{code}

@scabug
Copy link
Author

scabug commented Nov 15, 2016

@lrytz said:
This is a known issue: Scala-defined annotations are never emitted as Java-style annotations in the classfile, and therefore not visible to Java reflection. If you need an annotation to be visible to Java reflection, it needs to be defined in Java.

What you're looking for would be ClassfileAnnotation, not StaticAnnotation, but it's not implemented, you'll get a compile-time warning.

See also #7041

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants