javasuperoutputstreamtry-with-resources

Warning try-with-resources for closing super OutputStream


The bottom code compiles an input String and returns the result, as an int here. Although it works, I get a Netbeans warning telling me that the super.close() line in the close() method of the NewOutputStream class should be handled with try-with-resources. The change to:

try (super) {
    this.linkedHashMap.put(this.string,this.byteArrayOutputStream.toByteArray());
}

doesn't work. This question might also be related to try-with-resources-when-calling-super-constructor, but I don't know how to adapt the answer to my code.

Here's my code:

package group.javacompiler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class JavaCompiler {

    public static void main(String[] args) {
        try {
            String input = "1+2";
            String returnType = "int";

            String methodName = "methodName";
            String className = "ClassName";
            ArrayList<JavaFileObject> arrayList = new ArrayList<>();
            arrayList.add(new NewCharContent(className+".java",
                "public class "+className+" {\n"+
                "    public static "+returnType+" "+methodName+"() {\n"+
                "       return "+input+";\n"+
                "    }\n"+
                "}"
            ));
            LinkedHashMap<String,byte[]> linkedHashMap = new LinkedHashMap<>();
            javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            compiler.getTask(null,new NewForwardingJavaFileManager(compiler.getStandardFileManager(null,null,null),linkedHashMap),null,null,null,arrayList).call();

            int result = (int) new NewClassLoader(linkedHashMap).findClass(className).getMethod(methodName).invoke(null,new Object[]{});
            System.out.println(result);
        }
        catch (Throwable throwable) {
            System.out.println(throwable.getMessage());
        }
    }
    private static class NewCharContent extends SimpleJavaFileObject {
        
        private final String charSequence;
        
        private NewCharContent(String string,String charSequence) {
            super(URI.create("string:///"+string),Kind.SOURCE);
            this.charSequence = charSequence;
        }
        
        @Override
        public CharSequence getCharContent(boolean bool) {
            return this.charSequence;
        }
        
    }

    private static class NewForwardingJavaFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {

        private final LinkedHashMap<String,byte[]> linkedHashMap;

        private NewForwardingJavaFileManager(StandardJavaFileManager fileManager,LinkedHashMap<String,byte[]> linkedHashMap) {
            super(fileManager);
            this.linkedHashMap = linkedHashMap;
        }

        @Override
        public JavaFileObject getJavaFileForOutput(final Location location,final String string,Kind kind,FileObject fileOject) {
            return new NewJavaFileForOutput(string,this.linkedHashMap);
        }

    }

    private static class NewJavaFileForOutput extends SimpleJavaFileObject {

        private final String string;
        private final LinkedHashMap<String,byte[]> linkedHashMap;

        private NewJavaFileForOutput(String string,LinkedHashMap<String,byte[]> linkedHashMap) {
            super(URI.create("string:///"+string+".class"),Kind.CLASS);
            this.string = string;
            this.linkedHashMap = linkedHashMap;
        }

        @Override
        public OutputStream openOutputStream() {
            return new NewOutputStream(this.string,this.linkedHashMap);
        }

    }

    private static class NewOutputStream extends OutputStream {

        private final String string;
        private final LinkedHashMap<String,byte[]> linkedHashMap;
        private final ByteArrayOutputStream byteArrayOutputStream;

        private NewOutputStream(String string,LinkedHashMap<String,byte[]> linkedHashMap) {
            this.string = string;
            this.linkedHashMap = linkedHashMap;
            this.byteArrayOutputStream = new ByteArrayOutputStream();
        }

        @Override
        public void write(int intValue) {
            this.byteArrayOutputStream.write(intValue);
        }

        @Override
        public void close() throws IOException {
            this.linkedHashMap.put(this.string,this.byteArrayOutputStream.toByteArray());
            super.close();
        }

    }

    private static class NewClassLoader extends ClassLoader {

        private final LinkedHashMap<String,byte[]> linkedHashMap;

        private NewClassLoader(LinkedHashMap<String,byte[]> linkedHashMap) {
            this.linkedHashMap = linkedHashMap;
        }

        @Override
        public Class<?> findClass(String string) {
            byte[] byteArray = this.linkedHashMap.get(string);
            return defineClass(string,byteArray,0,byteArray.length);
        }

    }

}

Solution

  • As mentioned the super implementation is known to do nothing, but the problem here is that super.close() is not necessarily called. A simple try-finally is sufficient.

        @Override
        public void close() throws IOException {
            try {
                linkedHashMap.put(string,
                        byteArrayOutputStream.toByteArray());
            } finally {
                super.close();
            }
        }
    

    Actually try-with-resources should have been done like:

    try (NewOutputStream nos = new NewOutputStream(this.string,this.linkedHashMap)) {
        ...
    }
    

    This ensures the closing.

    You would need to do some things differently, but I am glad seeing advanced compiler related code.

    Here I would consider easier (more direct solvable) coding style issues.