Information Technology Reference
In-Depth Information
One important point in this code snippet is the snapshotIterator() method
that should be used if statements in the method's body will be changed while
the loop runs. This avoids ConcurrentModificationException s.
6.1 Removing Statements
The fact that all statements in the body of a method are stored into a chain
makes it very easy to remove a complete statement from the Jimple code. This
can be done by just removing it from the chain:
body.getUnits().remove(unit);
After removing statements, dead code can remain. Soot already offers optimiza-
tions for removing such code, propagating definitions that are only used once,
and others. If the subsequent Jimple optimization pack ( jop ) is enabled, those
optimizations are applied automatically.
6.2 Adding New Statements
In general, the unit chain allows new statements to be placed before
( insertBefore() )orafter( insertAfter() ) a specific code point. These must
be fully-constructed Jimple units containing all required expressions, i.e., the
operands in case of a primitive arithmetic operation. The Jimple.v() singleton
provides factory methods called Jimple.v().newX for generating new Jimple
statments and expressions where X stands for the different kinds of AST ele-
ments. An example is newStaticInvokeExpr() which creates a new static in-
voke expression to be used inside an invoke statement or as the right side of an
assignment.
Let us go back to our original SMS Messenger Example (c.f. Section 2.2)
and insert some checks that prevent the application from sending SMS mes-
sages to premium rate numbers. This check has to be placed before the
sendTextMessage() method in line 30 and could look like the one described
in Listing 1.13 for premium rate numbers that start with 0900.
1 if (!phoneNr.getText().toString().startsWith( "0900" ))
2
smsManager.sendTextMessage(phoneNr.getText().toString(), null ,
3
message.getText().toString(), null , null );
Listing 1.13. 0900 Premium Rate SMS Check
Before this statement can be constructed, various expressions must be generated:
- String constant for the number “0900”
- Method call to the startsWith() method. The result must be stored in a
new local variable that does not conflict with any existing local variable.
- “if” statement with then and else branch
A complete example for integrating such a check is described in Listing 1.14.
 
Search WWH ::




Custom Search