Java code can load classes dynamically at runtime with the forName() method of java.lang.Class. PreEmptive Protection™ DashO™ can't always determine which classes your code loads this way. If it renames or removes a class your code loads by name, your protected application fails at runtime. This article explains how to tell DashO which classes to keep.
Why DashO Can't Always Detect Dynamic Loading
Consider the following code:
public Object getNewClass() {
String newClassName = getUserInputString();
try {
Object newClass = Class.forName(newClassName).newInstance();
return newClass;
} catch(Exception e) {
// handle
}
}This code loads a class by name and creates an instance of it. The name comes from user input, so DashO has no way to predict which classes the user will request. The solution is to keep the names of all classes the code could load. DashO can still rename their methods and fields. This requires manual configuration.
Choose an Approach
| Situation | What to do |
| You know which classes can load, and they share a base class or interface. | Nothing. DashO detects the pattern automatically. See Predictable Dynamic Loading. |
| You know which classes can load, but they use another creation pattern. | Add each class as a <classes> entry point. See Predictable Dynamic Loading. |
| You don't know which classes can load when you protect the application. | Add the existing classes as <unconditional> entry points. See Unpredictable Dynamic Loading. |
| You aren't sure where your code loads classes dynamically. | Enable fornamedetection and review the report. See Find Dynamic Loading with the Reflection Report. |
Predictable Dynamic-Loading
This is the simplest case: you know your application well enough to know exactly which classes it can load dynamically.
If the classes share a base class or common interface, DashO detects the pattern automatically and keeps all classes of that type. In this example, DashO keeps all Shape classes:
String s = getShapeName();
Shape myShape = (Shape)Class.forName(s).newInstance();
myShape.draw();If your code uses a different creation pattern, add each class to the <entrypoints> section of your project file:
<entrypoints>
<classes name="Triangle"/>
<classes name="Rectangle"/>
</entrypoints>DashO can still remove unused methods from the Shape hierarchy.
Unpredictable Dynamic-Loading
Sometimes you can't know which classes will load when you protect the application. For example, a user interface builder may let its users add their own or third-party components. In this case, add the existing classes as unconditional entry points:
<entrypoints>
<unconditional name="Triangle"/>
<unconditional name="Rectangle"/>
</entrypoints>For each unconditional entry point:
- DashO doesn't remove any of its methods or fields, regardless of your Removal options.
- DashO doesn't rename the class or its members, regardless of your Renaming options.
- DashO treats every method in the class as an entry point method.
These rules keep your interface to as-yet-unknown classes intact.
Find Dynamic Loading with the Reflection Report
The fornamedetection option handles most or all dynamically loaded classes.
-
Add the option to the <global> section of your project file:
<global> <option>fornamedetection</option> </global> - Build your project. DashO reports every place it finds
forName(), both in the report file and in the output after dependency analysis. -
Review each entry.
NOTE: Reflection use public void com.yoyodyne.Application.getInterface() – java.lang.Class.newInstance() - [BaseInterface Possible: InterfaceImplementor] Reflection use public boolean com.yoyodyne.Test.connect() - Class.forName() Reflection use public float com.yoyodyne.Test.calculate() - Class.forName() - [com.yoyodyne.Linker] - For each entry where DashO can't determine the loaded class, add that class as a
<classes>entry point in the <entrypoints> section. - Save your project file and build it again.
Limitations and Troubleshooting
| Limitation or issue | Details |
| The protected application fails to load a class at runtime. | DashO renamed or removed a dynamically loaded class. Add it as an entry point. |
| DashO produces no output classes or JAR files. | DashO found reflection usage, and the force global option isn't set. |
| The reflection report doesn't name a class for a method. | DashO can't determine it. Configure the class manually. |
| Unused code in a dynamically loaded class isn't removed. | Unconditional entry points keep all methods and fields. If you know which classes load, use <classes> entry points instead. |