Java: ArrayList.clear 从传递给 Map 的 ArrayList 中移除元素

Java: ArrayList.clear removes elements from ArrayList passed to Map

这是我多年来第一次编写 Java 代码,我正在开发一个 Ghidra 脚本,该脚本将系统调用符号映射到它们的调用函数。

private HashMap<Symbol, Reference[]> symbolRefs = new HashMap<Symbol, Reference[]>();
private HashMap<Symbol, List<Function>> callerFuncs = new HashMap<Symbol, List<Function>>();

.
.
.

private void mapSysCallToCallerFunctions(FunctionManager funcMan) throws Exception {
    List<Function> funcs = new ArrayList<Function>();
    for(HashMap.Entry<Symbol, Reference[]> entry: this.symbolRefs.entrySet()) {
        for(Reference ref : entry.getValue()) {
            Function caller = funcMan.getFunctionContaining(ref.getFromAddress());
            if(caller != null) {
                funcs.add(caller);
            }
        }
        this.callerFuncs.put(entry.getKey(), funcs);
        funcs.clear();
    }
}

我的问题是我想清除 "funcs" 列表,以便我可以在下一次迭代中再次使用空列表。由于某些未知原因,这导致我的 HashMap 中的函数列表也为空。如果我在这里打印我的 HashMap:

private void printCallerSymbolMap() throws Exception {
    for(HashMap.Entry<Symbol, List<Function>> entry: this.callerFuncs.entrySet()) {
        printf("Symbol %s:\n", entry.getKey().toString());
        for(Function func : entry.getValue()) {
            printf("Called by function %s\n", func.getName());
        }
    }
}

我刚得到输出:

INFO  Symbol system: (GhidraScript)  
INFO  Symbol system: (GhidraScript) 

但是,当我删除 funcs.clear() 时,我得到:

INFO  Symbol system: (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function main (GhidraScript)  
INFO  Symbol system: (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function main (GhidraScript)  

不过应该是这样的:

INFO  Symbol system: (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Symbol system: (GhidraScript) 
INFO  Called by function main (GhidraScript)  

我有两个系统符号,因为它是 thunked。

代替清除列表,每次都初始化列表。

private void mapSysCallToCallerFunctions(FunctionManager funcMan) throws Exception {
    List<Function> funcs;
    for(HashMap.Entry<Symbol, Reference[]> entry: this.symbolRefs.entrySet()) {
        funcs = new ArrayList<Function>();
        for(Reference ref : entry.getValue()) {
            Function caller = funcMan.getFunctionContaining(ref.getFromAddress());
            if(caller != null) {
                funcs.add(caller);
            }
        }
        this.callerFuncs.put(entry.getKey(), funcs);
    }
}