< prev index next >

test/lib/jdk/test/lib/InMemoryJavaCompiler.java

Print this page
rev 2476 : [mq]: xmodule-to-patch-module

@@ -26,19 +26,22 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import javax.tools.ForwardingJavaFileManager;
 import javax.tools.FileObject;
 import javax.tools.JavaCompiler;
 import javax.tools.JavaCompiler.CompilationTask;
 import javax.tools.JavaFileObject;
 import javax.tools.JavaFileObject.Kind;
 import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardLocation;
 import javax.tools.ToolProvider;
 
 /**
  * {@code InMemoryJavaCompiler} can be used for compiling a {@link
  * CharSequence} to a {@code byte[]}.

@@ -102,15 +105,28 @@
             return className;
         }
     }
 
     private static class FileManagerWrapper extends ForwardingJavaFileManager {
-        private MemoryJavaFileObject file;
+        private static final Location PATCH_LOCATION = new Location() {
+            @Override
+            public String getName() {
+                return "patch module location";
+            }
+
+            @Override
+            public boolean isOutputLocation() {
+                return false;
+            }
+        };
+        private final MemoryJavaFileObject file;
+        private final String moduleOverride;
 
-        public FileManagerWrapper(MemoryJavaFileObject file) {
+        public FileManagerWrapper(MemoryJavaFileObject file, String moduleOverride) {
             super(getCompiler().getStandardFileManager(null, null, null));
             this.file = file;
+            this.moduleOverride = moduleOverride;
         }
 
         @Override
         public JavaFileObject getJavaFileForOutput(Location location, String className,
                                                    Kind kind, FileObject sibling)

@@ -119,10 +135,32 @@
                 throw new IOException("Expected class with name " + file.getClassName() +
                                       ", but got " + className);
             }
             return file;
         }
+
+        @Override
+        public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
+            if (fo == file && moduleOverride != null) {
+                return PATCH_LOCATION;
+            }
+            return super.getLocationForModule(location, fo, pkgName);
+        }
+
+        @Override
+        public String inferModuleName(Location location) throws IOException {
+            if (location == PATCH_LOCATION) {
+                return moduleOverride;
+            }
+            return super.inferModuleName(location);
+        }
+
+        @Override
+        public boolean hasLocation(Location location) {
+            return super.hasLocation(location) || location == StandardLocation.PATCH_MODULE_PATH;
+        }
+
     }
 
     /**
      * Compiles the class with the given name and source code.
      *

@@ -146,8 +184,17 @@
     private static JavaCompiler getCompiler() {
         return ToolProvider.getSystemJavaCompiler();
     }
 
     private static CompilationTask getCompilationTask(MemoryJavaFileObject file, String... options) {
-        return getCompiler().getTask(null, new FileManagerWrapper(file), null, Arrays.asList(options), null, Arrays.asList(file));
+        List<String> opts = new ArrayList<>();
+        String moduleOverride = null;
+        for (String opt : options) {
+            if (opt.startsWith("-Xmodule:")) {
+                moduleOverride = opt.substring("-Xmodule:".length());
+            } else {
+                opts.add(opt);
+            }
+        }
+        return getCompiler().getTask(null, new FileManagerWrapper(file, moduleOverride), null, opts, null, Arrays.asList(file));
     }
 }
< prev index next >