Search in sources :

Example 6 with IterableSet

use of soot.util.IterableSet in project soot by Sable.

the class DavaPrinter method printTo.

public void printTo(SootClass cl, PrintWriter out) {
    // IterableSet packagesUsed = new IterableSet();
    IterableSet importList = new IterableSet();
    {
        String curPackage = cl.getJavaPackageName();
        if (!curPackage.equals("")) {
            out.println("package " + curPackage + ";");
            out.println();
        }
        if (cl.hasSuperclass()) {
            SootClass superClass = cl.getSuperclass();
            importList.add(superClass.toString());
        // packagesUsed.add(superClass.getJavaPackageName());
        }
        Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
        while (interfaceIt.hasNext()) {
            String interfacePackage = ((SootClass) interfaceIt.next()).toString();
            if (!importList.contains(interfacePackage))
                importList.add(interfacePackage);
        // if (!packagesUsed.contains(interfacePackage))
        // packagesUsed.add(interfacePackage);
        }
        Iterator<SootMethod> methodIt = cl.methodIterator();
        while (methodIt.hasNext()) {
            SootMethod dm = (SootMethod) methodIt.next();
            if (dm.hasActiveBody()) {
                // packagesUsed = packagesUsed.union(((DavaBody) dm.getActiveBody()).get_PackagesUsed());
                importList = importList.union(((DavaBody) dm.getActiveBody()).getImportList());
            }
            Iterator<SootClass> eit = dm.getExceptions().iterator();
            while (eit.hasNext()) {
                String thrownPackage = eit.next().toString();
                if (!importList.contains(thrownPackage))
                    importList.add(thrownPackage);
            // if (!packagesUsed.contains(thrownPackage))
            // packagesUsed.add(thrownPackage);
            }
            Iterator<Type> pit = dm.getParameterTypes().iterator();
            while (pit.hasNext()) {
                Type t = (Type) pit.next();
                if (t instanceof RefType) {
                    String paramPackage = ((RefType) t).getSootClass().toString();
                    if (!importList.contains(paramPackage))
                        importList.add(paramPackage);
                // if (packagesUsed.contains(paramPackage) == false)
                // packagesUsed.add(paramPackage);
                }
            }
            Type t = dm.getReturnType();
            if (t instanceof RefType) {
                String returnPackage = ((RefType) t).getSootClass().toString();
                if (!importList.contains(returnPackage))
                    importList.add(returnPackage);
            // if (packagesUsed.contains(returnPackage) == false)
            // packagesUsed.add(returnPackage);
            }
        }
        Iterator<SootField> fieldIt = cl.getFields().iterator();
        while (fieldIt.hasNext()) {
            SootField f = (SootField) fieldIt.next();
            if (f.isPhantom())
                continue;
            Type t = f.getType();
            if (t instanceof RefType) {
                String fieldPackage = ((RefType) t).getSootClass().toString();
                if (!importList.contains(fieldPackage))
                    importList.add(fieldPackage);
            }
        }
        Iterator<String> pit = importList.iterator();
        List<String> toImport = new ArrayList<String>();
        while (pit.hasNext()) {
            /*
            	 * dont import any file which has currentPackage.className
            	 * dont import any file which starts with java.lang
            	 */
            String temp = (String) pit.next();
            // System.out.println("temp is "+temp);
            if (temp.indexOf("java.lang") > -1) {
                // problem is that we need to import sub packages java.lang.ref
                // for instance if the type is java.lang.ref.WeakReference
                String tempClassName = RemoveFullyQualifiedName.getClassName(temp);
                if (temp.equals("java.lang." + tempClassName)) {
                    // System.out.println("temp was not printed as it belongs to java.lang");
                    continue;
                }
            }
            if (curPackage.length() > 0 && temp.indexOf(curPackage) > -1) {
                // System.out.println("here  "+temp);
                continue;
            }
            if (cl.toString().equals(temp))
                continue;
            // System.out.println("printing"+);
            toImport.add(temp);
        }
        /*
             * Check that we are not importing two classes with the same last name
             * If yes then remove explicit import and import the whole package
             * else output explicit import statement
             */
        Iterator it = toImport.iterator();
        while (it.hasNext()) {
            String temp = (String) it.next();
            if (RemoveFullyQualifiedName.containsMultiple(toImport.iterator(), temp, null)) {
                // import package add *
                if (temp.lastIndexOf('.') > -1) {
                    temp = temp.substring(0, temp.lastIndexOf('.'));
                    out.println("import " + temp + ".*;");
                } else
                    throw new DecompilationException("Cant find the DOT . for fullyqualified name");
            } else {
                if (temp.lastIndexOf('.') == -1) {
                // dot not found this is a class belonging to this package so dont add
                } else
                    out.println("import " + temp + ";");
            }
        }
        boolean addNewLine = false;
        addNewLine = true;
        if (addNewLine)
            out.println();
        /*if (!packagesUsed.isEmpty())
                out.println();

            packagesUsed.add("java.lang");
            packagesUsed.add(curPackage);
            */
        Dava.v().set_CurrentPackageContext(importList);
        // Dava.v().set_CurrentPackageContext(packagesUsed);
        Dava.v().set_CurrentPackage(curPackage);
    }
    // Print class name + modifiers
    {
        String classPrefix = "";
        classPrefix = classPrefix + " " + Modifier.toString(cl.getModifiers());
        classPrefix = classPrefix.trim();
        if (!cl.isInterface()) {
            classPrefix = classPrefix + " class";
            classPrefix = classPrefix.trim();
        }
        out.print(classPrefix + " " + cl.getShortJavaStyleName());
    }
    // Print extension
    if (cl.hasSuperclass() && !(cl.getSuperclass().getName().equals("java.lang.Object"))) {
        String superClassName = cl.getSuperclass().getName();
        // Nomair Naeem 8th Feb 2006
        // also check if the super class name is not a fully qualified
        // name. in which case if the package is imported no need for
        // the long name
        superClassName = RemoveFullyQualifiedName.getReducedName(importList, superClassName, cl.getType());
        out.print(" extends " + superClassName + "");
    }
    // Print interfaces
    {
        Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
        if (interfaceIt.hasNext()) {
            if (cl.isInterface())
                out.print(" extends ");
            else
                out.print(" implements ");
            out.print("" + (interfaceIt.next()).getName() + "");
            while (interfaceIt.hasNext()) out.print(", " + (interfaceIt.next()).getName() + "");
        }
    }
    out.println();
    out.println("{");
    // Print fields
    {
        Iterator<SootField> fieldIt = cl.getFields().iterator();
        if (fieldIt.hasNext()) {
            while (fieldIt.hasNext()) {
                SootField f = fieldIt.next();
                if (f.isPhantom())
                    continue;
                String declaration = null;
                Type fieldType = f.getType();
                String qualifiers = Modifier.toString(f.getModifiers()) + " ";
                qualifiers += RemoveFullyQualifiedName.getReducedName(importList, fieldType.toString(), fieldType);
                qualifiers = qualifiers.trim();
                if (qualifiers.equals(""))
                    declaration = Scene.v().quotedNameOf(f.getName());
                else
                    declaration = qualifiers + " " + Scene.v().quotedNameOf(f.getName()) + "";
                if (f.isFinal() && f.isStatic()) {
                    if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
                        double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
                        out.println("    " + declaration + " = " + val + ";");
                    } else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
                        float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
                        out.println("    " + declaration + " = " + val + "f;");
                    } else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
                        long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
                        out.println("    " + declaration + " = " + val + "l;");
                    } else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
                        int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                        out.println("    " + declaration + " = '" + ((char) val) + "';");
                    } else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
                        int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                        if (val == 0)
                            out.println("    " + declaration + " = false;");
                        else
                            out.println("    " + declaration + " = true;");
                    } else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
                        int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                        out.println("    " + declaration + " = " + val + ";");
                    } else if (f.hasTag("StringConstantValueTag")) {
                        String val = ((StringConstantValueTag) f.getTag("StringConstantValueTag")).getStringValue();
                        out.println("    " + declaration + " = \"" + val + "\";");
                    } else {
                        // System.out.println("Couldnt find type of
                        // field"+f.getDeclaration());
                        out.println("    " + declaration + ";");
                    }
                } else // field is static final
                {
                    out.println("    " + declaration + ";");
                }
            }
        }
    }
    // Print methods
    {
        Iterator<SootMethod> methodIt = cl.methodIterator();
        if (methodIt.hasNext()) {
            if (cl.getMethodCount() != 0)
                out.println();
            while (methodIt.hasNext()) {
                SootMethod method = (SootMethod) methodIt.next();
                if (method.isPhantom())
                    continue;
                if (!Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())) {
                    if (!method.hasActiveBody())
                        throw new RuntimeException("method " + method.getName() + " has no active body!");
                    else
                        printTo(method.getActiveBody(), out);
                    if (methodIt.hasNext())
                        out.println();
                } else {
                    // if method is abstract then print the declaration
                    out.print("    ");
                    out.print(method.getDavaDeclaration());
                    out.println(";");
                    if (methodIt.hasNext())
                        out.println();
                }
            }
        }
    }
    if (G.v().SootClassNeedsDavaSuperHandlerClass.contains(cl)) {
        out.println("\n    private static class DavaSuperHandler{");
        out.println("         java.util.Vector myVector = new java.util.Vector();");
        out.println("\n         public Object get(int pos){");
        out.println("            return myVector.elementAt(pos);");
        out.println("         }");
        out.println("\n         public void store(Object obj){");
        out.println("            myVector.add(obj);");
        out.println("         }");
        out.println("    }");
    }
    out.println("}");
}
Also used : LongType(soot.LongType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) ByteType(soot.ByteType) IterableSet(soot.util.IterableSet) SootClass(soot.SootClass) FloatType(soot.FloatType) IntType(soot.IntType) RefType(soot.RefType) RefType(soot.RefType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) DoubleType(soot.DoubleType) Iterator(java.util.Iterator) SootMethod(soot.SootMethod) SootField(soot.SootField) ArrayList(java.util.ArrayList) List(java.util.List) CharType(soot.CharType) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag)

Example 7 with IterableSet

use of soot.util.IterableSet in project soot by Sable.

the class SETNode method verify.

public void verify() {
    Iterator<IterableSet> sbit = subBodies.iterator();
    while (sbit.hasNext()) {
        IterableSet body = sbit.next();
        Iterator bit = body.iterator();
        while (bit.hasNext()) if ((bit.next() instanceof AugmentedStmt) == false)
            logger.debug("Error in body: " + getClass());
        Iterator cit = body2childChain.get(body).iterator();
        while (cit.hasNext()) ((SETNode) cit.next()).verify();
    }
}
Also used : Iterator(java.util.Iterator) UnmodifiableIterableSet(soot.util.UnmodifiableIterableSet) IterableSet(soot.util.IterableSet) AugmentedStmt(soot.dava.internal.asg.AugmentedStmt)

Example 8 with IterableSet

use of soot.util.IterableSet in project soot by Sable.

the class SETNode method dump.

private void dump(PrintStream out, String indentation) {
    String TOP = ".---", TAB = "|  ", MID = "+---", BOT = "`---";
    out.println(indentation);
    out.println(indentation + TOP);
    out.println(indentation + TAB + getClass());
    out.println(indentation + TAB);
    Iterator it = body.iterator();
    while (it.hasNext()) out.println(indentation + TAB + ((AugmentedStmt) it.next()).toString());
    Iterator<IterableSet> sbit = subBodies.iterator();
    while (sbit.hasNext()) {
        IterableSet subBody = sbit.next();
        out.println(indentation + MID);
        Iterator bit = subBody.iterator();
        while (bit.hasNext()) out.println(indentation + TAB + ((AugmentedStmt) bit.next()).toString());
        out.println(indentation + TAB);
        Iterator cit = body2childChain.get(subBody).iterator();
        while (cit.hasNext()) ((SETNode) cit.next()).dump(out, TAB + indentation);
    }
    out.println(indentation + BOT);
}
Also used : Iterator(java.util.Iterator) UnmodifiableIterableSet(soot.util.UnmodifiableIterableSet) IterableSet(soot.util.IterableSet)

Example 9 with IterableSet

use of soot.util.IterableSet in project soot by Sable.

the class SETNode method find_LabeledBlocks.

public void find_LabeledBlocks(LabeledBlockFinder lbf) {
    Iterator<IterableSet> sbit = subBodies.iterator();
    while (sbit.hasNext()) {
        Iterator cit = body2childChain.get(sbit.next()).iterator();
        while (cit.hasNext()) ((SETNode) cit.next()).find_LabeledBlocks(lbf);
    }
    lbf.perform_ChildOrder(this);
    lbf.find_LabeledBlocks(this);
}
Also used : Iterator(java.util.Iterator) UnmodifiableIterableSet(soot.util.UnmodifiableIterableSet) IterableSet(soot.util.IterableSet)

Example 10 with IterableSet

use of soot.util.IterableSet in project soot by Sable.

the class SETNode method add_SubBody.

public void add_SubBody(IterableSet body) {
    subBodies.add(body);
    body2childChain.put(body, new IterableSet());
}
Also used : UnmodifiableIterableSet(soot.util.UnmodifiableIterableSet) IterableSet(soot.util.IterableSet)

Aggregations

IterableSet (soot.util.IterableSet)12 Iterator (java.util.Iterator)8 UnmodifiableIterableSet (soot.util.UnmodifiableIterableSet)8 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 BooleanType (soot.BooleanType)1 ByteType (soot.ByteType)1 CharType (soot.CharType)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 SootField (soot.SootField)1