/* Soot - a Java Optimization Framework
 * Copyright (C) 2012 Michael Markert, Frank Hartmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package soot.dex;

import java.io.File;

import soot.SootClass;
import soot.SootResolver;
import soot.javaToJimple.IInitialResolver.Dependencies;
import soot.dex.DexlibWrapper;

public class DexResolver {
    /**
     * Resolve the class contained in file into the passed soot class.
     *
     * @param file the path to the dex/apk file to resolve
     * @param className the name of the class to resolve
     * @param sc the soot class that will represent the class
     * @return the dependencies of this class.
     */
    public static Dependencies resolveFromFile(File file, String className, SootClass sc) {
        DexClass c = new DexlibWrapper(file).getClass(className);
        if (c == null)
            throw new RuntimeException("Class " + className + " not found at " + file.getPath());

        sc.setModifiers(c.getModifiers());
        Dependencies deps = new Dependencies();
        // interfaces for hierarchy level
        for (String interfaceName : c.getInterfaces()) {
            String interfaceClassName = Util.dottedClassName(interfaceName);
            SootClass interfaceClass = SootResolver.v().makeClassRef(interfaceClassName);
            sc.addInterface(interfaceClass);
            deps.typesToHierarchy.add(interfaceClass.getType());
        }
        // super class for hierarchy level
        String superClassName = Util.dottedClassName(c.getSuperclass());
        SootClass superClass = SootResolver.v().makeClassRef(superClassName);
        sc.setSuperclass(superClass);
        deps.typesToHierarchy.add(superClass.getType());

        // all types for signature level
        for (DexType t : c.getAllTypes()) {
            deps.typesToSignature.add(t.toSoot());
        }

        // fields
        for (DexField f : c.getDeclaredFields()) {
            sc.addField(f.toSoot());
        }

        // methods
        for (DexMethod m : c.getDeclaredMethods()) {
            sc.addMethod(m.toSoot());
        }

        return deps;
    }
}

@@ -896,16 +896,66 @@
 o.File;%0D
+%0Aimport java.util.Map;%0D%0Aimport java.util.TreeMap;%0D
 %0A%0D%0Aimpor
@@ -1061,68 +1061,119 @@
 s;%0D%0A
-import soot.dex.DexlibWrapper;%0D%0A%0D%0Apublic class DexResolver %7B
+%0D%0Apublic class DexResolver %7B%0D%0A%09%0D%0A%09static Map%3CFile,DexlibWrapper%3E cache = new TreeMap%3CFile, DexlibWrapper%3E();%0D%0A%09
 %0D%0A  
@@ -1598,48 +1598,188 @@
     
-    DexClass c = new DexlibWrapper(file)
+%09DexlibWrapper wrapper = cache.get(file);%0D%0A    %09if(wrapper==null) %7B%0D%0A    %09%09wrapper = new DexlibWrapper(file);%0D%0A    %09%09cache.put(file, wrapper);%0D%0A    %09%7D%0D%0A        DexClass c = wrapper
 .get

fix for memory leak