Download Obfuscating Classes

Transcript
04 0672326388 CH03
4/9/04
2:34 PM
Page 35
35
Potential Problems and Common Solutions
The simplest solution is to configure the obfuscator to preserve the names of dynamically
loaded classes. The content of the class, such as the methods, variables, and code, can still be
transformed.
Reflection
Reflection requires compile-time knowledge of method and field names, so it is also affected
by obfuscation. Be sure to use a good obfuscator and to review the log file for warnings. Just
as with the dynamic class loading, if runtime errors are caused by obfuscation, you must
exclude from obfuscation the method or field names that are referenced in Class.getMethod
or Class.getField.
Serialization
Serialized Java objects include instance data and information about the class. If the version of
the class or its structure changes, a deserialization exception can result. Obfuscated classes
can be serialized and deserialized, but an attempt to deserialize an instance of a nonobfuscated class by an obfuscated class will fail. This is not a very common problem, and it can
usually be solved by excluding the serializable classes from obfuscation or avoiding the
mixing of serialized classes.
Naming Conventions Violation
The renaming of methods can violate design patterns such as Enterprise JavaBeans (EJB),
where the bean developer is required to provide methods with certain names and signatures.
EJB callback methods such as ejbCreate and ejbRemove are not defined by a super class or an
interface. Providing these methods with a specific signature is a mere convention prescribed
by EJB specification and enforced by the container. Changing callback method names violates
the naming convention and makes the bean unusable. You should always be sure to exclude
the names of such methods from obfuscation.
Maintenance Difficulties
Last, but not least, obfuscation makes maintaining and troubleshooting applications more
difficult. Java exception handling is an effective way of isolating the faulty code, and looking
at the stack trace can generally give you a good idea of what went wrong and where. Keeping
the debugging information for source filenames and line numbers enables the runtime to
report the exact location in code where the error occurred. If done carelessly, obfuscation can
inhibit this feature and make debugging harder because the developer sees only the obfuscated class names instead of the real class names and line numbers.