Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Modifying an Application without Modifying Its Source Code

Curt Wu and Steve Marotta are software engineers at Charles River Analytics (www.cra.com).

Developers using third-party applications or libraries often face the challenge of customizing that software to suit the task at hand. Sometimes the application performs exactly as designed, but still doesn't quite do what is required in a unique situation, and no amount of parameter tweaking does the trick. In this case, customization can be especially challenging if the third-party application is not open-source and offers limited extension points.

But even if source code is available, there may be good reasons not to modify it. For instance, one reason to use third-party code is to avoid reinventing the wheel and to automatically get that fancy new tire upgrade when Wheel 2.0 is released. Direct modification of code means that you've created a fork in the source code, and customizations will need to be manually merged with any subsequent versions that are released. Keeping the original application intact generally provides greater flexibility in the deployment of any modified code. For example, if the new code can be kept distinct and decoupled from the original application source code, then it is easier to port these changes to other applications. This let developers apply the same behavioral changes to multiple applications from different vendors. In this case, any additional code must be inserted in such a way that the original application will execute as-is, blissfully unaware that its behavior has been modified.

For a concrete example, let's examine the use of sockets in Java. Suppose we wanted to modify how a Java application communicates over a network to simulate low bandwidth or poor connection quality. We can achieve this by modifying or replacing Java's Socket class. Although inter-host communication between Java applications does not require Java Sockets, in practice, nearly all such communication leverages the Socket library found in the JRE's java.net package -- either directly or indirectly (e.g., through RMI classes). To slow down communications and simulate a slower network, we want to introduce a carefully controlled delay into each socket data stream. How can we do this without access to the application source code? There are several possible ways to do this, and we will discuss two approaches that we found useful for our modified socket problem.

By Your Own Bootstraps

One point of insertion of new code for an application is at its boundaries -- where it interfaces with external libraries. If the changes we would like to make can be made within these external libraries (e.g., communication over sockets), then replacing an external library with one that incorporates customized code would modify behavior without modifying the main application. Provided you have the means to modify and rebuild it, replacing a typical library found on the classpath is straightforward. However, the Socket class is part of the JRE in rt.jar, and modifying the JRE creates some complications.

If we want to modify a class in the JRE, there is a direct approach and a more subtle replacement approach. The direct approach is to build a custom JRE by, for example, replacing the socket-related classes within rt.jar. While this works, replacing the JRE limits the portability of the solution to other environments because multiple JREs would need to be maintained. A similar but more maintainable solution is to modify a JRE at runtime by adding a specific class to the bootstrap classloader that supersedes the behavior of the same class within rt.jar. For those unfamiliar with the bootstrap classloader, it is the primordial classloader that loads the JRE classes and is distinct from the system classloader, which loads classes from the classpath and is lower in the classpath hierarchy.

For our socket modification example, we would create a modified Socket class with the same name and interface as the original Socket but with different implementation that meets our needs. The name and interface must be identical because this new Socket will be a drop-in replacement. Science-fiction fans may recognize this as the "evil twin" approach, where clients of the Socket are unaware that beneath the same familiar exterior lurks a replacement implementation. Once we have the modified Socket, we simply prepend this new class to the bootstrap classpath, and the new Socket essentially replaces the original Socket for any code within the JVM that invokes it (see Example 1). Note that the new class must be prepended to the bootstrap classpath, so it takes precedence over the original Socket class found later in the path in rt.jar.

java -cp thirdparty.jar -Xbootclasspath/p:custom.jar; com.cra.MyApp

Example 1: Modifying the behavior of thirdparty.jar by prepending custom.jar to the bootstrap classpath

The downside of this modified JRE approach is that the changes to the libraries are JVM-wide and thus all code is stuck using these modified libraries. Also, because the modified code must be on the bootstrap classloader, all code that supports the modification (i.e., dependencies) must also be loaded by this classloader. If the application requires a custom classloader or is otherwise incompatible with the bootstrap classloader, and the modified code depends on code within the application, then this may not be feasible. Finally, the issue that is possibly most troubling is that we are modifying the internals of the JRE. Because we are replacing the implementation of classes in the JRE, any solution is highly dependent on the JRE version, and thus brittle. To support multiple JREs, we would need to maintain an entire family of evil twins, and any future changes to our modified code would need to be duplicated across the entire family.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.