• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection capabilities of other languages  RSS feed

 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Authors,

How would you compare's java reflection capablities with those of other languages like C# or Python etc. Is java lacking in some respect?
 
Nate Forman
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your interest, Karthik.

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.

Best Regards,

Nate
 
Jim Bracks
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What in your opinion Java Refection lacks?
Thanks.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Nate


Thanks Nate,

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!.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Bracks:
What in your opinion Java Refection lacks?
Thanks.


Hi Jim,
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...
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not a (completely) Java zealot, but I wouldn't say that obtaining method parameters names is *more granularity*. The Java bytecode if containing debug info can be queried for local variable names and so on. In case it doesn't contain debug info this info is indeed not available.
I would like to see deeper differences in order to say that java reflection and java tools are missing something regarding this .


--
./pope
[the_mindstorm]
 
Nate Forman
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't feel like storing the names of parameters is more granularity. Not in the sense that supporting lambda expressions and allowing you to see the innards of methods would be. Storing parameter names is adding more metadata about the current level of granularity, and while that's useful, it doesn't really open up new dimensions in capability.

Best Regards,

Nate
 
Ira Forman
author
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!