963 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
964 nullCheck(location);
965 nullCheck(service);
966 Module.getModule(getClass()).addUses(service);
967 if (location.isModuleOrientedLocation()) {
968 Collection<Path> paths = locations.getLocation(location);
969 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
970 Layer bootLayer = Layer.boot();
971 Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.of(), finder, Collections.emptySet());
972 Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
973 return ServiceLoaderHelper.load(layer, service);
974 } else {
975 return ServiceLoader.load(service, getClassLoader(location));
976 }
977 }
978
979 @Override @DefinedBy(Api.COMPILER)
980 public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
981 checkModuleOrientedOrOutputLocation(location);
982 if (!(fo instanceof PathFileObject))
983 throw new IllegalArgumentException(fo.getName());
984 int depth = 1; // allow 1 for filename
985 if (pkgName != null && !pkgName.isEmpty()) {
986 depth += 1;
987 for (int i = 0; i < pkgName.length(); i++) {
988 switch (pkgName.charAt(i)) {
989 case '/': case '.':
990 depth++;
991 }
992 }
993 }
994 Path p = Locations.normalize(((PathFileObject) fo).path);
995 int fc = p.getNameCount();
996 if (depth < fc) {
997 Path root = p.getRoot();
998 Path subpath = p.subpath(0, fc - depth);
999 Path dir = (root == null) ? subpath : root.resolve(subpath);
1000 // need to find dir in location
1001 return locations.getLocationForModule(location, dir);
1002 } else {
1003 return null;
|
963 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
964 nullCheck(location);
965 nullCheck(service);
966 Module.getModule(getClass()).addUses(service);
967 if (location.isModuleOrientedLocation()) {
968 Collection<Path> paths = locations.getLocation(location);
969 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
970 Layer bootLayer = Layer.boot();
971 Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.of(), finder, Collections.emptySet());
972 Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
973 return ServiceLoaderHelper.load(layer, service);
974 } else {
975 return ServiceLoader.load(service, getClassLoader(location));
976 }
977 }
978
979 @Override @DefinedBy(Api.COMPILER)
980 public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
981 checkModuleOrientedOrOutputLocation(location);
982 if (!(fo instanceof PathFileObject))
983 return null;
984 int depth = 1; // allow 1 for filename
985 if (pkgName != null && !pkgName.isEmpty()) {
986 depth += 1;
987 for (int i = 0; i < pkgName.length(); i++) {
988 switch (pkgName.charAt(i)) {
989 case '/': case '.':
990 depth++;
991 }
992 }
993 }
994 Path p = Locations.normalize(((PathFileObject) fo).path);
995 int fc = p.getNameCount();
996 if (depth < fc) {
997 Path root = p.getRoot();
998 Path subpath = p.subpath(0, fc - depth);
999 Path dir = (root == null) ? subpath : root.resolve(subpath);
1000 // need to find dir in location
1001 return locations.getLocationForModule(location, dir);
1002 } else {
1003 return null;
|