I have this class in Java (it’s from JaCoCo Project):
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);
}
}
When I compile this class the Eclipse create MemoryMultiReportOutput.class and MemoryMultiReportOutput$1.class.
First question: Why Eclipse create the MemoryMultiReportOutput$1.class? Eclipse considers the ByteArrayOutputStream out a InnerClass?
But my problem is, when I load the MemoryMultiReportOutput.class how can I load the all innerclasses present in parent class?
To answer your first question:
Here you are creating a subclass of the ByteArrayOutputStream on the fly, i.e anonymous. This is why you have another .class file.
To answer your second question:
You can only load parent inner classes, visible to the subclass, through the Superclass’s instance object :
If the inner class is static i.e a top-level nested class (since there is no such thing as inner static class) can be instantiated like this:
and it does not require an object instance of the superclass.
Hope this helps!