Sponsored Links
-->

Saturday, May 5, 2018

Advanced Java Programming Lecture 5 Reflection dr hab. Szymon ...
src: images.slideplayer.com

In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.


Video Reflection (computer programming)



Historical background

The earliest computers were programmed in their native assembly language, which were inherently reflective, as these original architectures could be programmed by defining instructions as data and using self-modifying code. As programming moved to compiled higher-level languages such as Algol, Cobol, and Fortran (but also Pascal and C and many other languages), this reflective ability largely disappeared until programming languages with reflection built into their type systems appeared.

Brian Cantwell Smith's 1982 doctoral dissertation introduced the notion of computational reflection in procedural programming languages and the notion of the meta-circular interpreter as a component of 3-Lisp.


Maps Reflection (computer programming)



Uses

Reflection helps programmers make generic software libraries to display data, process different formats of data, perform serialization or deserialization of data for communication, or do bundling and unbundling of data for containers or bursts of communication.

Effective use of reflection almost always requires a plan: A design framework, encoding description, object library, a map of a database or entity relations.

Reflection makes a language more suited to network-oriented code. For example, it assists languages such as Java to operate well in networks by enabling libraries for serialization, bundling and varying data formats. Languages without reflection (e.g. C) have to use auxiliary compilers, e.g. for Abstract Syntax Notation, to produce code for serialization and bundling.

Reflection can be used for observing and modifying program execution at runtime. A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal related to that enclosure. This is typically accomplished by dynamically assigning program code at runtime.

In object-oriented programming languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.

Reflection is often used as part of software testing, such as for the runtime creation/instantiation of mock objects.

Reflection is also a key strategy for metaprogramming.

In some object-oriented programming languages, such as C# and Java, reflection can be used to override member accessibility rules. For example, reflection makes it possible to change the value of a field marked "private" in a third-party library's class.


Modern computer on a white background. Mirror reflection ...
src: st3.depositphotos.com


Implementation

A language supporting reflection provides a number of features available at runtime that would otherwise be difficult to accomplish in a lower-level language. Some of these features are the abilities to:

  • Discover and modify source-code constructions (such as code blocks, classes, methods, protocols, etc.) as first-class objects at runtime.
  • Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.
  • Evaluate a string as if it were a source-code statement at runtime.
  • Create a new interpreter for the language's bytecode to give a new meaning or purpose for a programming construct.

These features can be implemented in different ways. In MOO, reflection forms a natural part of everyday programming idiom. When verbs (methods) are called, various variables such as verb (the name of the verb being called) and this (the object on which the verb is called) are populated to give the context of the call. Security is typically managed by accessing the caller stack programmatically: Since callers() is a list of the methods by which the current verb was eventually called, performing tests on callers()[1] (the command invoked by the original user) allows the verb to protect itself against unauthorised use.

Compiled languages rely on their runtime system to provide information about the source code. A compiled Objective-C executable, for example, records the names of all methods in a block of the executable, providing a table to correspond these with the underlying methods (or selectors for these methods) compiled into the program. In a compiled language that supports runtime creation of functions, such as Common Lisp, the runtime environment must include a compiler or an interpreter.

Reflection can be implemented for languages not having built-in reflection facilities by using a program transformation system to define automated source-code changes.


Weekly Programming and Reflection Diary | All Young Learners ...
src: i.pinimg.com


Examples

The following code snippets create an instance foo of class Foo and invoke its method PrintHello. For each programming language, normal and reflection-based call sequences are shown.

C#

The following is an example in C#:

Delphi

This Delphi example assumes that a TFoo class has been declared in a unit called Unit1:

This is a notable example, since Delphi is an unmanaged, fully natively compiled language, unlike most other languages that support reflection. Its language architecture inherits from strongly typed Pascal, but with significant influence from Smalltalk. Compare with the other examples here, many of which are dynamic or script languages like Perl, Python or PHP, or languages with a runtime like Java or C#.

eC

The following is an example in eC:

ECMAScript

The following is an example in ECMAScript, and therefore also applies to JavaScript and ActionScript:

Go

The following is an example in Go:

Java

The following is an example in Java:

Objective-C

The following is an example in Objective-C, implying either the OpenStep or Foundation Kit framework is used:

Perl

The following is an example in Perl:

PHP

The following is an example in PHP:

Python

The following is an example in Python:

R

The following is an example in R:

Ruby

The following is an example in Ruby:


Reflection methods in the Samaritan Demo รข€
src: s-media-cache-ak0.pinimg.com


See also

  • List of reflective programming languages and platforms
  • Mirror (programming)
  • Programming paradigms
  • Self-hosting
  • Self-modifying code
  • Type introspection

What is the concept of reflection in Java? - YouTube
src: i.ytimg.com


References

Notes

Documents

  • Jonathan M. Sobel and Daniel P. Friedman. An Introduction to Reflection-Oriented Programming (1996), Indiana University.
  • Anti-Refection technique using C# and C++/CLI wrapper to prevent code thief

Weekly Programming and Reflection Diary | OMG! My work is in print ...
src: s-media-cache-ak0.pinimg.com


Further reading

  • Ira R. Forman and Nate Forman, Java Reflection in Action (2005), ISBN 1-932394-18-4
  • Ira R. Forman and Scott Danforth, Putting Metaclasses to Work (1999), ISBN 0-201-43305-2

Everyday Spaciousness: A Reflection on Upaya's Resident Program by ...
src: i.pinimg.com


External links

  • Reflection in logic, functional and object-oriented programming: a short comparative study
  • An Introduction to Reflection-Oriented Programming
  • Brian Foote's pages on Reflection in Smalltalk
  • Java Reflection API Tutorial from Oracle

Source of article : Wikipedia