How would you compare's java reflection capablities with those of other languages like C# or Python etc. Is java lacking in some respect?
I'm going to defer this partially to my dad who will be available later in the week. I think he'll have some valuable insights.
Though Java reflection is a great tool to have in your toolbox, it does have its limitations. We try to point these out (and ways to get around them) in our conceptual presentation in the book. It really comes down to the topic of your previous posting--modifying the running program. Java doesn't do much (any?) of this, and that means that you need to exercise other options like generating code, etc. It definitely does not implement the full potential of a reflective API.
I just haven't had much time to study C# yet. I would guess (from what I have seen) that its capabilities are similar to Java's, but I could be surprised...
Python has a special place in our hearts because the reflection API in the latest version is based on my dad's previous book, "Putting Metaclasses to Work", with his colleague Scott Danforth. I've been told that you can do some really cool things with the Python API, but I've not been able to study it yet due to work and book obligations.
Originally posted by Nate Forman:
Python has a special place in our hearts because the reflection API in the latest version is based on my dad's previous book, "Putting Metaclasses to Work", with his colleague Scott Danforth.
Yup I just noticed, Python's method resolution order in case of multiple inheritance is based on your dad's book? ..very interesting actually! May be you can confirm. We are honoured to have you guys here!.
Originally posted by Jim Bracks:
What in your opinion Java Refection lacks?
In this comparison between C# and Java's reflection, we can see that
One might notice from the above code samples that there is slightly more granularity in the C# Reflection API than the Java Reflection API as can be seen by the fact that C# has a ParameterInfo class which contains metadata about the parameters of a Method while Java uses Class objects for that which lose some information such as the name of the parameter.
Just wanna share some info that I bookmarked before... In this case, C# got better design than Java's reflection API...
I would like to see deeper differences in order to say that java reflection and java tools are missing something regarding this .
I have casually looked at one book on C#. With that in mind, I don't believe that there are qualitative differences between Java and C# in that reflection in both is basically limited to introspection. However, I believe Java did it better in that it has class objects that are instances of the metaclass, Class. I may be wrong but I don't believe C#'s Type qualifies as a metaclass. This difference prepares Java to be extended with user defined metaclasses.
Now, let me tell why you want to have user defined metaclasses. Someday, when we all have lots of reflective code, we will want to modularize it. The metaclass allows us to modularize aspects of an application along the lines prescribed by AOP. The advantage is that this kind of modularization is achieved inside the programming language rather than with a new one or with bytecode manipulation.
Of course, the reflective API will need to be beefed up to allow one to modify classes by adding members. Appropriate control of such powerful features will be necessary. For example, adding a field to a class object is safe if the the class has neither instances nor subclasses. Adding a field to a class object that has instances is problematic. In Section 6.4 of "Java Reflection in Action", we present an example of how to structure an application so that a class object can be dynamically replaced when it has instances. This requires a combination of a special class loader and dynamic proxies.