1

I'm using Groovy's static type checking to enforce protection of my application from its configuration scripts.

Is it normal that the script can still access private fields, even with static type checking enabled?

Debugging in the code, I see that the access ends up in StaticTypeCheckingVisitor.hasAccessToMember where receiver and accessor are both set to the destination class but I would have expected that accessor is the script class...

alex137
  • 178
  • 1
  • 8

1 Answers1

0

Yes, it is normal. Groovy doesn't generally cares about visibility, see this blog post You wonder probably how it's even possible... because Groovy uses Reflection API. Here is the SO question about it.

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
  • I know that Groovy allows access to private fields using reflection. But I was surprised that Groovy STC (Static-Type-Checking) allows it. This is not my expectation of type-checking... I'm especially surprised since access to private **methods** are properly rejected by the type-checker (and properly made available by the runtime if you don't activate STC) – alex137 Jun 28 '23 at 15:15