Is it possible to have method / field literals comparable to class literature in Java / Scala?

Java Foo.class and the Scala class literal syntax classOf[Foo] return a reflective look at the class in question.

Is it possible, and it would be advisable, to provide something like .method/.field or methodOf[] / fieldOf[] to obtain comparable reflective access to methods and fields?

How would this be implemented in Java / Scala?

In the case of Java, I would suggest that this will require either a change in language (very unlikely) or some kind of magic with bytecode tools / AspectJ, while in Scala it is possible to implement it with an implicit conversion.

+4
source share
2 answers

Yes and no. Paul Phillips has certainly shown interest in such a substance, and there is a lot of work currently going on in the torso around the upcoming scala reflections.

It is doubtful that we will see something like your proposed syntax. Methods are not a first-class construct and, as such, refer only to their class. But we get a nice scala convenient way to access elements through reflection, including default parameters, parameter names, etc.

+8
source

I don’t remember where, but recently I came across a Java library that will take Java classes as input and generate a metaclass, so to speak, with static fields (I think) that were references to all fields and methods in the target class. This, of course, is not as elegant as what you are looking for, but it seemed to me a potentially useful piece of magic.

+2
source

All Articles