我對Java的這個類(這是從JaCoCo項目):負載內部類的Java
public class MemoryMultiReportOutput implements IMultiReportOutput {
private final Map<String, ByteArrayOutputStream> files = new HashMap<String, ByteArrayOutputStream>();
private final Set<String> open = new HashSet<String>();
private boolean closed = false;
public OutputStream createFile(final String path) throws IOException {
assertFalse("Duplicate output " + path, files.containsKey(path));
open.add(path);
final ByteArrayOutputStream out = new ByteArrayOutputStream() {
@Override
public void close() throws IOException {
open.remove(path);
super.close();
}
};
files.put(path, out);
return out;
}
public void close() throws IOException {
closed = true;
}
public void assertEmpty() {
assertEquals(Collections.emptySet(), files.keySet());
}
public void assertFile(String path) {
assertNotNull(String.format("Missing file %s. Actual files are %s.",
path, files.keySet()), files.get(path));
}
public void assertSingleFile(String path) {
assertEquals(Collections.singleton(path), files.keySet());
}
public byte[] getFile(String path) {
assertFile(path);
return files.get(path).toByteArray();
}
public InputStream getFileAsStream(String path) {
return new ByteArrayInputStream(getFile(path));
}
public void assertAllClosed() {
assertEquals(Collections.emptySet(), open);
assertTrue(closed);
}
}
當我編譯這個類在Eclipse中創建MemoryMultiReportOutput.class
和MemoryMultiReportOutput$1.class
。
第一個問題:爲什麼Eclipse創建了MemoryMultiReportOutput$1.class
? Eclipse認爲ByteArrayOutputStream out
是一個InnerClass?
但我的問題是,當我加載MemoryMultiReportOutput.class
如何加載父類中的所有內部類?
爲什麼你擔心加載內部類?你在寫一個類加載器嗎? – Yishai
我有一個調用此類的JUnit測試,如果我沒有MemoryMultiReportOutput.class和MemoryMultiReportOutput $ 1.class加載,JUnitCore.run(...)的結果是ClassNotFound ... 是的,我有這[ ClassLoader](http://pastebin.com/TjmLr702)... – josecampos