What is reflection, and why is it useful?
I\'m particularly interested in Java, but I assume the principles are the same in any language.
From java documentation page
java.lang.reflect
package provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.
AccessibleObject
allows suppression of access checks if the necessary ReflectPermission
is available.
Classes in this package, along with java.lang.Class
accommodate applications such as debuggers, interpreters, object inspectors, class browsers, and services such as Object Serialization
and JavaBeans
that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class
It includes following functionality.
Have a look at this documentation link for the methods exposed by Class
class.
From this article (by Dennis Sosnoski, President, Sosnoski Software Solutions, Inc) and this article (security-explorations pdf):
I can see considerable drawbacks than uses of using Reflection
User of Reflection:
Drawbacks of Reflection:
General abuses:
Have a look at this SE question regarding abuse of reflection feature:
How do I read a private field in Java?
Summary:
Insecure use of its functions conducted from within a system code can also easily lead to the compromise of a Java security model. So use this feature sparingly