/* Soot - a Java Optimization Framework
 * Copyright (C) 2012 Michael Markert, Frank Hartmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package soot.dex.instructions;

import static soot.dex.Util.dottedClassName;

import java.util.HashSet;
import java.util.Set;

import org.jf.dexlib.TypeIdItem;
import org.jf.dexlib.Code.Instruction;
import org.jf.dexlib.Code.InstructionWithReference;
import org.jf.dexlib.Code.TwoRegisterInstruction;
import org.jf.dexlib.Code.Format.Instruction22c;

import soot.RefType;
import soot.dex.DexBody;
import soot.dex.DexType;
import soot.dex.DvkTyper;
import soot.jimple.AssignStmt;
import soot.jimple.InstanceOfExpr;
import soot.jimple.Jimple;
import soot.jimple.internal.JAssignStmt;

public class InstanceOfInstruction extends DexlibAbstractInstruction {

    public InstanceOfInstruction (Instruction instruction, int codeAdress) {
        super(instruction, codeAdress);
    }

    public void jimplify (DexBody body) {
        Instruction22c i = (Instruction22c)instruction;
        int dest = i.getRegisterA();
        int source = i.getRegisterB();
        String className = dottedClassName(((TypeIdItem)(i.getReferencedItem())).getTypeDescriptor());
        InstanceOfExpr e = Jimple.v().newInstanceOfExpr(body.getRegisterLocal(source), RefType.v(className));
        AssignStmt assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), e);
        defineBlock(assign);
        tagWithLineNumber(assign);
        body.add(assign);
        if (DvkTyper.ENABLE_DVKTYPER) {
          int op = (int)instruction.opcode.value;
          body.captureAssign((JAssignStmt)assign, op);
        }
    }

    @Override
    boolean overridesRegister(int register) {
        TwoRegisterInstruction i = (TwoRegisterInstruction) instruction;
        int dest = i.getRegisterA();
        return register == dest;
    }

    @Override
    public Set<DexType> introducedTypes() {
        InstructionWithReference i = (InstructionWithReference) instruction;

        Set<DexType> types = new HashSet<DexType>();
        types.add(new DexType((TypeIdItem) i.getReferencedItem()));
        return types;
    }
}

@@ -1236,24 +1236,43 @@
 t.RefType;%0D%0A
+import soot.Type;%0D%0A
 import soot.
@@ -1870,43 +1870,41 @@
     
-String className = dottedClassName(
+%0D%0A        Type t = DexType.toSoot
 ((Ty
@@ -1940,30 +1940,11 @@
 ()))
-.getTypeDescriptor());
+;%0D%0A
 %0D%0A  
@@ -2032,28 +2032,9 @@
 e), 
-RefType.v(className)
+t
 );%0D%0A

Correct type is now given to newInstanceOfExpr. Exemple of bug: type [B was converted to byte[] and byte[] was written in the bytecode instead of [B.