diff --git a/sootup.core/src/main/java/sootup/core/cache/ClassCache.java b/sootup.core/src/main/java/sootup/core/cache/ClassCache.java index 27674076559..9ff63ae4651 100644 --- a/sootup.core/src/main/java/sootup/core/cache/ClassCache.java +++ b/sootup.core/src/main/java/sootup/core/cache/ClassCache.java @@ -24,20 +24,37 @@ import java.util.Collection; import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import sootup.core.frontend.SootClassSource; import sootup.core.model.SootClass; import sootup.core.types.ClassType; /** Interface for different caching strategies of resolved classes. */ -public interface ClassCache { +public interface ClassCache { - SootClass getClass(ClassType classType); + /** + * return the SootClass identified by classType + * */ + C getClass(@Nonnull ClassType classType); + /** + * return all SootClasses stored in this Cache + * */ @Nonnull - Collection getClasses(); - - void putClass(ClassType classType, SootClass sootClass); - - boolean hasClass(ClassType classType); + Collection getClasses(); + + /** + * add the given SootClass into the Cache + * */ + // TODO: simplify: classType can be retrieved from sootClass.getType() + @Nullable + C putClass(@Nonnull ClassType classType, @Nonnull C sootClass); + + /** + * Check if the associated SootClass for ClassType exists in the Cache + * */ + boolean hasClass(@Nonnull ClassType classType); int size(); } diff --git a/sootup.core/src/main/java/sootup/core/cache/FullCache.java b/sootup.core/src/main/java/sootup/core/cache/FullCache.java index f1932077315..852bd645940 100644 --- a/sootup.core/src/main/java/sootup/core/cache/FullCache.java +++ b/sootup.core/src/main/java/sootup/core/cache/FullCache.java @@ -30,28 +30,28 @@ import sootup.core.types.ClassType; /** Cache that stores any class that has been resolved. */ -public class FullCache implements ClassCache { +public class FullCache implements ClassCache { - protected final Map cache = new HashMap<>(); + protected final Map cache = new HashMap<>(); @Override - public synchronized SootClass getClass(ClassType classType) { + public synchronized C getClass(@Nonnull ClassType classType) { return cache.get(classType); } @Nonnull @Override - public synchronized Collection getClasses() { + public synchronized Collection getClasses() { return cache.values(); } @Override - public void putClass(ClassType classType, SootClass sootClass) { - cache.putIfAbsent(classType, sootClass); + public C putClass(@Nonnull ClassType classType, @Nonnull C sootClass) { + return cache.putIfAbsent(classType, sootClass); } @Override - public boolean hasClass(ClassType classType) { + public boolean hasClass(@Nonnull ClassType classType) { return cache.containsKey(classType); } diff --git a/sootup.core/src/main/java/sootup/core/cache/LRUCache.java b/sootup.core/src/main/java/sootup/core/cache/LRUCache.java index 3f327d99a56..aafd404db03 100644 --- a/sootup.core/src/main/java/sootup/core/cache/LRUCache.java +++ b/sootup.core/src/main/java/sootup/core/cache/LRUCache.java @@ -31,37 +31,38 @@ * Cache that implements a least recently used strategy. If the amount of stored classes exceeds a * specified amount, the lest recently used class will be overwritten. */ -public class LRUCache implements ClassCache { - private final LinkedHashMap cache; +public class LRUCache implements ClassCache { + + @Nonnull private final LinkedHashMap cache; public LRUCache(int cacheSize) { cache = - new LinkedHashMap(cacheSize, 1, true) { + new LinkedHashMap(cacheSize, 1, true) { @Override - protected boolean removeEldestEntry(Map.Entry eldest) { + protected boolean removeEldestEntry(Map.Entry eldest) { return size() > cacheSize; }; }; } @Override - public synchronized SootClass getClass(ClassType classType) { + public synchronized C getClass(@Nonnull ClassType classType) { return cache.get(classType); } @Nonnull @Override - public synchronized Collection getClasses() { + public synchronized Collection getClasses() { return cache.values(); } @Override - public void putClass(ClassType classType, SootClass sootClass) { - cache.putIfAbsent(classType, sootClass); + public C putClass(@Nonnull ClassType classType, @Nonnull C sootClass) { + return cache.putIfAbsent(classType, sootClass); } @Override - public boolean hasClass(ClassType classType) { + public boolean hasClass(@Nonnull ClassType classType) { return cache.containsKey(classType); } diff --git a/sootup.core/src/main/java/sootup/core/cache/MutableClassCache.java b/sootup.core/src/main/java/sootup/core/cache/MutableClassCache.java index c4d1435e1ca..16bb440d1ff 100644 --- a/sootup.core/src/main/java/sootup/core/cache/MutableClassCache.java +++ b/sootup.core/src/main/java/sootup/core/cache/MutableClassCache.java @@ -27,11 +27,11 @@ import sootup.core.types.ClassType; /** Interface for caches which are mutable and allow classes to be removed from. */ -public interface MutableClassCache extends ClassCache { +public interface MutableClassCache extends ClassCache { SootClass removeClass(ClassType classType); default SootClass replaceClass( - @Nonnull ClassType oldType, @Nonnull ClassType newType, @Nonnull SootClass newClass) { + @Nonnull ClassType oldType, @Nonnull ClassType newType, @Nonnull C newClass) { SootClass oldClass = removeClass(oldType); putClass(newType, newClass); return oldClass; diff --git a/sootup.core/src/main/java/sootup/core/cache/MutableFullCache.java b/sootup.core/src/main/java/sootup/core/cache/MutableFullCache.java index e03b8512724..a22c82ac1e8 100644 --- a/sootup.core/src/main/java/sootup/core/cache/MutableFullCache.java +++ b/sootup.core/src/main/java/sootup/core/cache/MutableFullCache.java @@ -23,6 +23,8 @@ */ import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import sootup.core.model.SootClass; import sootup.core.types.ClassType; @@ -30,8 +32,20 @@ * Mutable version of the {@link FullCache} that additionally allows for a removal of cached * classes. */ -public class MutableFullCache extends FullCache implements MutableClassCache { +public class MutableFullCache extends FullCache implements MutableClassCache { + + /** + * set SootClass into Cache, if element exists it will be replaced. + * */ + @Override + @Nullable + public C putClass(@Nonnull ClassType classType, @Nonnull C sootClass) { + return cache.put(classType, sootClass); + } + /** + * removes the SootClass identified by classType from the Cache. + * */ @Override public SootClass removeClass(@Nonnull ClassType classType) { if (this.hasClass(classType)) {