简体   繁体   中英

Load Inner Class Java

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:

final ByteArrayOutputStream out = new ByteArrayOutputStream() {
        @Override
        public void close() throws IOException {
            open.remove(path);
            super.close();
        }
    };

Here you are creating a subclass of the ByteArrayOutputStream on the fly, ie 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 :

Superclass s = new Superclass();
Superclass.Subclass sub = s.new Subclass();

If the inner class is static ie a top-level nested class (since there is no such thing as inner static class) can be instantiated like this:

Superclass.Subclass s = new Superclass.Subclass();

and it does not require an object instance of the superclass.

Hope this helps!

Your creating an anonymous inner class with the

new ByteArrayOutputStream()

That's why you see the MemoryMultiReportOutput$1.class file.

You don't need to do anything to load the inner classes. That will happen automatically.

If your asking how to access the inner class from another class that's a bit different. You would need to mark it public or provide an accessor that would return an instance of the class. Is that what you were asking?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM