So I’m using reflection to scrape classes for annotated properties, which I then use to inject automatically loaded resources. This seems to be working well, but I have a question about getters and setters in this context: Is there a way to determine if custom getter/setters exist on property? I have a few use cases:
The first is for the sake of robust warnings. E.g. if the annotation allows the property to be mutated, but only if the user “promises” to make a callback, then I’d like to throw an exception if no custom setter is defined at all. It’s not perfect, but I believe this would capture the majority of cases someone declares a property as
var but forgets to make the callback.
The second case is related to optimization. I’d actually like to use the
javaField property directly when possible, because it exposes get/set methods that don’t allocate
vararg arrays; however, whether it’s null is predicated only on the existence of a backing field, which can be true regardless of whether a custom getter/setter is defined. Hence the desire for a direct check.
Thoughts? I’m trying to minimize allocations as much as possible here - both to minimize GC overhead and maintain cache coherency, if at all possible. I can tolerate the reflective aspect the few times it is invoked, but I’d like to avoid having wrapper classes everywhere just to make an optional callback on mutations. Annotations, reflection and LOTS of documentation seemed like a decent way of going about this, but there are some pitfalls like I’ve described here.