ClassCastException: datastructures.instances.JClass 无法转换为 java.util.ArrayList

ClassCastException: datastructures.instances.JClass cannot be cast to java.util.ArrayList

我知道有很多关于 SO 的 CCE 问题。我已经详细或简要地阅读了其中的大部分内容,但我找不到任何适合我情况的内容。我的确切错误是:

Exception in thread "pool-1-thread-1" java.lang.ClassCastException: datastructures.instances.JClass cannot be cast to java.util.ArrayListif ((results = mCallsDownstreamCache.get(origin)) == null) {

正如您将在代码中看到的,我正在做的是从缓存 (HashMap) 中请求 ArrayList,然后据此做出决定。这里的奇怪行为是 datastructures.instances.JClass 无法在生成错误的代码段中引用。

为了给您一些背景信息,我有一个数据库 "model" 可以满足来自 "controller" 的请求。这些结果存储在模型本地的缓存中,如果它们存在,模型将 return 缓存因此不必访问数据库。我的缓存元素实际上是 Commons' JCS.

的装饰器

违规行包含在 block commentinline 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();
    }
}

一些可能有帮助或无帮助的其他详细信息:

通过更改您的配置以包含 documentation 中显示的区域特定配置,并将区域传递给您的包装器,应该可以解决问题。

看起来您正在为所有包装器使用相同的缓存区域,因此在您的包装器中引用相同的 'under laying cache'。您能否将所有包装器的 mCache = JCS.getInstance("default"); 更改为 mCache = JCS.getInstance("uniqueNameForWrapper"); 之类的内容?