[Soot-list] Bad use of primitive type when performing transformations needed

Quentin Sabah quentin.sabah at inria.fr
Sun Jun 9 10:26:25 EDT 2013


> 1    tmpInt = new java.lang.Integer
> 2    specialinvoke tmpInt.<java.lang.Integer: void <init>(int)>(17)
> 3    j = virtualinvoke var1.<java.lang.String: int length()>()
> 4    tmpInt = tmpInt + j
> 5    tmpInt = staticinvoke <java.lang.Integer: java.lang.Integer valueOf(int)>(tmpInt)
> 6    virtualinvoke info.<java.util.ArrayList: boolean add(java.lang.Object)>(tmpInt)

There are several problems with this code:
- Line 4, you cannot add an Integer (tmpInt) and an int, first you need to convert the Integer into an int with Integer.intValue(). And the result of line 4 should be stored in an int variable. 
- At line 5, you cannot invoke valueOf(int) with an Integer parameter.

You should have something like:
4a  k = virtualinvoke tmpInt.<java.lang.Integer: int intValue()>()
4b  j = k + j
5   tmpInt = staticinvoke <java.lang.Integer: java.lang.Integer valueOf(int)>(j)

Of course, when you write pure Java code, these conversions are done automatically by javac, but in jimple you must generate these yourself.

When you encounter this kind of problems, 
I suggest you to write a small example in pure Java like:

  public static void main(String [] args) {
    Integer i = new Integer(17);
    int j = 14;
    i = i + j;
  }

Then compile it with javac, and observe the produced bytecode with "javap -c". You'll observe the various conversions added by the compiler:

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/Integer
       3: dup           
       4: bipush        17
       6: invokespecial #3                  // Method java/lang/Integer."<init>":(I)V
       9: astore_1      
      10: bipush        14
      12: istore_2      
      13: aload_1       
      14: invokevirtual #4                  // Method java/lang/Integer.intValue:()I
      17: iload_2       
      18: iadd          
      19: invokestatic  #5                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      22: astore_1      
      23: return

-- 
Quentin Sabah, CIFRE Ph.D. student
Grenoble University
INRIA-SARDES                   | STMicroelectronics/AST
Montbonnot, France             | Grenoble, France
mailto:quentin.sabah at inria.fr  | mailto:quentin.sabah at st.com
phone: +33 476 61 52 42        | phone: +33 476 58 44 14



More information about the Soot-list mailing list