Skip to content

Commit

Permalink
make Caches more specific; fix/implement MutableCache.putClass()
Browse files Browse the repository at this point in the history
  • Loading branch information
swissiety committed Jan 21, 2025
1 parent cb2af47 commit 7bd9e7d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
31 changes: 24 additions & 7 deletions sootup.core/src/main/java/sootup/core/cache/ClassCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends SootClass> {

SootClass getClass(ClassType classType);
/**
* return the SootClass identified by classType
* */
C getClass(@Nonnull ClassType classType);

/**
* return all SootClasses stored in this Cache
* */
@Nonnull
Collection<SootClass> getClasses();

void putClass(ClassType classType, SootClass sootClass);

boolean hasClass(ClassType classType);
Collection<C> 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();
}
14 changes: 7 additions & 7 deletions sootup.core/src/main/java/sootup/core/cache/FullCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends SootClass> implements ClassCache<C> {

protected final Map<ClassType, SootClass> cache = new HashMap<>();
protected final Map<ClassType, C> 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<SootClass> getClasses() {
public synchronized Collection<C> 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);
}

Expand Down
19 changes: 10 additions & 9 deletions sootup.core/src/main/java/sootup/core/cache/LRUCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassType, SootClass> cache;
public class LRUCache<C extends SootClass> implements ClassCache<C> {

@Nonnull private final LinkedHashMap<ClassType, C> cache;

public LRUCache(int cacheSize) {
cache =
new LinkedHashMap<ClassType, SootClass>(cacheSize, 1, true) {
new LinkedHashMap<ClassType, C>(cacheSize, 1, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<ClassType, SootClass> eldest) {
protected boolean removeEldestEntry(Map.Entry<ClassType, C> 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<SootClass> getClasses() {
public synchronized Collection<C> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends SootClass> extends ClassCache<C> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@
*/

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import sootup.core.model.SootClass;
import sootup.core.types.ClassType;

/**
* 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<C extends SootClass> extends FullCache<C> implements MutableClassCache<C> {

/**
* 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)) {
Expand Down

0 comments on commit 7bd9e7d

Please sign in to comment.