CSC/ECE 517 Fall 2009/wiki2 5 jdf: Difference between revisions
No edit summary |
No edit summary |
||
Line 38: | Line 38: | ||
<pre> | <pre> | ||
mystr = "Hello | mystr = "Hello World!" | ||
mystr.class | mystr.class | ||
Line 52: | Line 52: | ||
=== Reflection in Java === | === Reflection in Java === | ||
Reflection in Java is achived by making method calls on the ''Class'' object, i.e. to use reflection first it is necessary to obtain an instance of ''Class''. | |||
The following code shows how to obtain the ''Class'' from a string object: | |||
<pre> | |||
Class myclass = "Hello World!".getClass(); | |||
</pre> | |||
=== Reflection in C# === | === Reflection in C# === |
Revision as of 01:54, 10 October 2009
Please note that this is still work in progress - Not Yet Ready for Review
Reflection is widely used in programming languages. This topic is not about reflection per se, but about reflection APIs--the facilities that particular languages provide for doing reflection. Is reflection "built in" to some o-o languages, while others require external libraries to perform reflection? What built-in and library functions are provided? Which APIs are easiest to use and why?
Reflection APIs
Introduction
Reflection is a feature offered by many modern programming languages. Reflection provides programmers with the ability to write code that determines information about a set of code during run time. The information that is acquired through reflection can be used to develop very sophisticated program features. Programs can be written to adapt based on the characteristics of the code itself. The information that can be obtained via reflection includes elements such as:
- The class types of objects that are present in the running program
- The class hierarchy of an object or set of objects
- The attributes and methods of objects
- Details of the method calls supported by objects [1]
Reflection is present in many programming languages and its use is particularly prevalent in object oriented languages, such as Java and Ruby. The level of implementation and ease of use varies among languages. The objective of this article is to provide an overview of the different Application Programming Interfaces or APIs that exist in various Object Oriented Languages to provide reflection functionality.
Languages that Provide a Reflection API
Among the languages that provide Reflection APIs we count the following:
- Ruby
- Java
- C#
- VB.NET
- Smalltalk-80
Reflection in Ruby
Reflection is built into Ruby, it is a standard feature of the language. Objects in Ruby support reflection by default, hence it is not necessary to use any external or additional libraries. In essence, the programmer does not need to do anything special to star using reflection, it is a native part of the language [1, 2].
The following Ruby code will display the class type of a variable:
mystr = "Hello World!" mystr.class
The following code displays all the methods that are exposed by the class:
mystr.methods
Reflection in Java
Reflection in Java is achived by making method calls on the Class object, i.e. to use reflection first it is necessary to obtain an instance of Class.
The following code shows how to obtain the Class from a string object:
Class myclass = "Hello World!".getClass();
Reflection in C#
Reflection in VB.NET
Other Considerations
Reflection is a very powerful programming feature, usually reserved for more experienced programmers. The use of reflection must be done keeping in mind that the advantages achieved from discovering details about the code during run time come at a price[3]. The following must be considered:
- Reflection incurs heavy overhead, i.e. code that uses reflection cannot be optimized by the compiler in the same way that standard code is. Code that relies on reflection should be expected to run slower.
- Reflection may not work in the deployed environment due to security constraints.
- Code that uses reflection may result in undesired or unexpected side effects, for instance changes to private variables that were never intended to be exposed.
Resources
References
[1] Thomas, D., Fowler C. and Hunt A. Programming Ruby, The Pragmatic Programmer's Guide. 2nd Edition. The Pragmatic Bookshelf.
[2] Help and documentation for the Ruby programming language, RUBY-DOC.ORG
[3] Trail: The Reflection API (The Java Tutorials) [2]
CSC 517 Fall 2009
Wiki 2 Assignment
Author: Newwolf