我知道 SO 上有很多 CCE 问题。我已详细或简要地阅读了其中的大部分内容,但找不到任何适用于我的情况的内容。我的确切错误是:
Exception in thread "pool-1-thread-1" java.lang.ClassCastException: datastructures.instances.JClass cannot be cast to java.util.ArrayList
在if ((results = mCallsDownstreamCache.get(origin)) == null) {
正如您将在代码中看到的,我正在做的是请求 ArrayList
从缓存( HashMap
)中,然后做出决定。这里奇怪的行为是 datastructures.instances.JClass
绝不在生成错误的代码段中引用。
为了给您一些背景信息,我有一个数据库“模型”,它满足“ Controller ”的请求。这些结果存储在模型本地的缓存中,如果它们存在,模型将返回缓存,因此不必访问数据库。我的缓存元素实际上是 Commons' JCS 的装饰器.
有问题的行被包裹在 block comment
中和inline comment
public class AnalyzeModel extends Model {
public final String TAG = getClass().getSimpleName();
public CacheDecorator<Integer, JClass> mClassCache = new CacheDecorator<Integer, JClass>();
public CacheDecorator<Integer, JMethod> mMethodCache = new CacheDecorator<Integer, JMethod>();
public CacheDecorator<Integer, ArrayList<Integer>> mCallsUpstreamCache =
new CacheDecorator<Integer, ArrayList<Integer>>();
public CacheDecorator<Integer, ArrayList<Integer>> mCallsDownstreamCache =
new CacheDecorator<Integer, ArrayList<Integer>>();
public void close() {
super.close();
}
public Pair<Integer, ArrayList<Integer>> selectCallGraphDownstream(int origin) {
ArrayList<Integer> results = new ArrayList<Integer>();
/**
* This is the offending line
*/
if ((results = mCallsDownstreamCache.get(origin)) == null) {
// End error line
results = new ArrayList<Integer>();
for (Record r : mQuery.select(
mQuery.mQuerier.select(Calls.CALLS.TID)
.from(Calls.CALLS)
.where(Calls.CALLS.SID.eq(origin)))) {
results.add(r.getValue(Calls.CALLS.TID));
}
mCallsDownstreamCache.put(origin, results);
Statistics.CACHE_MISS++;
} else {
Statistics.CACHE_HITS++;
}
return new Pair<Integer, ArrayList<Integer>>(origin, results);
}
}
public class CacheDecorator<K, V> {
public final String TAG = getClass().getSimpleName();
private CacheAccess<K, V> mCache;
public CacheDecorator() {
try {
mCache = JCS.getInstance("default");
} catch (CacheException e) {
BillBoard.e(TAG, "Error getting cache configuration: " + e.toString());
e.printStackTrace();
}
}
/**
* Get an object from cache
* @param obj object to retrieve from cache
* @return generic object of retrieved value, null if not found
*/
public V get(K obj) {
return mCache.get(obj);
}
/**
* Place an object in cache
* @param key generic key for reference
* @param obj generic object to be cached
*/
public synchronized void put(K key, V obj) {
try {
if(obj != null) {
mCache.putSafe(key, obj);
}
} catch( CacheException e) {
//BillBoard.d(TAG, obj.toString());
//BillBoard.e(TAG, "Error placing item in cache: " + e.toString());
//e.printStackTrace();
}
}
/**
* Get the stats from our cache manager
* @return String of our cache object
*/
public String getStats() {
shutDownCache();
return mCache.getStats();
}
public static void shutDownCache() {
CompositeCacheManager.getInstance().shutDown();
}
}
一些可能有用或可能没有帮助的其他详细信息:
Pair<V, K>
数据结构只是一个不可变的两对元组类-
CacheDecorator.get(V obj)
返回null
如果缓存中不存在该对象 - 我在选角等方面尝试了很多
-
JClass
代码中其他地方确实有引用,但有问题的方法中没有引用 -
JClass
是一个java类的表示,它是一个自定义结构
最佳答案
通过更改您的配置以包含特定于区域的配置,如 documentation 中所示。 ,并将一个区域传递给您的包装器,应该可以解决问题。
看起来您对所有包装器使用相同的缓存区域,因此在包装器中引用相同的“底层缓存”。您能否将所有包装器的 mCache = JCS.getInstance("default");
更改为 mCache = JCS.getInstance("uniqueNameForWrapper");
之类的内容?
关于java - ClassCastException:datastructs.instances.JClass 无法转换为 java.util.ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30720411/