< prev index next >

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

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

*** 26,44 **** --- 26,47 ---- 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,116 **** return className; } } private static class FileManagerWrapper extends ForwardingJavaFileManager { ! private MemoryJavaFileObject file; ! public FileManagerWrapper(MemoryJavaFileObject file) { super(getCompiler().getStandardFileManager(null, null, null)); this.file = file; } @Override public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) --- 105,132 ---- return className; } } private static class FileManagerWrapper extends ForwardingJavaFileManager { ! 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, 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,128 **** --- 135,166 ---- 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,153 **** 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)); } } --- 184,200 ---- private static JavaCompiler getCompiler() { return ToolProvider.getSystemJavaCompiler(); } private static CompilationTask getCompilationTask(MemoryJavaFileObject file, String... options) { ! 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 >