Reflection: solution of "unconventional" tasksI 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. <? $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: // http://php.net/manual/en/language.oop5.autoload.php ?>
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) { e.printStackTrace(); } // In JAVA loaded classes must be in the classpath at startup You can not talk about the Java reflection without mentioning about annotations.
clazz = Class.forName("some.package.SomeExampleClass"); for (Method method : clazz.getDeclaredMethods()) { if (method.getAnnotation(OnTimerTick.class) != null) { method.invoke(this); } } Next: How can this mechanism be used for "non-traditional" tasks. 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:
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? Sources: sources/KostaPC/reflection/java_reflection_encoder
|