Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use-cases for reflection

Tags:

Recently I was talking to a co-worker about C++ and lamented that there was no way to take a string with the name of a class field and extract the field with that name; in other words, it lacks reflection. He gave me a baffled look and asked when anyone would ever need to do such a thing.

Off the top of my head I didn't have a good answer for him, other than "hey, I need to do it right now". So I sat down and came up with a list of some of the things I've actually done with reflection in various languages. Unfortunately, most of my examples come from my web programming in Python, and I was hoping that the people here would have more examples. Here's the list I came up with:

  1. Given a config file with lines like
    x = "Hello World!"
    y = 5.0
    dynamically set the fields of some config object equal to the values in that file. (This was what I wished I could do in C++, but actually couldn't do.)

  2. When sorting a list of objects, sort based on an arbitrary attribute given that attribute's name from a config file or web request.

  3. When writing software that uses a network protocol, reflection lets you call methods based on string values from that protocol. For example, I wrote an IRC bot that would translate
    !some_command arg1 arg2
    into a method call actions.some_command(arg1, arg2) and print whatever that function returned back to the IRC channel.

  4. When using Python's __getattr__ function (which is sort of like method_missing in Ruby/Smalltalk) I was working with a class with a whole lot of statistics, such as late_total. For every statistic, I wanted to be able to add _percent to get that statistic as a percentage of the total things I was counting (for example, stats.late_total_percent). Reflection made this very easy.

So can anyone here give any examples from their own programming experiences of times when reflection has been helpful? The next time a co-worker asks me why I'd "ever want to do something like that" I'd like to be more prepared.

like image 395
Eli Courtwright Avatar asked Sep 08 '08 13:09

Eli Courtwright


People also ask

When should reflection be used?

Reflection allows programmer to access entities in program dynamically. i.e. while coding an application if programmer is unaware about a class or its methods, he can make use of such class dynamically (at run time) by using reflection. It is frequently used in scenarios where a class name changes frequently.

Where is reflection used?

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.

Why would you use reflection in Java?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.


1 Answers

I can list following usage for reflection:

  • Late binding
  • Security (introspect code for security reasons)
  • Code analysis
  • Dynamic typing (duck typing is not possible without reflection)
  • Metaprogramming

Some real-world usages of reflection from my personal experience:

  • Developed plugin system based on reflection
  • Used aspect-oriented programming model
  • Performed static code analysis
  • Used various Dependency Injection frameworks
  • ...

Reflection is good thing :)

like image 198
aku Avatar answered Sep 30 '22 04:09

aku