Reflection: solution of "unconventional" tasks

I will not chew all too much as the idea of reflection has nothing tricky, just about this mechanism is often overlooked. The person who wrote something harder than copypaste examples instantly on a pair of explanations and examples.

Reflection - is, in simple words, the way to handle programe your own code. The very concept of reflection in programming is now being used in the context of "managed code" - the code that's just executed in the interpreter, but not directly in compiled languages.

Reflexes are relatively common, but this design method has great advantages compared with standard design patterns. Reflection allows you to make extremely modular code without recompiling that has a different meaning in "managed code" in which there is no possibility in C/C++/ASM (etc) direct access to any location in memory and rewrite.

A typical use of reflection - is to download the desired class or a method based on the incoming data, to implement a feature was not necessary to append the new conditions in an if or switch.

A few simple examples of the use of this approach in JAVA and PHP.

------ PHP ------

$processor = new RequestProcessor();
$funcname = RequestProcessor::FUNC_PREFIX.$_GET['action'];
if(method_exists($processor, $funcname)) {
echo $processor->$funcname($xpath, $name[0]);
// PHP to dynamically load the classes is possible through the built-in mechanisms:

------ JAVA ------

Class clazz = null;
try {
clazz = Class.forName("some.package.TheActionsClass");
Object obj = clazz.newInstance();
Method actionMethod = clazz.getDeclaredMethod(anAction);
String result = (String) actionMethod.invoke(obj);
System.out.println("result: "+result);
} catch (
ClassNotFoundException | InstantiationException |
IllegalAccessException | NoSuchMethodException |
InvocationTargetException e)
// In JAVA loaded classes must be in the classpath at startup

You can not talk about the Java reflection without mentioning about annotations.
In Java, there is a mechanism such as annotations (java annotations) allows to parameterize code separate from its content. This built on the many technologies (JPA, for example), greatly simplifies the development.
Abstract can be delivered to the classical, field and method, and there in order to set their own descriptions of the code and to use it in any convenient manner without additional structures.
A simple example:
Add to any method in the code annotation @OnTimeTick, further with the help of reflection mechanism can get all of these methods and respectively call them by triggering a timer.

------ JAVA ------

clazz = Class.forName("some.package.SomeExampleClass");
for (Method method : clazz.getDeclaredMethods()) {
    if (method.getAnnotation(OnTimerTick.class) != null) {

Next: How can this mechanism be used for "non-traditional" tasks.
Java has the one property, which is sometimes very, very frustrating developers - it is easy to decompile. Most often, this escape code obfuscation that does not solve the main task - to compile, but makes code analysis кода demotivating complicated, but even so feasible in a reasonable time.

The very structure of the organization compiled code (byte-code) is quite simple and in itself can give some information Application without decompiling - whether jar (zip folder, in fact), or just the class files. Moreover, even the last code obfuscation has compiled in the form of patterns, which may be the use of such antivirus software.

What does the reflection can help? Reflection in java allows you to create your own class loader, where the input we get the binary data stream, and the output must provide a copy of the desired class. Thus the actual byte code can be encrypted in any way, which only enough imagination.

In this folder shows the simplest version of the bootloader. The code consists of two parts:

  • cryptographer - a class that encrypts byte code and packages to file additional information necessary to load the class.
  • loader - a class that loads encrypted file and decrypts and runs the method main(), so that the encrypted class will run just as well as without encryption.

As an example of encryption is used strictly for BASE64. Such a pair cryptographer downloader allows you to encrypt and upload any compiled class file. Not complicated revision will thus packing in a single file the whole structure of classes.

Where it can be useful?
If you are sending the code over open networks, such as when the code is a functional robot and add to the load should be placed on the open resources on pastebin, for example.

Sources: sources/KostaPC/reflection/java_reflection_encoder


Inception E-Zine