use of soot.dava.internal.javaRep.DVirtualInvokeExpr 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.");
}
}
use of soot.dava.internal.javaRep.DVirtualInvokeExpr in project soot by Sable.
the class DavaBody method javafy_invoke_expr.
private void javafy_invoke_expr(ValueBox vb) {
InvokeExpr ie = (InvokeExpr) vb.getValue();
String className = ie.getMethodRef().declaringClass().toString();
String packageName = ie.getMethodRef().declaringClass().getJavaPackageName();
String classPackageName = packageName;
if (className.lastIndexOf('.') > 0) {
// 0 doesnt make sense
classPackageName = className.substring(0, className.lastIndexOf('.'));
}
if (!packageName.equals(classPackageName))
throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
addToImportList(className);
for (int i = 0; i < ie.getArgCount(); i++) {
Value arg = ie.getArg(i);
if (arg instanceof IntConstant)
ie.getArgBox(i).setValue(DIntConstant.v(((IntConstant) arg).value, ie.getMethodRef().parameterType(i)));
else
javafy(ie.getArgBox(i));
}
if (ie instanceof InstanceInvokeExpr) {
javafy(((InstanceInvokeExpr) ie).getBaseBox());
if (ie instanceof VirtualInvokeExpr) {
VirtualInvokeExpr vie = (VirtualInvokeExpr) ie;
vb.setValue(new DVirtualInvokeExpr(vie.getBase(), vie.getMethodRef(), vie.getArgs(), thisLocals));
} else if (ie instanceof SpecialInvokeExpr) {
SpecialInvokeExpr sie = (SpecialInvokeExpr) ie;
vb.setValue(new DSpecialInvokeExpr(sie.getBase(), sie.getMethodRef(), sie.getArgs()));
} else if (ie instanceof InterfaceInvokeExpr) {
InterfaceInvokeExpr iie = (InterfaceInvokeExpr) ie;
vb.setValue(new DInterfaceInvokeExpr(iie.getBase(), iie.getMethodRef(), iie.getArgs()));
} else
throw new RuntimeException("InstanceInvokeExpr " + ie + " not javafied correctly");
} else if (ie instanceof StaticInvokeExpr) {
StaticInvokeExpr sie = (StaticInvokeExpr) ie;
if (sie instanceof NewInvokeExpr) {
NewInvokeExpr nie = (NewInvokeExpr) sie;
RefType rt = nie.getBaseType();
className = rt.getSootClass().toString();
packageName = rt.getSootClass().getJavaPackageName();
classPackageName = packageName;
if (className.lastIndexOf('.') > 0) {
// 0 doesnt make sense
classPackageName = className.substring(0, className.lastIndexOf('.'));
}
if (!packageName.equals(classPackageName))
throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
addToImportList(className);
vb.setValue(new DNewInvokeExpr((RefType) nie.getType(), nie.getMethodRef(), nie.getArgs()));
} else {
SootMethodRef methodRef = sie.getMethodRef();
className = methodRef.declaringClass().toString();
packageName = methodRef.declaringClass().getJavaPackageName();
classPackageName = packageName;
if (className.lastIndexOf('.') > 0) {
// 0 doesnt make sense
classPackageName = className.substring(0, className.lastIndexOf('.'));
}
if (!packageName.equals(classPackageName))
throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
addToImportList(className);
// addPackage(methodRef.declaringClass().getJavaPackageName());
vb.setValue(new DStaticInvokeExpr(methodRef, sie.getArgs()));
}
} else
throw new RuntimeException("InvokeExpr " + ie + " not javafied correctly");
}
use of soot.dava.internal.javaRep.DVirtualInvokeExpr in project soot by Sable.
the class NewStringBufferSimplification method inExprOrRefValueBox.
public void inExprOrRefValueBox(ValueBox argBox) {
if (DEBUG)
System.out.println("ValBox is: " + argBox.toString());
Value tempArgValue = argBox.getValue();
if (DEBUG)
System.out.println("arg value is: " + tempArgValue);
if (!(tempArgValue instanceof DVirtualInvokeExpr)) {
if (DEBUG)
System.out.println("Not a DVirtualInvokeExpr" + tempArgValue.getClass());
return;
}
// check this is a toString for StringBuffer
if (DEBUG)
System.out.println("arg value is a virtual invokeExpr");
DVirtualInvokeExpr vInvokeExpr = ((DVirtualInvokeExpr) tempArgValue);
// need this try catch since DavaStmtHandler expr will not have a "getMethod"
try {
if (!(vInvokeExpr.getMethod().toString().equals("<java.lang.StringBuffer: java.lang.String toString()>")))
return;
} catch (Exception e) {
return;
}
if (DEBUG)
System.out.println("Ends in toString()");
Value base = vInvokeExpr.getBase();
List args = new ArrayList();
while (base instanceof DVirtualInvokeExpr) {
DVirtualInvokeExpr tempV = (DVirtualInvokeExpr) base;
if (DEBUG)
System.out.println("base method is " + tempV.getMethod());
if (!tempV.getMethod().toString().startsWith("<java.lang.StringBuffer: java.lang.StringBuffer append")) {
if (DEBUG)
System.out.println("Found a virtual invoke which is not a append" + tempV.getMethod());
return;
}
args.add(0, tempV.getArg(0));
// System.out.println("Append: "+((DVirtualInvokeExpr)base).getArg(0) );
// move to next base
base = ((DVirtualInvokeExpr) base).getBase();
}
if (!(base instanceof DNewInvokeExpr))
return;
if (DEBUG)
System.out.println("New expr is " + ((DNewInvokeExpr) base).getMethod());
if (!((DNewInvokeExpr) base).getMethod().toString().equals("<java.lang.StringBuffer: void <init>()>"))
return;
/*
* The arg is a new invoke expr of StringBuffer and all the appends are present in the args list
*/
if (DEBUG)
System.out.println("Found a new StringBuffer.append list in it");
// argBox contains the new StringBuffer
Iterator it = args.iterator();
Value newVal = null;
while (it.hasNext()) {
Value temp = (Value) it.next();
if (newVal == null)
newVal = temp;
else {
// create newVal + temp
newVal = new GAddExpr(newVal, temp);
}
}
if (DEBUG)
System.out.println("New expression for System.out.println is" + newVal);
argBox.setValue(newVal);
}
use of soot.dava.internal.javaRep.DVirtualInvokeExpr in project soot by Sable.
the class SuperFirstStmtHandler method createNewASTConstructor.
public void createNewASTConstructor(ASTStatementSequenceNode initNode) {
List<Object> newConstructorBody = new ArrayList<Object>();
List<AugmentedStmt> newStmts = new ArrayList<AugmentedStmt>();
/*
* add any definitions to live variables that might be in body X
*/
// we have gotten argsTwoType size() out of the handler so thats the
// index count
// mustInitialize has the live variables that need to be initialized
// create new ReftType for DavaSuperHandler
RefType type = (new SootClass("DavaSuperHandler")).getType();
// make JimpleLocal to be used in each arg
// takes care of
Local jimpleLocal = new JimpleLocal("handler", type);
// handler
// make reference to a method of name get takes one int arg belongs to
// DavaSuperHandler
ArrayList tempList = new ArrayList();
tempList.add(IntType.v());
SootMethodRef getMethodRef = makeMethodRef("get", tempList);
// Iterator typeIt = argsTwoTypes.iterator();
if (mustInitialize != null) {
Iterator<Local> initIt = mustInitialize.iterator();
while (initIt.hasNext()) {
Local initLocal = initIt.next();
Type tempType = initLocal.getType();
// takes
DIntConstant arg = DIntConstant.v(mustInitializeIndex, IntType.v());
// care
// of
// the
// index
mustInitializeIndex++;
ArrayList tempArgList = new ArrayList();
tempArgList.add(arg);
DVirtualInvokeExpr tempInvokeExpr = new DVirtualInvokeExpr(jimpleLocal, getMethodRef, tempArgList, new HashSet<Object>());
// NECESASARY CASTING OR RETRIEVAL OF PRIM TYPES TO BE DONE HERE
Value toAddExpr = getProperCasting(tempType, tempInvokeExpr);
if (toAddExpr == null)
throw new DecompilationException("UNABLE TO CREATE TOADDEXPR:" + tempType);
// need to create a def stmt with the local on the left and
// toAddExpr on the right
GAssignStmt assign = new GAssignStmt(initLocal, toAddExpr);
newStmts.add(new AugmentedStmt(assign));
}
}
// add any statements following the this.<init> statement
Iterator<AugmentedStmt> it = initNode.getStatements().iterator();
while (it.hasNext()) {
AugmentedStmt augStmt = (AugmentedStmt) it.next();
Stmt stmtTemp = augStmt.get_Stmt();
if (stmtTemp == originalConstructorUnit) {
break;
}
}
while (it.hasNext()) {
/*
* notice we dont need to clone these because these will be removed
* from the other method from which we are copying these
*/
newStmts.add(it.next());
}
if (newStmts.size() > 0) {
newConstructorBody.add(new ASTStatementSequenceNode(newStmts));
}
// adding body Y now
List<Object> originalASTMethodSubBodies = originalASTMethod.get_SubBodies();
if (originalASTMethodSubBodies.size() != 1)
throw new CorruptASTException("size of ASTMethodNode subBody not 1");
List<Object> oldASTBody = (List<Object>) originalASTMethodSubBodies.get(0);
Iterator<Object> itOld = oldASTBody.iterator();
boolean sanity = false;
while (itOld.hasNext()) {
// going through originalASTMethodNode's ASTNodes
ASTNode tempNode = (ASTNode) itOld.next();
// enter only if its not the initNode
if (tempNode instanceof ASTStatementSequenceNode) {
if ((((ASTStatementSequenceNode) tempNode).getStatements()).equals(initNode.getStatements())) {
sanity = true;
break;
}
}
}
if (!sanity) {
// means we never found the initNode which shouldnt happen
throw new DecompilationException("never found the init node");
}
// Y are all the nodes following the initNode
while (itOld.hasNext()) {
newConstructorBody.add(itOld.next());
}
// setDeclarations in newNode
// The LocalVariableCleaner which is called in the end of DavaBody will
// clear up any declarations that are not required
List<AugmentedStmt> newConstructorDeclarations = new ArrayList<AugmentedStmt>();
for (AugmentedStmt as : originalASTMethod.getDeclarations().getStatements()) {
DVariableDeclarationStmt varDecStmt = (DVariableDeclarationStmt) as.get_Stmt();
newConstructorDeclarations.add(new AugmentedStmt((DVariableDeclarationStmt) varDecStmt.clone()));
}
ASTStatementSequenceNode newDecs = new ASTStatementSequenceNode(new ArrayList<AugmentedStmt>());
if (newConstructorDeclarations.size() > 0) {
newDecs = new ASTStatementSequenceNode(newConstructorDeclarations);
// DONT FORGET TO SET THE DECLARATIONS IN THE METHOD ONCE IT IS
// CREATED
// newASTConstructorMethod.setDeclarations(newDecs);
// declarations are always the first element
newConstructorBody.add(0, newDecs);
}
// so we have any declarations followed by body Y
// have to put the newConstructorBody into an list of subBodies which
// goes into the newASTConstructorMethod
newASTConstructorMethod = new ASTMethodNode(newConstructorBody);
// dont forget to set the declarations
newASTConstructorMethod.setDeclarations(newDecs);
}
use of soot.dava.internal.javaRep.DVirtualInvokeExpr in project soot by Sable.
the class SuperFirstStmtHandler method createAugmentedStmtToAdd.
private AugmentedStmt createAugmentedStmtToAdd(Local newLocal, SootMethodRef getMethodRef, Value tempVal) {
ArrayList tempArgList = new ArrayList();
tempArgList.add(tempVal);
DVirtualInvokeExpr tempInvokeExpr = new DVirtualInvokeExpr(newLocal, getMethodRef, tempArgList, new HashSet<Object>());
// create Invoke Stmt with virtualInvoke as the expression
GInvokeStmt s = new GInvokeStmt(tempInvokeExpr);
return new AugmentedStmt(s);
}
Aggregations