54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.Comparator;
57 import java.util.HashMap;
58 import java.util.Iterator;
59 import java.util.Map;
60 import java.util.Objects;
61 import java.util.ServiceLoader;
62 import java.util.Set;
63 import java.util.stream.Stream;
64 import java.util.zip.ZipException;
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.main.Option;
75 import com.sun.tools.javac.resources.CompilerProperties.Errors;
76 import com.sun.tools.javac.util.Assert;
77 import com.sun.tools.javac.util.Context;
78 import com.sun.tools.javac.util.Context.Factory;
79 import com.sun.tools.javac.util.DefinedBy;
80 import com.sun.tools.javac.util.DefinedBy.Api;
81 import com.sun.tools.javac.util.List;
82 import com.sun.tools.javac.util.ListBuffer;
83 import com.sun.tools.javac.util.Options;
84
85 import static java.nio.charset.StandardCharsets.US_ASCII;
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 *
1149 }
1150
1151 @Override @DefinedBy(Api.COMPILER)
1152 public Location getLocationForModule(Location location, String moduleName) throws IOException {
1153 checkModuleOrientedOrOutputLocation(location);
1154 nullCheck(moduleName);
1155 if (location == SOURCE_OUTPUT && getSourceOutDir() == null)
1156 location = CLASS_OUTPUT;
1157 return locations.getLocationForModule(location, moduleName);
1158 }
1159
1160 @Override @DefinedBy(Api.COMPILER)
1161 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
1162 nullCheck(location);
1163 nullCheck(service);
1164 getClass().getModule().addUses(service);
1165 if (location.isModuleOrientedLocation()) {
1166 Collection<Path> paths = locations.getLocation(location);
1167 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
1168 ModuleLayer bootLayer = ModuleLayer.boot();
1169 Configuration cf = bootLayer.configuration().resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet());
1170 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
1171 return ServiceLoader.load(layer, service);
1172 } else {
1173 return ServiceLoader.load(service, getClassLoader(location));
1174 }
1175 }
1176
1177 @Override @DefinedBy(Api.COMPILER)
1178 public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
1179 checkModuleOrientedOrOutputLocation(location);
1180 if (!(fo instanceof PathFileObject pathFileObject))
1181 return null;
1182 Path p = Locations.normalize(pathFileObject.path);
1183 // need to find p in location
1184 return locations.getLocationForModule(location, p);
1185 }
1186
1187 @Override @DefinedBy(Api.COMPILER)
1188 public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths)
1189 throws IOException {
1190 nullCheck(location);
|
54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.Comparator;
57 import java.util.HashMap;
58 import java.util.Iterator;
59 import java.util.Map;
60 import java.util.Objects;
61 import java.util.ServiceLoader;
62 import java.util.Set;
63 import java.util.stream.Stream;
64 import java.util.zip.ZipException;
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.main.JavaCompiler;
75 import com.sun.tools.javac.main.JavaCompiler.CodeReflectionSupport;
76 import com.sun.tools.javac.main.Option;
77 import com.sun.tools.javac.resources.CompilerProperties.Errors;
78 import com.sun.tools.javac.util.Assert;
79 import com.sun.tools.javac.util.Context;
80 import com.sun.tools.javac.util.Context.Factory;
81 import com.sun.tools.javac.util.DefinedBy;
82 import com.sun.tools.javac.util.DefinedBy.Api;
83 import com.sun.tools.javac.util.List;
84 import com.sun.tools.javac.util.ListBuffer;
85 import com.sun.tools.javac.util.Options;
86
87 import static java.nio.charset.StandardCharsets.US_ASCII;
88 import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
89
90 import static javax.tools.StandardLocation.*;
91
92 /**
93 * This class provides access to the source, class and other files
94 * used by the compiler and related tools.
95 *
1151 }
1152
1153 @Override @DefinedBy(Api.COMPILER)
1154 public Location getLocationForModule(Location location, String moduleName) throws IOException {
1155 checkModuleOrientedOrOutputLocation(location);
1156 nullCheck(moduleName);
1157 if (location == SOURCE_OUTPUT && getSourceOutDir() == null)
1158 location = CLASS_OUTPUT;
1159 return locations.getLocationForModule(location, moduleName);
1160 }
1161
1162 @Override @DefinedBy(Api.COMPILER)
1163 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
1164 nullCheck(location);
1165 nullCheck(service);
1166 getClass().getModule().addUses(service);
1167 if (location.isModuleOrientedLocation()) {
1168 Collection<Path> paths = locations.getLocation(location);
1169 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
1170 ModuleLayer bootLayer = ModuleLayer.boot();
1171 ModuleLayer augmentedModuleLayer;
1172 ClassLoader parentCL;
1173 if (CodeReflectionSupport.CODE_LAYER != null) {
1174 // create a layer whose parent is Babylon's code layer
1175 augmentedModuleLayer = CodeReflectionSupport.CODE_LAYER;
1176 parentCL = CodeReflectionSupport.CODE_LAYER.findLoader("jdk.incubator.code");
1177 } else {
1178 augmentedModuleLayer = bootLayer;
1179 parentCL = ClassLoader.getSystemClassLoader();
1180 }
1181 Configuration cf = augmentedModuleLayer.configuration()
1182 .resolveAndBind(ModuleFinder.of(), finder, Collections.emptySet());
1183 ModuleLayer layer = augmentedModuleLayer.defineModulesWithOneLoader(cf, parentCL);
1184 return ServiceLoader.load(layer, service);
1185 } else {
1186 return ServiceLoader.load(service, getClassLoader(location));
1187 }
1188 }
1189
1190 @Override @DefinedBy(Api.COMPILER)
1191 public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
1192 checkModuleOrientedOrOutputLocation(location);
1193 if (!(fo instanceof PathFileObject pathFileObject))
1194 return null;
1195 Path p = Locations.normalize(pathFileObject.path);
1196 // need to find p in location
1197 return locations.getLocationForModule(location, p);
1198 }
1199
1200 @Override @DefinedBy(Api.COMPILER)
1201 public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths)
1202 throws IOException {
1203 nullCheck(location);
|