11/*
2- * Copyright (c) 2013, 2017 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2013, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3232import java .util .Arrays ;
3333import java .util .List ;
3434
35- import javax .tools .ForwardingJavaFileManager ;
3635import javax .tools .FileObject ;
36+ import javax .tools .ForwardingJavaFileManager ;
3737import javax .tools .JavaCompiler ;
3838import javax .tools .JavaCompiler .CompilationTask ;
39+ import javax .tools .JavaFileManager ;
3940import javax .tools .JavaFileObject ;
4041import javax .tools .JavaFileObject .Kind ;
4142import javax .tools .SimpleJavaFileObject ;
@@ -167,25 +168,12 @@ public boolean hasLocation(Location location) {
167168 * @param className The name of the class
168169 * @param sourceCode The source code for the class with name {@code className}
169170 * @param options additional command line options
170- * @throws RuntimeException if the compilation did not succeed
171+ * @throws RuntimeException if the compilation did not succeed or if closing
172+ * the {@code JavaFileManager} used for the compilation did not succeed
171173 * @return The resulting byte code from the compilation
172174 */
173175 public static byte [] compile (String className , CharSequence sourceCode , String ... options ) {
174176 MemoryJavaFileObject file = new MemoryJavaFileObject (className , sourceCode );
175- CompilationTask task = getCompilationTask (file , options );
176-
177- if (!task .call ()) {
178- throw new RuntimeException ("Could not compile " + className + " with source code " + sourceCode );
179- }
180-
181- return file .getByteCode ();
182- }
183-
184- private static JavaCompiler getCompiler () {
185- return ToolProvider .getSystemJavaCompiler ();
186- }
187-
188- private static CompilationTask getCompilationTask (MemoryJavaFileObject file , String ... options ) {
189177 List <String > opts = new ArrayList <>();
190178 String moduleOverride = null ;
191179 for (String opt : options ) {
@@ -195,6 +183,19 @@ private static CompilationTask getCompilationTask(MemoryJavaFileObject file, Str
195183 opts .add (opt );
196184 }
197185 }
198- return getCompiler ().getTask (null , new FileManagerWrapper (file , moduleOverride ), null , opts , null , Arrays .asList (file ));
186+ try (JavaFileManager fileManager = new FileManagerWrapper (file , moduleOverride )) {
187+ CompilationTask task = getCompiler ().getTask (null , fileManager , null , opts , null , Arrays .asList (file ));
188+ if (!task .call ()) {
189+ throw new RuntimeException ("Could not compile " + className + " with source code " + sourceCode );
190+ }
191+
192+ return file .getByteCode ();
193+ } catch (IOException ioe ) {
194+ throw new RuntimeException (ioe );
195+ }
196+ }
197+
198+ private static JavaCompiler getCompiler () {
199+ return ToolProvider .getSystemJavaCompiler ();
199200 }
200201}
0 commit comments