CSC/ECE 517 Fall 2010/ch1 1c NR: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 19: Line 19:
==Reflection in Java==
==Reflection in Java==
Reflection in Java is achieved by making method calls on a Class object. Before using reflection on an object, first it is necessary to map it to an instance of Class. It is important to note that this method only works for types that are derived from Object, not all types in Java are an object, e.g. boolean. <br />
Reflection in Java is achieved by making method calls on a Class object. Before using reflection on an object, first it is necessary to map it to an instance of Class. It is important to note that this method only works for types that are derived from Object, not all types in Java are an object, e.g. boolean. <br />
Basic Api in java:
<b>getClass</b> - for a given instance, an Object type Class is returned.(that instance of type Class which is created for every type of object by JVM which helps to futher get the information about runtime properties of the object and its members.)
<pre>
Class cls= "Hello World".getClass();
</pre>
<b><forName></b> - Object of type Class is returned. (Used when the type is available but no instance.This is also used to obtain the Class for primitive types.)
<pre>
Class cls = boolean.class; 
</pre>
<b>Class.forName()</b> - Used when a fully qualified class name is available.
<pre>
Class cls = Class.forName("[[Ljava.lang.String;");
</pre>
<b>getName</b> - for a given instance, the name of the corresponding class is returned.
<pre>
cls.getName();
</pre>
<b>
getMethod
</b>


=Reflection as Package=
=Reflection as Package=

Revision as of 05:53, 8 September 2010

Introduction to Reflection

Reflection is a language feature that enables a program to examine itself at runtime and possibly change its behavior accordingly. It was introduced by Brian Cantwell Smith as a framework for language extension.There are two aspects to reflection :

  • Introspection - the ability for a program to observe and reason about its own state.
  • Intercession - the ability for a program to modify its own execution state or alter its own interpretation or meaning.

What is crucial here is that a given program can behave not only as a function, but also as a data structure that can be examined and manipulated to change its behavior. These properties lead to an easily extensible language since the structures used by the language implementation are accessible to the programmer. The programmer can now define programming constructs that would otherwise have been either impossible or extremely difficult to define. These properties have led to the adoption of reflection as a primary means for language extensibility. Reflection is most commonly used in many dynamically typed languages such as Ruby,Smalltalk, Objective-c and scripting languages like Perl,PHP. Statically typed languages such as Java, ML or Haskell also support reflection. Reflective programming languages and platforms provides a comprehensive list of all languages and platforms supporting reflection.


Reflection as Language Feature vs Reflection as Package

For the sake of simplicity, we take Ruby and Java to represent languages that have Reflection as a language feature and reflection as a package respectively.

Reflection in Ruby

One of the languages that has reflection as a built-in language feature is Ruby. 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 start using reflection, it is a native part of the language. Additionally, Ruby's intuitive syntax and ease of use make it possible for someone who is not an expert to write Ruby code that uses reflection. Ruby's reflection functionality is made available by the Object class from which everything in Ruby is derived.



Reflection in Java

Reflection in Java is achieved by making method calls on a Class object. Before using reflection on an object, first it is necessary to map it to an instance of Class. It is important to note that this method only works for types that are derived from Object, not all types in Java are an object, e.g. boolean.

Basic Api in java:

getClass - for a given instance, an Object type Class is returned.(that instance of type Class which is created for every type of object by JVM which helps to futher get the information about runtime properties of the object and its members.)

Class cls= "Hello World".getClass();

<forName> - Object of type Class is returned. (Used when the type is available but no instance.This is also used to obtain the Class for primitive types.)

Class cls = boolean.class;  

Class.forName() - Used when a fully qualified class name is available.

Class cls = Class.forName("[[Ljava.lang.String;");

getName - for a given instance, the name of the corresponding class is returned.

cls.getName();

getMethod

Reflection as Package

Conclusion

References