use of soot.SootMethod in project soot by Sable.
the class ClassResolver method createMethodDecl.
/**
* Method Declaration Creation
*/
private void createMethodDecl(polyglot.ast.MethodDecl method) {
String name = createName(method);
// parameters
ArrayList parameters = createParameters(method);
// exceptions
ArrayList<SootClass> exceptions = createExceptions(method);
soot.SootMethod sootMethod = createSootMethod(name, method.flags(), method.returnType().type(), parameters, exceptions);
finishProcedure(method, sootMethod);
}
use of soot.SootMethod in project soot by Sable.
the class ClassResolver method handleAssert.
/**
* Handling for assert stmts - extra fields and methods are needed in the
* Jimple
*/
private void handleAssert(polyglot.ast.ClassBody cBody) {
// find any asserts in class body but not in inner class bodies
AssertStmtChecker asc = new AssertStmtChecker();
cBody.visit(asc);
if (!asc.isHasAssert())
return;
// two extra fields
// $assertionsDisabled field is added to the actual class where the
// assert is found (even if its an inner class - interfaces cannot
// have asserts stmts directly contained within them)
String fieldName = "$assertionsDisabled";
soot.Type fieldType = soot.BooleanType.v();
if (!sootClass.declaresField(fieldName, fieldType)) {
soot.SootField assertionsDisabledField = Scene.v().makeSootField(fieldName, fieldType, soot.Modifier.STATIC | soot.Modifier.FINAL);
sootClass.addField(assertionsDisabledField);
assertionsDisabledField.addTag(new soot.tagkit.SyntheticTag());
}
// class$ field is added to the outer most class if sootClass
// containing the assert is inner - if the outer most class is
// an interface - add instead to special interface anon class
soot.SootClass addToClass = sootClass;
while ((InitialResolver.v().getInnerClassInfoMap() != null) && (InitialResolver.v().getInnerClassInfoMap().containsKey(addToClass))) {
addToClass = InitialResolver.v().getInnerClassInfoMap().get(addToClass).getOuterClass();
}
// this field is named after the outer class even if the outer
// class is an interface and will be actually added to the
// special interface anon class
fieldName = "class$" + addToClass.getName().replaceAll(".", "$");
if ((InitialResolver.v().getInterfacesList() != null) && (InitialResolver.v().getInterfacesList().contains(addToClass.getName()))) {
addToClass = getSpecialInterfaceAnonClass(addToClass);
}
fieldType = soot.RefType.v("java.lang.Class");
if (!addToClass.declaresField(fieldName, fieldType)) {
soot.SootField classField = Scene.v().makeSootField(fieldName, fieldType, soot.Modifier.STATIC);
addToClass.addField(classField);
classField.addTag(new soot.tagkit.SyntheticTag());
}
// two extra methods
// class$ method is added to the outer most class if sootClass
// containing the assert is inner - if the outer most class is
// an interface - add instead to special interface anon class
String methodName = "class$";
soot.Type methodRetType = soot.RefType.v("java.lang.Class");
ArrayList paramTypes = new ArrayList();
paramTypes.add(soot.RefType.v("java.lang.String"));
// make meth
soot.SootMethod sootMethod = Scene.v().makeSootMethod(methodName, paramTypes, methodRetType, soot.Modifier.STATIC);
AssertClassMethodSource assertMSrc = new AssertClassMethodSource();
sootMethod.setSource(assertMSrc);
if (!addToClass.declaresMethod(methodName, paramTypes, methodRetType)) {
addToClass.addMethod(sootMethod);
sootMethod.addTag(new soot.tagkit.SyntheticTag());
}
// clinit method is added to actual class where assert is found
// if the class already has a clinit method its method source is
// informed of an assert
methodName = "<clinit>";
methodRetType = soot.VoidType.v();
paramTypes = new ArrayList();
// make meth
sootMethod = Scene.v().makeSootMethod(methodName, paramTypes, methodRetType, soot.Modifier.STATIC);
PolyglotMethodSource mSrc = new PolyglotMethodSource();
mSrc.setJBB(InitialResolver.v().getJBBFactory().createJimpleBodyBuilder());
mSrc.hasAssert(true);
sootMethod.setSource(mSrc);
if (!sootClass.declaresMethod(methodName, paramTypes, methodRetType)) {
sootClass.addMethod(sootMethod);
} else {
((soot.javaToJimple.PolyglotMethodSource) sootClass.getMethod(methodName, paramTypes, methodRetType).getSource()).hasAssert(true);
}
}
use of soot.SootMethod in project soot by Sable.
the class ConstantValueToInitializerTransformer method getOrCreateInitializer.
private SootMethod getOrCreateInitializer(SootClass sc, Set<SootField> alreadyInitialized) {
SootMethod smInit;
// Create a static initializer if we don't already have one
smInit = sc.getMethodByNameUnsafe("<clinit>");
if (smInit == null) {
smInit = Scene.v().makeSootMethod("<clinit>", Collections.<Type>emptyList(), VoidType.v());
smInit.setActiveBody(Jimple.v().newBody(smInit));
sc.addMethod(smInit);
smInit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
} else if (smInit.isPhantom())
return null;
else {
smInit.retrieveActiveBody();
// somewhere
for (Unit u : smInit.getActiveBody().getUnits()) {
Stmt s = (Stmt) u;
for (ValueBox vb : s.getDefBoxes()) if (vb.getValue() instanceof FieldRef)
alreadyInitialized.add(((FieldRef) vb.getValue()).getField());
}
}
return smInit;
}
use of soot.SootMethod in project soot by Sable.
the class ConstantValueToInitializerTransformer method transformClass.
public void transformClass(SootClass sc) {
SootMethod smInit = null;
Set<SootField> alreadyInitialized = new HashSet<SootField>();
for (SootField sf : sc.getFields()) {
// different constructors might assign different values.
if (!sf.isStatic() || !sf.isFinal())
continue;
// generate a second one
if (alreadyInitialized.contains(sf))
continue;
// Look for constant values
for (Tag t : sf.getTags()) {
Stmt initStmt = null;
if (t instanceof DoubleConstantValueTag) {
double value = ((DoubleConstantValueTag) t).getDoubleValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), DoubleConstant.v(value));
} else if (t instanceof FloatConstantValueTag) {
float value = ((FloatConstantValueTag) t).getFloatValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), FloatConstant.v(value));
} else if (t instanceof IntegerConstantValueTag) {
int value = ((IntegerConstantValueTag) t).getIntValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), IntConstant.v(value));
} else if (t instanceof LongConstantValueTag) {
long value = ((LongConstantValueTag) t).getLongValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), LongConstant.v(value));
} else if (t instanceof StringConstantValueTag) {
String value = ((StringConstantValueTag) t).getStringValue();
initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), StringConstant.v(value));
}
if (initStmt != null) {
if (smInit == null)
smInit = getOrCreateInitializer(sc, alreadyInitialized);
if (smInit != null)
smInit.getActiveBody().getUnits().addFirst(initStmt);
}
}
}
if (smInit != null) {
Chain<Unit> units = smInit.getActiveBody().getUnits();
if (units.isEmpty() || !(units.getLast() instanceof ReturnVoidStmt))
units.add(Jimple.v().newReturnVoidStmt());
}
}
use of soot.SootMethod in project soot by Sable.
the class PhaseDumper method dumpAllBodies.
private void dumpAllBodies(String baseName, boolean deleteGraphFiles) {
List<SootClass> classes = Scene.v().getClasses(SootClass.BODIES);
for (SootClass cls : classes) {
for (Iterator m = cls.getMethods().iterator(); m.hasNext(); ) {
SootMethod method = (SootMethod) m.next();
if (method.hasActiveBody()) {
Body body = method.getActiveBody();
if (deleteGraphFiles) {
deleteOldGraphFiles(body, baseName);
}
dumpBody(body, baseName);
}
}
}
}
Aggregations