10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javac.file; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.net.MalformedURLException; 31 import java.net.URI; 32 import java.net.URISyntaxException; 33 import java.net.URL; 34 import java.nio.CharBuffer; 35 import java.nio.charset.Charset; 36 import java.nio.file.FileSystem; 37 import java.nio.file.FileSystems; 38 import java.nio.file.FileVisitOption; 39 import java.nio.file.FileVisitResult; 40 import java.nio.file.Files; 41 import java.nio.file.InvalidPathException; 42 import java.nio.file.LinkOption; 43 import java.nio.file.Path; 44 import java.nio.file.Paths; 45 import java.nio.file.ProviderNotFoundException; 46 import java.nio.file.SimpleFileVisitor; 47 import java.nio.file.attribute.BasicFileAttributes; 48 import java.nio.file.spi.FileSystemProvider; 49 import java.util.ArrayList; 60 import java.util.Set; 61 import java.util.stream.Collectors; 62 import java.util.stream.Stream; 63 64 import javax.lang.model.SourceVersion; 65 import javax.tools.FileObject; 66 import javax.tools.JavaFileManager; 67 import javax.tools.JavaFileObject; 68 import javax.tools.StandardJavaFileManager; 69 70 import com.sun.tools.javac.file.RelativePath.RelativeDirectory; 71 import com.sun.tools.javac.file.RelativePath.RelativeFile; 72 import com.sun.tools.javac.resources.CompilerProperties.Errors; 73 import com.sun.tools.javac.util.Assert; 74 import com.sun.tools.javac.util.Context; 75 import com.sun.tools.javac.util.Context.Factory; 76 import com.sun.tools.javac.util.DefinedBy; 77 import com.sun.tools.javac.util.DefinedBy.Api; 78 import com.sun.tools.javac.util.List; 79 import com.sun.tools.javac.util.ListBuffer; 80 import com.sun.tools.javac.util.JDK9Wrappers.Configuration; 81 import com.sun.tools.javac.util.JDK9Wrappers.Layer; 82 import com.sun.tools.javac.util.JDK9Wrappers.ModuleFinder; 83 import com.sun.tools.javac.util.JDK9Wrappers.Module; 84 import com.sun.tools.javac.util.JDK9Wrappers.ServiceLoaderHelper; 85 86 import static java.nio.file.FileVisitOption.FOLLOW_LINKS; 87 88 import static javax.tools.StandardLocation.*; 89 90 /** 91 * This class provides access to the source, class and other files 92 * used by the compiler and related tools. 93 * 94 * <p><b>This is NOT part of any supported API. 95 * If you write code that depends on this, you do so at your own risk. 96 * This code and its internal interfaces are subject to change or 97 * deletion without notice.</b> 98 */ 99 public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager { 100 101 public static char[] toArray(CharBuffer buffer) { 102 if (buffer.hasArray()) 103 return buffer.compact().flip().array(); 104 else 963 return locations.getOutputLocation(CLASS_OUTPUT); 964 } 965 966 private Path getSourceOutDir() { 967 return locations.getOutputLocation(SOURCE_OUTPUT); 968 } 969 970 @Override @DefinedBy(Api.COMPILER) 971 public Location getLocationForModule(Location location, String moduleName) throws IOException { 972 checkModuleOrientedOrOutputLocation(location); 973 nullCheck(moduleName); 974 if (location == SOURCE_OUTPUT && getSourceOutDir() == null) 975 location = CLASS_OUTPUT; 976 return locations.getLocationForModule(location, moduleName); 977 } 978 979 @Override @DefinedBy(Api.COMPILER) 980 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException { 981 nullCheck(location); 982 nullCheck(service); 983 Module.getModule(getClass()).addUses(service); 984 if (location.isModuleOrientedLocation()) { 985 Collection<Path> paths = locations.getLocation(location); 986 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()])); 987 Layer bootLayer = Layer.boot(); 988 Configuration cf = bootLayer.configuration().resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet()); 989 Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader()); 990 return ServiceLoaderHelper.load(layer, service); 991 } else { 992 return ServiceLoader.load(service, getClassLoader(location)); 993 } 994 } 995 996 @Override @DefinedBy(Api.COMPILER) 997 public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException { 998 checkModuleOrientedOrOutputLocation(location); 999 if (!(fo instanceof PathFileObject)) 1000 return null; 1001 Path p = Locations.normalize(((PathFileObject) fo).path); 1002 // need to find p in location 1003 return locations.getLocationForModule(location, p); 1004 } 1005 1006 @Override @DefinedBy(Api.COMPILER) 1007 public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths) 1008 throws IOException { 1009 nullCheck(location); 1010 checkModuleOrientedOrOutputLocation(location); | 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javac.file; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.lang.module.Configuration; 31 import java.lang.module.ModuleFinder; 32 import java.net.MalformedURLException; 33 import java.net.URI; 34 import java.net.URISyntaxException; 35 import java.net.URL; 36 import java.nio.CharBuffer; 37 import java.nio.charset.Charset; 38 import java.nio.file.FileSystem; 39 import java.nio.file.FileSystems; 40 import java.nio.file.FileVisitOption; 41 import java.nio.file.FileVisitResult; 42 import java.nio.file.Files; 43 import java.nio.file.InvalidPathException; 44 import java.nio.file.LinkOption; 45 import java.nio.file.Path; 46 import java.nio.file.Paths; 47 import java.nio.file.ProviderNotFoundException; 48 import java.nio.file.SimpleFileVisitor; 49 import java.nio.file.attribute.BasicFileAttributes; 50 import java.nio.file.spi.FileSystemProvider; 51 import java.util.ArrayList; 62 import java.util.Set; 63 import java.util.stream.Collectors; 64 import java.util.stream.Stream; 65 66 import javax.lang.model.SourceVersion; 67 import javax.tools.FileObject; 68 import javax.tools.JavaFileManager; 69 import javax.tools.JavaFileObject; 70 import javax.tools.StandardJavaFileManager; 71 72 import com.sun.tools.javac.file.RelativePath.RelativeDirectory; 73 import com.sun.tools.javac.file.RelativePath.RelativeFile; 74 import com.sun.tools.javac.resources.CompilerProperties.Errors; 75 import com.sun.tools.javac.util.Assert; 76 import com.sun.tools.javac.util.Context; 77 import com.sun.tools.javac.util.Context.Factory; 78 import com.sun.tools.javac.util.DefinedBy; 79 import com.sun.tools.javac.util.DefinedBy.Api; 80 import com.sun.tools.javac.util.List; 81 import com.sun.tools.javac.util.ListBuffer; 82 83 import static java.nio.file.FileVisitOption.FOLLOW_LINKS; 84 85 import static javax.tools.StandardLocation.*; 86 87 /** 88 * This class provides access to the source, class and other files 89 * used by the compiler and related tools. 90 * 91 * <p><b>This is NOT part of any supported API. 92 * If you write code that depends on this, you do so at your own risk. 93 * This code and its internal interfaces are subject to change or 94 * deletion without notice.</b> 95 */ 96 public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager { 97 98 public static char[] toArray(CharBuffer buffer) { 99 if (buffer.hasArray()) 100 return buffer.compact().flip().array(); 101 else 960 return locations.getOutputLocation(CLASS_OUTPUT); 961 } 962 963 private Path getSourceOutDir() { 964 return locations.getOutputLocation(SOURCE_OUTPUT); 965 } 966 967 @Override @DefinedBy(Api.COMPILER) 968 public Location getLocationForModule(Location location, String moduleName) throws IOException { 969 checkModuleOrientedOrOutputLocation(location); 970 nullCheck(moduleName); 971 if (location == SOURCE_OUTPUT && getSourceOutDir() == null) 972 location = CLASS_OUTPUT; 973 return locations.getLocationForModule(location, moduleName); 974 } 975 976 @Override @DefinedBy(Api.COMPILER) 977 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException { 978 nullCheck(location); 979 nullCheck(service); 980 getClass().getModule().addUses(service); 981 if (location.isModuleOrientedLocation()) { 982 Collection<Path> paths = locations.getLocation(location); 983 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()])); 984 ModuleLayer bootLayer = ModuleLayer.boot(); 985 Configuration cf = bootLayer.configuration().resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet()); 986 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader()); 987 return ServiceLoader.load(layer, service); 988 } else { 989 return ServiceLoader.load(service, getClassLoader(location)); 990 } 991 } 992 993 @Override @DefinedBy(Api.COMPILER) 994 public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException { 995 checkModuleOrientedOrOutputLocation(location); 996 if (!(fo instanceof PathFileObject)) 997 return null; 998 Path p = Locations.normalize(((PathFileObject) fo).path); 999 // need to find p in location 1000 return locations.getLocationForModule(location, p); 1001 } 1002 1003 @Override @DefinedBy(Api.COMPILER) 1004 public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths) 1005 throws IOException { 1006 nullCheck(location); 1007 checkModuleOrientedOrOutputLocation(location); |