Reflective programming
Template:Short description Template:Distinguish
In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.<ref>Template:Citation</ref>
Historical background
The earliest computers were programmed in their native assembly languages, which were inherently reflective, as these original architectures could be programmed by defining instructions as data and using self-modifying code. As the bulk of programming moved to higher-level compiled languages such as ALGOL, COBOL, Fortran, Pascal, and C, this reflective ability largely disappeared until new programming languages with reflection built into their type systems appeared.Template:Citation needed
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.<ref>Brian Cantwell Smith, Procedural Reflection in Programming Languages, Department of Electrical Engineering and Computer Science, Massachusetts Institute of Technology, PhD dissertation, 1982.</ref><ref>Brian C. Smith. Reflection and semantics in a procedural language Template:Webarchive. Technical Report MIT-LCS-TR-272, Massachusetts Institute of Technology, Cambridge, Massachusetts, January 1982.</ref>
Uses
Reflection helps programmers make generic software libraries to display data, process different formats of data, perform serialization and 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 such as C are required to use auxiliary compilers for tasks like 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 of 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 bypass member accessibility rules. For C#-properties this can be achieved by writing directly onto the (usually invisible) backing field of a non-public property. It is also possible to find non-public methods of classes and types and manually invoke them. This works for project-internal files as well as external libraries such as .NET's assemblies and Java's archives.
Implementation
Template:Unreferenced section A language that supports 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()[0] (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 without built-in reflection by using a program transformation system to define automated source-code changes.
Security considerations
Reflection may allow a user to create unexpected control flow paths through an application, potentially bypassing security measures. This may be exploited by attackers.<ref>Template:Cite report</ref> Historical vulnerabilities in Java caused by unsafe reflection allowed code retrieved from potentially untrusted remote machines to break out of the Java sandbox security mechanism. A large scale study of 120 Java vulnerabilities in 2013 concluded that unsafe reflection is the most common vulnerability in Java, though not the most exploited.<ref>Template:Cite magazine</ref>
Examples
The following code snippets create an instance <syntaxhighlight lang="text" class="" style="" inline="1">foo</syntaxhighlight> of class <syntaxhighlight lang="text" class="" style="" inline="1">Foo</syntaxhighlight> and invoke its method <syntaxhighlight lang="text" class="" style="" inline="1">PrintHello</syntaxhighlight>. For each programming language, normal and reflection-based call sequences are shown.
Common Lisp
The following is an example in Common Lisp using the Common Lisp Object System:
<syntaxhighlight lang="lisp"> (defclass foo () ()) (defmethod print-hello ((f foo)) (format T "Hello from ~S~%" f))
- Normal, without reflection
(let ((foo (make-instance 'foo)))
(print-hello foo))
- With reflection to look up the class named "foo" and the method
- named "print-hello" that specializes on "foo".
(let* ((foo-class (find-class (read-from-string "foo")))
(print-hello-method (find-method (symbol-function (read-from-string "print-hello"))
nil (list foo-class))))
(funcall (sb-mop:method-generic-function print-hello-method)
(make-instance foo-class)))
</syntaxhighlight>
C
Reflection is not possible in C, though parts of reflection can be emulated.
<syntaxhighlight lang="c">
- include <stdio.h>
- include <stdlib.h>
- include <string.h>
typedef struct {
// ...
} Foo;
typedef void (*Method)(void*);
// The method: Foo::printHello void Foo_printHello(maybe_unused void* instance) {
(void)instance; // Instance ignored for a static method
printf("Hello, world!\n");
}
// Simulated method table typedef struct {
const char* name; Method fn;
} MethodEntry;
MethodEntry fooMethods[] = {
{ "printHello", Foo_printHello },
{ NULL, NULL } // Sentinel to mark end
};
// Simulate reflective method lookup nodiscard Method findMethodByName(const char* name) {
for (size_t i = 0; fooMethods[i].name; i++) {
if (strcmp(fooMethods[i].name, name) == 0) {
return fooMethods[i].fn;
}
}
return NULL;
}
int main() {
// Without reflection Foo fooInstance; Foo_printHello(&fooInstance);
// With emulated reflection
Foo* fooReflected = malloc(sizeof(Foo));
if (!fooReflected) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
const char* methodName = "printHello"; Method m = findMethodByName(methodName);
if (m) {
m(fooReflected);
} else {
fprintf(stderr, "Method '%s' not found\n", methodName);
}
free(fooReflected); return 0;
} </syntaxhighlight>
C++
The following is an example in C++ (using reflection added in C++26).
<syntaxhighlight lang="cpp"> import std;
using std::string_view; using std::vector; using std::meta::access_context; using std::meta::exception; using std::meta::info; using std::views::filter;
nodiscard consteval bool isNonstaticMethod(info mem) noexcept {
return std::meta::is_class_member(mem)
&& !std::meta::is_static_member(mem)
&& std::meta::is_function(mem);
}
nodiscard consteval info findMethod(info ty, const char* name) {
constexpr auto ctx = access_context::current();
vector<info> members = std::meta::members_of(ty, ctx);
for (info member : members | filter(isNonstaticMethod)) {
if (std::meta::identifier_of(member) == name) {
return member;
}
}
throw exception("Failed to retrieve method of M!", ^^findMethod);
}
template <info Ty, auto Name> constexpr auto createInvokerImpl = []() -> auto {
using Type = [: Ty :];
static constexpr info M = findMethod(Ty, Name);
static_assert(
std::meta::parameters_of(M).size() == 0 &&
std::meta::return_type_of(M) == ^^void
);
return [](Type& instance) -> void { instance.[: M :](); };
}();
nodiscard consteval info createInvoker(info ty, string_view name) {
return std::meta::substitute(
^^createInvokerImpl,
{ std::meta::reflect_constant(ty), std::meta::reflect_constant_string(name) }
);
}
class Foo { private:
// ...
public:
Foo() = default;
void printHello() const noexcept {
std::println("Hello, world!");
}
};
int main(int argc, char* argv[]) {
Foo foo;
// Without reflection foo.printHello();
// With reflection auto invokePrint = [: createInvoker(^^Foo, "printHello") :]; invokePrint(foo);
return 0;
} </syntaxhighlight>
C#
The following is an example in C#:
<syntaxhighlight lang="c#"> namespace Wikipedia.Examples;
using System; using System.Reflection;
class Foo {
// ...
public Foo() {}
public void PrintHello()
{
Console.WriteLine("Hello, world!");
}
}
public class InvokeFooExample {
static void Main(string[] args)
{
// Without reflection
Foo foo = new();
foo.PrintHello();
// With reflection
Object foo = Activator.CreateInstance(typeof(Foo));
MethodInfo method = foo.GetType().GetMethod("PrintHello");
method.Invoke(foo, null);
}
} </syntaxhighlight>
Delphi, Object Pascal
This Delphi and Object Pascal example assumes that a Template:Mono class has been declared in a unit called Template:Mono:
<syntaxhighlight lang="Delphi"> uses RTTI, Unit1;
procedure WithoutReflection; var
Foo: TFoo;
begin
Foo := TFoo.Create; try Foo.Hello; finally Foo.Free; end;
end;
procedure WithReflection; var
RttiContext: TRttiContext; RttiType: TRttiInstanceType; Foo: TObject;
begin
RttiType := RttiContext.FindType('Unit1.TFoo') as TRttiInstanceType;
Foo := RttiType.GetMethod('Create').Invoke(RttiType.MetaclassType, []).AsObject;
try
RttiType.GetMethod('Hello').Invoke(Foo, []);
finally
Foo.Free;
end;
end; </syntaxhighlight>
eC
The following is an example in eC:
<syntaxhighlight lang=eC> // Without reflection Foo foo{}; foo.hello();
// With reflection Class fooClass = eSystem_FindClass(__thisModule, "Foo"); Instance foo = eInstance_New(fooClass); Method m = eClass_FindMethod(fooClass, "hello", fooClass.module); ((void(*)())(void*)m.function)(foo); </syntaxhighlight>
Go
The following is an example in Go:
<syntaxhighlight lang="go"> import (
"fmt" "reflect"
)
type Foo struct{}
func (f Foo) Hello() {
fmt.Println("Hello, world!")
}
func main() {
// Without reflection var f Foo f.Hello()
// With reflection
var fT reflect.Type = reflect.TypeOf(Foo{})
var fV reflect.Value = reflect.New(fT)
var m reflect.Value = fV.MethodByName("Hello")
if m.IsValid() {
m.Call(nil)
} else {
fmt.Println("Method not found")
}
} </syntaxhighlight>
Java
The following is an example in Java: <syntaxhighlight lang="java"> package org.wikipedia.example;
import java.lang.reflect.Method;
class Foo {
// ...
public Foo() {}
public void printHello() {
System.out.println("Hello, world!");
}
}
public class InvokeFooExample {
public static void main(String[] args) {
// Without reflection
Foo foo = new Foo();
foo.printHello();
// With reflection
try {
Foo foo = Foo.class.getDeclaredConstructor().newInstance();
Method m = foo.getClass().getDeclaredMethod("printHello", new Class<?>[0]);
m.invoke(foo);
} catch (ReflectiveOperationException e) {
System.err.printf("An error occurred: %s%n", e.getMessage());
}
}
} </syntaxhighlight>
Java also provides an internal class (not officially in the Java Class Library) in module jdk.unsupported, sun.reflect.Reflection which is used by sun.misc.Unsafe. It contains one method, Template:Java for obtaining the class making a call at a specified depth.<ref>Template:Cite web</ref> This is now superseded by using the class java.lang.StackWalker.StackFrame and its method Template:Java.
JavaScript/TypeScript
The following is an example in JavaScript:
<syntaxhighlight lang="javascript"> import 'reflect-metadata';
// Without reflection const foo = new Foo(); foo.hello();
// With reflection const foo = Reflect.construct(Foo); const hello = Reflect.get(foo, 'hello'); Reflect.apply(hello, foo, []);
// With eval eval('new Foo().hello()'); </syntaxhighlight>
The following is the same example in TypeScript:
<syntaxhighlight lang="typescript"> import 'reflect-metadata';
// Without reflection const foo: Foo = new Foo(); foo.hello();
// With reflection const foo: Foo = Reflect.construct(Foo); const hello: (this: Foo) => void = Reflect.get(foo, 'hello') as (this: Foo) => void; Reflect.apply(hello, foo, []);
// With eval eval('new Foo().hello()'); </syntaxhighlight>
Julia
The following is an example in Julia: <syntaxhighlight lang="julia-repl"> julia> struct Point
x::Int
y
end
- Inspection with reflection
julia> fieldnames(Point) (:x, :y)
julia> fieldtypes(Point) (Int64, Any)
julia> p = Point(3,4)
- Access with reflection
julia> getfield(p, :x) 3 </syntaxhighlight>
Objective-C
The following is an example in Objective-C, implying either the OpenStep or Foundation Kit framework is used:
<syntaxhighlight lang="ObjC"> // Foo class. @interface Foo : NSObject - (void)hello; @end
// Sending "hello" to a Foo instance without reflection. Foo* obj = [[Foo alloc] init]; [obj hello];
// Sending "hello" to a Foo instance with reflection. id obj = [[NSClassFromString(@"Foo") alloc] init]; [obj performSelector: @selector(hello)]; </syntaxhighlight>
Perl
The following is an example in Perl:
<syntaxhighlight lang="perl">
- Without reflection
my $foo = Foo->new; $foo->hello;
- or
Foo->new->hello;
- With reflection
my $class = "Foo" my $constructor = "new"; my $method = "hello";
my $f = $class->$constructor; $f->$method;
- or
$class->$constructor->$method;
- with eval
eval "new Foo->hello;"; </syntaxhighlight>
PHP
The following is an example in PHP:<ref>Template:Cite web</ref>
<syntaxhighlight lang="php"> // Without reflection $foo = new Foo(); $foo->hello();
// With reflection, using Reflections API $reflector = new ReflectionClass("Foo"); $foo = $reflector->newInstance(); $hello = $reflector->getMethod("hello"); $hello->invoke($foo); </syntaxhighlight>
Python
The following is an example in Python:
<syntaxhighlight lang="python"> from typing import Any
class Foo:
# ...
def print_hello() -> None:
print("Hello, world!")
if __name__ == "__main__":
# Without reflection obj: Foo = Foo() obj.print_hello()
# With reflection obj: Foo = globals()["Foo"]() _: Any = getattr(obj, "print_hello")()
# With eval
eval("Foo().print_hello()")
</syntaxhighlight>
R
The following is an example in R:
<syntaxhighlight lang="r">
- Without reflection, assuming foo() returns an S3-type object that has method "hello"
obj <- foo() hello(obj)
- With reflection
class_name <- "foo" generic_having_foo_method <- "hello" obj <- do.call(class_name, list()) do.call(generic_having_foo_method, alist(obj)) </syntaxhighlight>
Ruby
The following is an example in Ruby:
<syntaxhighlight lang="ruby">
- Without reflection
obj = Foo.new obj.hello
- With reflection
obj = Object.const_get("Foo").new obj.send :hello
- With eval
eval "Foo.new.hello" </syntaxhighlight>
Rust
Rust does not have compile-time reflection in the standard library, but it is possible using some third-party libraries such as "Template:Mono".<ref>Template:Cite web</ref>
<syntaxhighlight lang="rust"> use std::any::TypeId;
use bevy_reflect::prelude::*; use bevy_reflect::{
FunctionRegistry, GetTypeRegistration, Reflect, ReflectFunction, ReflectFunctionRegistry, ReflectMut, ReflectRef, TypeRegistry
};
- [derive(Reflect)]
- [reflect(DoFoo)]
struct Foo {
// ...
}
impl Foo {
fn new() -> Self {
Foo {}
}
fn print_hello(&self) {
println!("Hello, world!");
}
}
- [reflect_trait]
trait DoFoo {
fn print_hello(&self);
}
impl DoFoo for Foo {
fn print_hello(&self) {
self.print_hello();
}
}
fn main() {
// Without reflection let foo: Foo = Foo::new(); foo.print_hello();
// With reflection let mut registry: TypeRegistry = TypeRegistry::default();
registry.register::<Foo>(); registry.register_type_data::<Foo, ReflectFunctionRegistry>(); registry.register_type_data::<Foo, ReflectDoFoo>();
let foo: Foo = Foo; let reflect_foo: Box<dyn Reflect> = Box::new(foo);
// Version 1: call hello by trait
let trait_registration: &ReflectDoFoo = registry
.get_type_data::<ReflectDoFoo>(TypeId::of::<Foo>())
.expect("ReflectDoFoo not found for Foo");
let trait_object: &dyn DoFoo = trait_registration
.get(&*reflect_foo)
.expect("Failed to get DoFoo trait object");
trait_object.print_hello();
// Version 2: call hello by function name
let func_registry: &FunctionRegistry = registry
.get_type_data::<FunctionRegistry>(TypeId::of::<Foo>())
.expect("FunctionRegistry not found for Foo");
if let Some(dyn_func) = func_registry.get("print_hello") {
let result: Option<Box<dyn Reflect>> = dyn_func
.call(&*reflect_foo, Vec::<Box<dyn Reflect>>::new())
.ok();
if result.is_none() {
println!("Function called, no result returned (as expected for void return)");
}
} else {
println!("No function named hello found in FunctionRegistry");
}
} </syntaxhighlight>
Xojo
The following is an example using Xojo:
<syntaxhighlight lang="vbnet"> ' Without reflection Dim fooInstance As New Foo fooInstance.PrintHello
' With reflection Dim classInfo As Introspection.Typeinfo = GetTypeInfo(Foo) Dim constructors() As Introspection.ConstructorInfo = classInfo.GetConstructors Dim fooInstance As Foo = constructors(0).Invoke Dim methods() As Introspection.MethodInfo = classInfo.GetMethods For Each m As Introspection.MethodInfo In methods
If m.Name = "PrintHello" Then m.Invoke(fooInstance) End If
Next </syntaxhighlight>
See also
- List of reflective programming languages and platforms
- Mirror (programming)
- Programming paradigms
- Self-hosting (compilers)
- Self-modifying code
- Type introspection
- typeof
References
Citations
Sources
- Jonathan M. Sobel and Daniel P. Friedman. An Introduction to Reflection-Oriented Programming (1996), Indiana University.
- Anti-Reflection technique using C# and C++/CLI wrapper to prevent code thief
Further reading
- Ira R. Forman and Nate Forman, Java Reflection in Action (2005), Template:ISBN
- Ira R. Forman and Scott Danforth, Putting Metaclasses to Work (1999), Template:ISBN
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
Template:Programming paradigms navbox Template:Types of programming languages
- Programming constructs
- Programming language comparisons
- Articles with example BASIC code
- Articles with example C code
- Articles with example C Sharp code
- Articles with example Java code
- Articles with example JavaScript code
- Articles with example Julia code
- Articles with example Lisp (programming language) code
- Articles with example Objective-C code
- Articles with example Pascal code
- Articles with example Perl code
- Articles with example PHP code
- Articles with example Python (programming language) code
- Articles with example R code
- Articles with example Ruby code