Search in sources :

Example 6 with DavaBody

use of soot.dava.DavaBody in project soot by Sable.

the class SootMethod method getDavaDeclaration.

/*
	 * TODO: Nomair A. Naeem .... 8th Feb 2006 This is really messy coding So
	 * much for modularization!! Should some day look into creating the
	 * DavaDeclaration from within DavaBody
	 */
public String getDavaDeclaration() {
    if (getName().equals(staticInitializerName))
        return "static";
    StringBuffer buffer = new StringBuffer();
    // modifiers
    StringTokenizer st = new StringTokenizer(Modifier.toString(this.getModifiers()));
    if (st.hasMoreTokens())
        buffer.append(st.nextToken());
    while (st.hasMoreTokens()) buffer.append(" " + st.nextToken());
    if (buffer.length() != 0)
        buffer.append(" ");
    if (getName().equals(constructorName))
        buffer.append(getDeclaringClass().getShortJavaStyleName());
    else {
        Type t = this.getReturnType();
        String tempString = t.toString();
        /*
			 * Added code to handle RuntimeExcepotion thrown by getActiveBody
			 */
        if (hasActiveBody()) {
            DavaBody body = (DavaBody) getActiveBody();
            IterableSet<String> importSet = body.getImportList();
            if (!importSet.contains(tempString)) {
                body.addToImportList(tempString);
            }
            tempString = RemoveFullyQualifiedName.getReducedName(importSet, tempString, t);
        }
        buffer.append(tempString + " ");
        buffer.append(Scene.v().quotedNameOf(this.getName()));
    }
    buffer.append("(");
    // parameters
    Iterator<Type> typeIt = this.getParameterTypes().iterator();
    int count = 0;
    while (typeIt.hasNext()) {
        Type t = typeIt.next();
        String tempString = t.toString();
        /*
			 * Nomair A. Naeem 7th Feb 2006 It is nice to remove the fully
			 * qualified type names of parameters if the package they belong to
			 * have been imported javax.swing.ImageIcon should be just ImageIcon
			 * if javax.swing is imported If not imported WHY NOT..import it!!
			 */
        if (hasActiveBody()) {
            DavaBody body = (DavaBody) getActiveBody();
            IterableSet<String> importSet = body.getImportList();
            if (!importSet.contains(tempString)) {
                body.addToImportList(tempString);
            }
            tempString = RemoveFullyQualifiedName.getReducedName(importSet, tempString, t);
        }
        buffer.append(tempString + " ");
        buffer.append(" ");
        if (hasActiveBody()) {
            buffer.append(((DavaBody) getActiveBody()).get_ParamMap().get(new Integer(count++)));
        } else {
            if (t == BooleanType.v())
                buffer.append("z" + count++);
            else if (t == ByteType.v())
                buffer.append("b" + count++);
            else if (t == ShortType.v())
                buffer.append("s" + count++);
            else if (t == CharType.v())
                buffer.append("c" + count++);
            else if (t == IntType.v())
                buffer.append("i" + count++);
            else if (t == LongType.v())
                buffer.append("l" + count++);
            else if (t == DoubleType.v())
                buffer.append("d" + count++);
            else if (t == FloatType.v())
                buffer.append("f" + count++);
            else if (t == StmtAddressType.v())
                buffer.append("a" + count++);
            else if (t == ErroneousType.v())
                buffer.append("e" + count++);
            else if (t == NullType.v())
                buffer.append("n" + count++);
            else
                buffer.append("r" + count++);
        }
        if (typeIt.hasNext())
            buffer.append(", ");
    }
    buffer.append(")");
    // Print exceptions
    if (exceptions != null) {
        Iterator<SootClass> exceptionIt = this.getExceptions().iterator();
        if (exceptionIt.hasNext()) {
            buffer.append(" throws " + exceptionIt.next().getName());
            while (exceptionIt.hasNext()) {
                buffer.append(", " + exceptionIt.next().getName());
            }
        }
    }
    return buffer.toString().intern();
}
Also used : StringTokenizer(java.util.StringTokenizer) DavaBody(soot.dava.DavaBody) NumberedString(soot.util.NumberedString)

Example 7 with DavaBody

use of soot.dava.DavaBody in project soot by Sable.

the class LocalVariableCleaner method outASTMethodNode.

/*
	 * Get all locals declared in the method
	 * If the local is never defined (and hence never used) remove it
	 * If the local is defined BUT never used then you may remove it IF AND ONLY IF
	 *    The definition is either a copy stmt or an assignment of a constant (i.e. no side effects)
	 */
public void outASTMethodNode(ASTMethodNode node) {
    boolean redo = false;
    // create the uD and dU chains
    useDefs = new ASTUsesAndDefs(AST);
    AST.apply(useDefs);
    // get all local variables declared in this method
    Iterator decIt = node.getDeclaredLocals().iterator();
    ArrayList<Local> removeList = new ArrayList<Local>();
    while (decIt.hasNext()) {
        // going through each local declared
        Local var = (Local) decIt.next();
        List<DefinitionStmt> defs = getDefs(var);
        // if defs is 0 it means var never got defined
        if (defs.size() == 0) {
            // var is never defined and hence is certainly not used anywhere
            removeList.add(var);
        } else {
            // if a var is defined but not used then in some conditions we can remove it
            // check that each def is removable
            Iterator<DefinitionStmt> defIt = defs.iterator();
            while (defIt.hasNext()) {
                DefinitionStmt ds = defIt.next();
                if (canRemoveDef(ds)) {
                    // if removeStmt is successful since something change we need to redo
                    // everything hoping something else might be removed....
                    // in this case method returns true
                    redo = removeStmt(ds);
                }
            }
        // while going through defs
        }
    // end else defs was not zero
    }
    // going through each stmt
    // go through the removeList and remove all locals
    Iterator<Local> remIt = removeList.iterator();
    while (remIt.hasNext()) {
        Local removeLocal = remIt.next();
        node.removeDeclaredLocal(removeLocal);
        // retrieve DavaBody
        if (AST instanceof ASTMethodNode) {
            // this should always be true but whatever
            DavaBody body = ((ASTMethodNode) AST).getDavaBody();
            if (DEBUG) {
                System.out.println("body information");
                System.out.println("Control local is: " + body.get_ControlLocal());
                System.out.println("his locals are: " + body.get_ThisLocals());
                System.out.println("Param Map is: " + body.get_ParamMap());
                System.out.println("Locals are:" + body.getLocals());
            }
            Collection<Local> localChain = body.getLocals();
            if (removeLocal != null && localChain != null)
                localChain.remove(removeLocal);
        } else
            throw new DecompilationException("found AST which is not a methodNode");
        if (DEBUG)
            System.out.println("Removed" + removeLocal);
        redo = true;
    }
    if (redo) {
        // redo the whole function
        outASTMethodNode(node);
    }
}
Also used : DecompilationException(soot.dava.DecompilationException) DavaBody(soot.dava.DavaBody)

Aggregations

DavaBody (soot.dava.DavaBody)7 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 SootMethod (soot.SootMethod)3 ASTNode (soot.dava.internal.AST.ASTNode)3 SootClass (soot.SootClass)2 ASTMethodNode (soot.dava.internal.AST.ASTMethodNode)2 Chain (soot.util.Chain)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 Body (soot.Body)1 PrimType (soot.PrimType)1 SootField (soot.SootField)1 Value (soot.Value)1 BafBody (soot.baf.BafBody)1 DecompilationException (soot.dava.DecompilationException)1 CPApplication (soot.dava.toolkits.base.AST.transformations.CPApplication)1 EliminateConditions (soot.dava.toolkits.base.AST.transformations.EliminateConditions)1