Reflection - RetroGuard Documentation


Prev   Contents   Next

Use of reflection may cause your code to behave incorrectly after obfuscation. No obfuscator can automatically solve this problem without storing the entire name mapping table in your Jar archive, because the class, method, or field name can be constructed or changed in a way that is only known at run-time.

In the case of Class.forName(className) and the className.class structure (which, for pre-JDK5 code, compiles to Class.forName(className) behind the scenes), it is possible for RetroGuard to remap the class names automatically. This is true only where a simple string is passed to Class.forName. Class names constructed dynamically at run-time cannot be remapped automatically and so the resulting class names must be reserved from obfuscation in the script file.

To switch on automatic .class and Class.forName string mapping, add this option to your script:

.option MapClassString

If any of the following method calls are detected during obfuscation, a warning is posted to the log file.

  • In class java.lang.Class the methods:
    • Class forName(String className);
    • Field getDeclaredField(String name);
    • Field getField(String name);
    • Method getDeclaredMethod(String name, Class[] parameterTypes);
    • Method getMethod(String name, Class[] parameterTypes);
  • In class java.lang.ClassLoader the methods:
    • Class defineClass(String name, byte[] data, int offset, int length);
    • Class findLoadedClass(String name);
    • Class loadClass(String name);
    • Class loadClass(String name, boolean resolve);

When these warnings are found in the obfuscation log, you should examine your source code to determine if the introspection methods act only on classes, methods and fields outside of the JAR. If so, they will cause no problems.

If the methods can refer to classes, methods or fields within your Jar file, run-time problems can arise because these identifiers may have been obfuscated. In that case, the solution is to reserve these identifiers using the script file, so that obfuscation does not change them.

[RetroGuard-v2.2.x only]

In either case, once you have dealt with introspection warnings from a class, you may choose to suppress the warnings in future runs using the script line:

.nowarn com/pk/MyClass

Prev   Contents   Next
 Copyright © 1998-2007 Retrologic Systems.
 All rights reserved.