CSC/ECE 517 Fall 2012/ch1 1w5 su: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 176: Line 176:
   bar()
   bar()
   -----
   -----
     Param 0: $bar
     Param 0: $baz
       Passed by reference: 1
       Passed by reference: 1
       Can be null: 0
       Can be null: 0

Revision as of 23:42, 11 September 2012

Reflective Language Features vs Reflective Packages

Reflection is the ability of a computer program to examine and modify the structure and behavior (specifically the values, meta-data, properties and functions) of an object at runtime.<ref>Reflection on Wikipedia</ref>

The concept of reflection is best understood by reaching back to the study of self-awareness in artificial intelligence: "Here I am walking down the street in the rain. Since I'm starting to get drenched, I should open my umbrella." This thought fragment reveals a self-awareness of behavior and state, one that leads to a change in that selfsame behavior and state. It would be desirable for computations to avail themselves of these reflective capabilities, examining themselves in order to make use of meta-level information in decisions about what to do next. <ref>An Introduction to Reflection-Oriented Programming - J.M. Sobel , Daniel P. Friedman - 1996</ref>

A reflective language is a programming language that has been architected to allow reflection. This means the language was constructed to 'reason' effectively and consequentially about its own inference process. Languages : Ruby, PHP, Perl, Python

Languages that are not reflective can incorporate some of the features of reflection by using reflective packages. Languages : Java( includes a reflective package called "java.lang.reflect."), C#

Brief History of Reflection

Year Development <ref>Java Reflection - Ken Cooney</ref>
Jun 1976 Origin of Reflective Programming:

The idea of reflection came to Brian Smith at the Xerox Palo Alto Research Center. Smith was working on the KRL <ref>Knowledge Representation Language(KRL)</ref> representation language and as an exercise to learn the language, he worked on a project to represent KRL in KRL. He reasoned that this would be a good way to learn the language since he had to both use and mention the language. He worked on it long enough to become intrigued with the thought of building a system that was self-descriptive. In the five years since, Smith worked on initial versions of such a language, which he called MANTIQ.

1982 Brian Cantwell Smith writes a doctoral dissertation at MIT introducing the notion of computational reflection. 3-LISP is the first official programming language to use reflection. <ref> Brian Cantwell Smith, "Procedural Reflection in Programming Languages", Massachusetts Institute of Technology, Feb 1982 </ref>
1983 Smalltalk v1.0 has 75% of the standard Reflection command language. <ref>Infoworld July 27, 1987, News Briefs. P31,32</ref>
1984 The Interim 3-LISP Reference Manual is published, co authored by Brian Cantwell Smith. <ref>Brian Cantwell Smith, Jim des Rivières. "Interim 3-LISP Reference Manual". Xerox PARC. 1984</ref>
1987 Smalltalk v2.0 adds the rest of the Reflection. [6]
1987-89 Perl <ref> The Timeline of Perl and its Culture </ref>
Oct 1996 Visual J++ and C# has reflections. Python v1.4 <ref>A. Andersen, A note on reflection in Python 1.5, Distributed Multimedia Research Group Report, MPG-98-05, Lancaster University, UK, March 1998</ref>
Feb 1997 Java Reflections (JDK v1.1) <ref>Joe Weber. Special Edition Using Java 1.1, Third Edition. </ref>
Feb 1998 Python v1.5 makes it easier for reflective programming. [9]
Jul 2004 PHP v5.0 <ref>Reflection in php </ref>

Techniques used by reflective systems

There are two techniques used by reflective systems:

  • Introspection, which is the ability of a program to examine its own state.
  • Intercession, which is the ability of a program to modify its own execution state or alter its own interpretation or meaning.

Introspection

Introspection is what allows the program to "know" information about itself. This can include an object knowing what kind of object it is, and what methods it has.
Example (Ruby):

class Sample
  #empty class
end
samp = Sample.new
samp.class   # outputs the name of the class
samp.methods # list the methods that Sample and its' superclass,'Object', has

Intercession

Intercession includes calling methods dynamically, which means that the actual method that is called on an object is decided when the program is running, and not when the program is compiled.
Example (Ruby):

class Sample      #defines two methods that print different numbers depending on which method is called
  def printOne
    puts 1
  end
  def printTwo
    puts 2
  end
end
samp = Sample.new #Create an instance of the Sample class
option="printTwo"
samp.send(option) #printTwo method is called
option="printOne"
samp.send(option) #printOne method is called

Without reflection, calls to these methods could not be changed at runtime. However, reflection allows the method calls to depend on string values that do not have to be initialized until runtime as seen in the above code.

Some of the more dynamic intercessive features that reflection gives a programming language are possible because of the dynamic nature of the language. For example, in Ruby, a method can be added to an object that has already been instanced.

samp = Sample.new
def samp.anothermethod            #will create a method on the instance of Sample
  puts "This is a new method"
end

If another Sample object is created, this new object will not have access to the anothermethod method because that method was only added to a specific instance of the Sample class. In Java, methods cannot be created on existing objects (because it is a statically typed language), so this kind of reflection is not possible.

Types of Reflection

Parallel to the concepts of introspection and intercession, there are two types of reflection:

  • structural reflection
  • behavioral reflection

Behavioral reflection is the ability to intercept an operation such as method invocation and alter the behavior of that operation. If an operation is intercepted, the runtime system calls a method on a meta-object for notifying it of that event. The developers can define their own version of the meta-object so that the meta-object can execute the intercepted operation with customized semantics.

However, behavioral reflection only provides the ability to alter the behavior of operation; it does not provide the ability to alter data structures used by the program statically fixed at compile time. The latter, called structural reflection, allows a program to change, for example, the definition of a class, function or record on demand. <ref> http://www.dcs.bbk.ac.uk/research/techreps/2003/bbkcs-03-02.pdf</ref>

Reflective Languages Features VS Reflective Packages

In a reflective language,

  • you can access the information about an object or class from the object or class.

In a language with a reflective package,

  • reflection functionality is provided through intermediary objects.
  • the package is not automatically included in the functionality. It must be imported with the program.
  • Fewer features are available with the packages. The developer needs to add code for additional features to be availed (and handle the exceptions accordingly). This makes the program harder to maintain and less readable.
  • In Java, once a class is loaded, you cannot change its structure anymore. That is, you cannot change the number and types of fields, and you cannot change the number and signatures of methods anymore. If you want to do that, you have to do that at load-time (or compile time, of course). What you can change at runtime is the definition of methods, that's it.

Reflective Languages Features

Ruby

PHP

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. There is no installation needed to use these functions; they are part of the PHP core. PHP uses ReflectionClass class to report information about a class. <ref>PHP Reflections </ref>

<?php
    class myparent {
        public function foo($bar) {
            // do stuff
        }
    }

    class mychild extends myparent {
        public $val;

        private function bar(myparent &$baz) {
            // do stuff
        }

        public function __construct($val) {
            $this->val = $val;
        }
    }

    $child = new mychild('hello world');
    $child->foo('test');
?>

Script Output
$reflect = new ReflectionClass('mychild');
if ($reflect->isInstance($child)) {
   echo "child object is an instance of the class 'mychild'\n";
}
echo "\nPrinting all methods in the 'mychild' class:\n";
echo "============================================\n";
foreach( $reflect->getMethods() as $reflectmethod) {
   echo "  { $reflectmethod->getName()}()\n";
   echo "  ", str_repeat("-", strlen($reflectmethod->getName()) + 2), "\n";
   foreach( $reflectmethod->getParameters()  as $num => $param) {
       echo "    Param $num: \$", $param->getName() , "\n";
       echo "      Passed by reference: ", (int)$param->isPassedByReference(), "\n";
       echo "      Can be null: ", (int)$param->allowsNull(), "\n";
       echo "      Class type: ";
       if ($param->getClass()) {
           echo  $param->getClass()->getName();
       } else {
           echo "N/A";
       }
       echo "\n\n";
   }
}
child object is an instance of the class 'mychild'

Printing all methods in the 'mychild' class:
============================================
  bar()
  -----
    Param 0: $baz
      Passed by reference: 1
      Can be null: 0
      Class type: myparent

  __construct()
  -------------
    Param 0: $val
      Passed by reference: 0
      Can be null: 1
      Class type: N/A

  foo()
  -----
    Param 0: $bar
      Passed by reference: 0
      Can be null: 1
      Class type: N/A

Reflective Packages

Advantages and Disadvantages

Advantages

  1. Access to Metadata and Ability to Manipulate Classes: You can access the metadata of classes and methods, get the annotations and analyze them. Writing direct code is based on specific class details (like interface, method or field names) known at compile-time. If we need to write code that depends on class details known only at runtime, then reflection comes handy.
  2. Creating Adaptable and Flexible Solutions: Because of its dynamic nature, reflection is useful for creating adaptable and flexible software that is often not possible with direct code. Theoretically, it is well known that reflection can help add more dynamism and flexibility to frameworks and design patterns.
  3. Writing Tools that Require Implementation Details: Many of the developer tools need access to internal or implementation details of the code For example, an intelligent text editor can query a class using reflection and get details about its implementation, which can be very helpful. When user types in an object name and a ‘.’ to access a member, the text editor can pop with a list box with list of accessible members (obtained using reflection) that the programmer can select from.
  4. Signature-Based Polymorphism: Java and C# programmers are familiar with polymorphism based on interface inheritance. Reflection provides an alternative where we can invoke methods having same signature, from different classes not have a common interface (which is required in interface based polymorphism).

Applications

  • Class Browsers and Visual Development Environments: A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.
  • Debuggers and Test Tools: Debuggers need to be able to examine private members on classes which can be achieved with reflections. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.

Disadvantages

  1. Violates Object-Oriented Principles: Use of reflection essentially exposes much of implementation details – such as the methods, fields, accessibility and other metadata – of the class used. This violates the objectives of object oriented programming like abstraction and information hiding, where only the relevant higher level details of the class that the programmer needs to know are exposed to the users and low-level implementation details are hidden.
  2. Security: Reflection allows code to perform some operations that would be illegal in non-reflective code, such as accessing private fields and methods which could potentially be a security concern.
  3. Performance Overhead: Reflection requires dynamic resolution due to which certain optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
  4. Static type check: Using reflection lets you bypass the static type checking. This mean type errors will be caught at runtime instead of compile time.
  5. Exception handling: The static checks done by a compiler in static code have to be done by the programmers using exception handling code when reflections are used. Providing more exception handling code increases implementation effort and also adds significantly to the code length making it less readable.

Conclusion

Reflection allows the creation of more versatile and flexible programs through both introspective and intercessive features. These features include the ability for a program to know about itself, and to act on that information. Reflections can come in handy in implementing tools or technologies that involve the use of implementation details. But like any other language feature, reflection can be misused and should be used only when necessary.

References

[1] Sobel, J.M. and Daniel P. Friedman 1996 Reflection-Oriented Programming Indiana University, Bloomington, IN

[2] Smith, Brian Caldwell 1982 Procedural Reflection in Programming Languages MIT Press, Cambridge, MA

[3] Ducasse, Stéphane and Rmod Inria Lille Nord and Marcus Denker and Adrian Lienhard 2009 Evolving a Reflective Language Lessons Learned from Implementing Traits European Smalltalk User Group

[4] D. Bobrow, R. Gabriel, and J. White. CLOS in context—the shape of the design space. In A. Paepcke, editor, Object-Oriented Programming: the CLOS perspective, pages 29–61. MIT Press, 1993.

[5] Anh Nguyen-tuong and Steve J. Chapin and Andrew S. Grimshaw and Charlie Viles. 1998 Using Reflection for Flexibility and Extensibility in a Metacomputing Environment ORNL Central Research Library, Northrop Grumman Information Technologies Alexandria, VA

See Also

McHale, Ciaran 2008 Java Reflection Explained Simply

Using Java Reflection

Thomas, Dave with Chad Fowler and Andy Hunt. Programming Ruby. North Carolina: The Pragmatic Bookshelf, 2005.

References

<references />