Search in sources :

Example 1 with GCastExpr

use of soot.grimp.internal.GCastExpr in project soot by Sable.

the class SuperFirstStmtHandler method getProperCasting.

public Value getProperCasting(Type tempType, DVirtualInvokeExpr tempInvokeExpr) {
    if (tempType instanceof RefType) {
        // System.out.println("This is a reftype:"+tempType);
        return new GCastExpr(tempInvokeExpr, tempType);
    } else if (tempType instanceof PrimType) {
        PrimType t = (PrimType) tempType;
        if (t == BooleanType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Boolean"));
            // booleanValue
            SootMethod tempMethod = Scene.v().makeSootMethod("booleanValue", new ArrayList(), BooleanType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Boolean"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == ByteType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Byte"));
            // byteValue
            SootMethod tempMethod = Scene.v().makeSootMethod("byteValue", new ArrayList(), ByteType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Byte"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == CharType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Character"));
            // charValue
            SootMethod tempMethod = Scene.v().makeSootMethod("charValue", new ArrayList(), CharType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Character"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == DoubleType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Double"));
            // doubleValue
            SootMethod tempMethod = Scene.v().makeSootMethod("doubleValue", new ArrayList(), DoubleType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Double"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == FloatType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Float"));
            // floatValue
            SootMethod tempMethod = Scene.v().makeSootMethod("floatValue", new ArrayList(), FloatType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Float"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == IntType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Integer"));
            // intValue
            SootMethod tempMethod = Scene.v().makeSootMethod("intValue", new ArrayList(), IntType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Integer"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == LongType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Long"));
            // longValue
            SootMethod tempMethod = Scene.v().makeSootMethod("longValue", new ArrayList(), LongType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Long"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == ShortType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Short"));
            // shortValue
            SootMethod tempMethod = Scene.v().makeSootMethod("shortValue", new ArrayList(), ShortType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Short"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else {
            throw new DecompilationException("Unhandle primType:" + tempType);
        }
    } else {
        throw new DecompilationException("The type:" + tempType + " was not a reftye or primtype. PLEASE REPORT.");
    }
}
Also used : SootMethodRef(soot.SootMethodRef) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) SootClass(soot.SootClass) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) Value(soot.Value) PrimType(soot.PrimType) SootMethod(soot.SootMethod) GCastExpr(soot.grimp.internal.GCastExpr) HashSet(java.util.HashSet)

Example 2 with GCastExpr

use of soot.grimp.internal.GCastExpr in project soot by Sable.

the class TypeCastingError method inASTStatementSequenceNode.

public void inASTStatementSequenceNode(ASTStatementSequenceNode node) {
    for (AugmentedStmt as : node.getStatements()) {
        Stmt s = as.get_Stmt();
        if (!(s instanceof DefinitionStmt))
            continue;
        DefinitionStmt ds = (DefinitionStmt) s;
        if (myDebug)
            System.out.println("Definition stmt" + ds);
        ValueBox rightBox = ds.getRightOpBox();
        ValueBox leftBox = ds.getLeftOpBox();
        Value right = rightBox.getValue();
        Value left = leftBox.getValue();
        if (!(left.getType() instanceof PrimType && right.getType() instanceof PrimType)) {
            // only interested in prim type casting errors
            if (myDebug)
                System.out.println("\tDefinition stmt does not contain prims no need to modify");
            continue;
        }
        Type leftType = left.getType();
        Type rightType = right.getType();
        if (myDebug)
            System.out.println("Left type is: " + leftType);
        if (myDebug)
            System.out.println("Right type is: " + rightType);
        if (leftType.equals(rightType)) {
            if (myDebug)
                System.out.println("\tTypes are the same");
            if (myDebug)
                System.out.println("Right value is of instance" + right.getClass());
        }
        if (!leftType.equals(rightType)) {
            if (myDebug)
                System.out.println("\tDefinition stmt has to be modified");
            /*
				 * byte  	 Byte-length integer  	8-bit two's complement
				 * short 	Short integer 	16-bit two's complement
				 * int 	Integer 	32-bit two's complement
				 * long 	Long integer 	64-bit two's complement
				 * float 	Single-precision floating point 	32-bit IEEE 754
				 * double Double-precision floating point  	64-bit IEEE 754 	
				 */
            if (leftType instanceof ByteType && (rightType instanceof DoubleType || rightType instanceof FloatType || rightType instanceof IntType || rightType instanceof LongType || rightType instanceof ShortType)) {
                if (DEBUG)
                    System.out.println("Explicit casting to BYTE required");
                rightBox.setValue(new GCastExpr(right, ByteType.v()));
                if (DEBUG)
                    System.out.println("New right expr is " + rightBox.getValue().toString());
                continue;
            }
            if (leftType instanceof ShortType && (rightType instanceof DoubleType || rightType instanceof FloatType || rightType instanceof IntType || rightType instanceof LongType)) {
                if (DEBUG)
                    System.out.println("Explicit casting to SHORT required");
                rightBox.setValue(new GCastExpr(right, ShortType.v()));
                if (DEBUG)
                    System.out.println("New right expr is " + rightBox.getValue().toString());
                continue;
            }
            if (leftType instanceof IntType && (rightType instanceof DoubleType || rightType instanceof FloatType || rightType instanceof LongType)) {
                if (myDebug)
                    System.out.println("Explicit casting to INT required");
                rightBox.setValue(new GCastExpr(right, IntType.v()));
                if (myDebug)
                    System.out.println("New right expr is " + rightBox.getValue().toString());
                continue;
            }
            if (leftType instanceof LongType && (rightType instanceof DoubleType || rightType instanceof FloatType)) {
                if (DEBUG)
                    System.out.println("Explicit casting to LONG required");
                rightBox.setValue(new GCastExpr(right, LongType.v()));
                if (DEBUG)
                    System.out.println("New right expr is " + rightBox.getValue().toString());
                continue;
            }
            if (leftType instanceof FloatType && rightType instanceof DoubleType) {
                if (DEBUG)
                    System.out.println("Explicit casting to FLOAT required");
                rightBox.setValue(new GCastExpr(right, FloatType.v()));
                if (DEBUG)
                    System.out.println("New right expr is " + rightBox.getValue().toString());
                continue;
            }
        }
    }
}
Also used : LongType(soot.LongType) ShortType(soot.ShortType) ByteType(soot.ByteType) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) Stmt(soot.jimple.Stmt) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt) DefinitionStmt(soot.jimple.DefinitionStmt) FloatType(soot.FloatType) IntType(soot.IntType) ByteType(soot.ByteType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) Type(soot.Type) PrimType(soot.PrimType) ShortType(soot.ShortType) LongType(soot.LongType) ValueBox(soot.ValueBox) DoubleType(soot.DoubleType) Value(soot.Value) PrimType(soot.PrimType) GCastExpr(soot.grimp.internal.GCastExpr) DefinitionStmt(soot.jimple.DefinitionStmt)

Aggregations

PrimType (soot.PrimType)2 Value (soot.Value)2 GCastExpr (soot.grimp.internal.GCastExpr)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ByteType (soot.ByteType)1 DoubleType (soot.DoubleType)1 FloatType (soot.FloatType)1 IntType (soot.IntType)1 LongType (soot.LongType)1 RefType (soot.RefType)1 ShortType (soot.ShortType)1 SootClass (soot.SootClass)1 SootMethod (soot.SootMethod)1 SootMethodRef (soot.SootMethodRef)1 Type (soot.Type)1 ValueBox (soot.ValueBox)1 DecompilationException (soot.dava.DecompilationException)1 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)1 DVirtualInvokeExpr (soot.dava.internal.javaRep.DVirtualInvokeExpr)1