我有以下HashMap:
Map<String, String[]> aMap = new HashMap<String, String[]>()
以及具有以下签名的方法:
private boolean verifyFileName(String fileName, String[] allowableValues)
我调用这样的方法:
Map.Entry<String, String[]> pair = (Map.Entry)it.next();
...
verifyFileName(file.getName(), pair.getValue())
问题是verifyFileName
抛出一个错误,说它的第二个参数是ArrayList
。当我将verifyFileName
的签名更改为(String fileName, ArrayList<String> allowableValues)
时,错误消失。
这里发生了什么?
编辑:这是我使用HashMap的JUnit测试(用Groovy编写):
@Test
public void verifyGeneratedAndCompiledFiles() {
Iterator it = DIRECTORIES_TO_CHECK.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String[]> pair = it.next();
int count = 0
File createdFolder = new File(pair.getKey())
for (File file: createdFolder.listFiles()) {
println('checking for ' + file.getName())
if (file.length() == 0) {
fail(createdFolder.getPath() + ' folder contains an empty file')
}
if (!verifyFileName(file.getName(), pair.getValue())) {
fail(createdFolder.getPath() + ' folder is missing ' + file.getName())
}
count++
}
if (count != 2) {
fail(createdFolder.getName() + " folder contains an incorrect number of files")
}
count = 0
}
}
以及我在此声明和初始化DIRECTORIES_TO_CHECK
:
private static final Map<String, String[]> DIRECTORIES_TO_CHECK
static {
Map<String, String[]> aMap = new HashMap<String, String[]>()
aMap.put(DIRECTORIES_ROOT + "subproj/build/classes/generatedSource", ["Subproj.class", "Subproj2.class"])
aMap.put(DIRECTORIES_ROOT + "sub2/build/classes/generatedSource", ["Sub2.class", "Sub22.class"])
aMap.put(DIRECTORIES_ROOT + "subproj/generated/java", ["Subproj.java, Subproj2.java"])
aMap.put(DIRECTORIES_ROOT + "sub2/generated/java", ["Sub2.java", "Sub22.java"])
DIRECTORIES_TO_CHECK = Collections.unmodifiableMap(aMap)
}