Search code examples
scalaannotationsscala-macrosscala-macro-paradise

Can I use Java reflection to get the value of a member that has been added with a Scala macro annotation?


I have a Scala macro annotation that adds a val "x" to a class MyRecord at compile time.

I've also got some Java code that uses MyRecord.class.getDeclaredField(x) to get the value of the added field.

javap MyRecord.class shows that the added field is indeed public, yet the Java code throws an error, complaining that it's private final.

Here's a simplified project showing that Java reflection returns private final members.

Since I cannot alter the Java code, how can I get the added field to be seen as public by Java reflection?

Thanks,

Julian


Solution

  • The field is private, but the getter is public. Even for public vals, scalac doesn't create public fields.