Is there a way to access a specific Field
on a class without using reflection?
Consider this class:
class MyType {
public int theNumber;
}
Where I would like to get access to theNumber
's java.lang.reflect.Field
.
This works for sure:
Field f = MyType.class.getDeclaredField("theNumber");
However, I would like compile check on the field name, so ideally something like this instead (but of course my example doesn't compile):
Field f = MyType.class::theNumber;
Is this possible or am I way of wrt the compiler abilities?
The alternate for reflection is using Interface. Just taking from Effective Java by Joshua Bloch. We can obtain many of the benefits of reflection while incurring few of its costs by using it only in a very limited form.
If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.
getClass() is reflection per se since it can be used to examine the given object.
The getField() method of java. lang. Class class is used to get the specified field of this class, which is the field that is public and its members. The method returns the specified field of this class in the form of Field objects.
You can extend the java compiler with annotation processors. This processors are a way to scan your source code during compile. They were introduced with Annotations but they are able to scan the whole source code not only annotations.
With the scanned Source-Code you can generate accessor classes to any class you compile. This way you can eliminate reflection.
If you only want to get errors while you write code in your IDE you can make use of javax.annotation.processing.ProcessingEnvironment.getMessager().printMessage() (see also javax.tools.DiagnosticListener) to generate Errors the IDE can show.
So the basic Idea is:
In case you want to generate type save access to Field:
3.1. generate source that will access this source code
In case you want to ensure, that a reflective call to a field is valid:
3.2. raise an Error via ProcessingEnvironment.getMessager().printMessage()
Of course you have to write the code for checking reflective calls or generating the accessors.
And the information you want to get must be extractable from the sourcecode since all the magic happens during the compile and not at runtime
Interesting question. No, there's no way to do that without using java.lang.reflect
, but given the built-in class
pseudo-property on classes, I can see why you'd ask.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With