57 import java.util.Map;
58 import java.util.Objects;
59 import java.util.ServiceLoader;
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.util.Context;
73 import com.sun.tools.javac.util.DefinedBy;
74 import com.sun.tools.javac.util.DefinedBy.Api;
75 import com.sun.tools.javac.util.List;
76 import com.sun.tools.javac.util.ListBuffer;
77 import com.sun.tools.javac.util.ModuleWrappers.Configuration;
78 import com.sun.tools.javac.util.ModuleWrappers.Layer;
79 import com.sun.tools.javac.util.ModuleWrappers.ModuleFinder;
80 import com.sun.tools.javac.util.ModuleWrappers.ServiceLoaderHelper;
81
82 import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
83
84 import static javax.tools.StandardLocation.*;
85
86 /**
87 * This class provides access to the source, class and other files
88 * used by the compiler and related tools.
89 *
90 * <p><b>This is NOT part of any supported API.
91 * If you write code that depends on this, you do so at your own risk.
92 * This code and its internal interfaces are subject to change or
93 * deletion without notice.</b>
94 */
95 public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager {
96
97 @SuppressWarnings("cast")
98 public static char[] toArray(CharBuffer buffer) {
99 if (buffer.hasArray())
100 return ((CharBuffer)buffer.compact().flip()).array();
955
956 private Path getSourceOutDir() {
957 return locations.getOutputLocation(SOURCE_OUTPUT);
958 }
959
960 @Override @DefinedBy(Api.COMPILER)
961 public Location getModuleLocation(Location location, String moduleName) throws IOException {
962 nullCheck(location);
963 nullCheck(moduleName);
964 return locations.getModuleLocation(location, moduleName);
965 }
966
967 @Override @DefinedBy(Api.COMPILER)
968 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
969 nullCheck(location);
970 nullCheck(service);
971 if (location.isModuleLocation()) {
972 Collection<Path> paths = locations.getLocation(location);
973 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
974 Layer bootLayer = Layer.boot();
975 Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.empty(), finder, Collections.emptySet());
976 Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
977 return ServiceLoaderHelper.load(layer, service);
978 } else {
979 return ServiceLoader.load(service, getClassLoader(location));
980 }
981 }
982
983 @Override @DefinedBy(Api.COMPILER)
984 public Location getModuleLocation(Location location, JavaFileObject fo, String pkgName) throws IOException {
985 nullCheck(location);
986 if (!(fo instanceof PathFileObject))
987 throw new IllegalArgumentException(fo.getName());
988 int depth = 1; // allow 1 for filename
989 if (pkgName != null && !pkgName.isEmpty()) {
990 depth += 1;
991 for (int i = 0; i < pkgName.length(); i++) {
992 switch (pkgName.charAt(i)) {
993 case '/': case '.':
994 depth++;
995 }
|
57 import java.util.Map;
58 import java.util.Objects;
59 import java.util.ServiceLoader;
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.util.Context;
73 import com.sun.tools.javac.util.DefinedBy;
74 import com.sun.tools.javac.util.DefinedBy.Api;
75 import com.sun.tools.javac.util.List;
76 import com.sun.tools.javac.util.ListBuffer;
77 import com.sun.tools.javac.util.JDK9Wrappers.Configuration;
78 import com.sun.tools.javac.util.JDK9Wrappers.Layer;
79 import com.sun.tools.javac.util.JDK9Wrappers.ModuleFinder;
80 import com.sun.tools.javac.util.JDK9Wrappers.ServiceLoaderHelper;
81
82 import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
83
84 import static javax.tools.StandardLocation.*;
85
86 /**
87 * This class provides access to the source, class and other files
88 * used by the compiler and related tools.
89 *
90 * <p><b>This is NOT part of any supported API.
91 * If you write code that depends on this, you do so at your own risk.
92 * This code and its internal interfaces are subject to change or
93 * deletion without notice.</b>
94 */
95 public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager {
96
97 @SuppressWarnings("cast")
98 public static char[] toArray(CharBuffer buffer) {
99 if (buffer.hasArray())
100 return ((CharBuffer)buffer.compact().flip()).array();
955
956 private Path getSourceOutDir() {
957 return locations.getOutputLocation(SOURCE_OUTPUT);
958 }
959
960 @Override @DefinedBy(Api.COMPILER)
961 public Location getModuleLocation(Location location, String moduleName) throws IOException {
962 nullCheck(location);
963 nullCheck(moduleName);
964 return locations.getModuleLocation(location, moduleName);
965 }
966
967 @Override @DefinedBy(Api.COMPILER)
968 public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
969 nullCheck(location);
970 nullCheck(service);
971 if (location.isModuleLocation()) {
972 Collection<Path> paths = locations.getLocation(location);
973 ModuleFinder finder = ModuleFinder.of(paths.toArray(new Path[paths.size()]));
974 Layer bootLayer = Layer.boot();
975 Configuration cf = bootLayer.configuration().resolveRequiresAndUses(ModuleFinder.of(), finder, Collections.emptySet());
976 Layer layer = bootLayer.defineModulesWithOneLoader(cf, ClassLoader.getSystemClassLoader());
977 return ServiceLoaderHelper.load(layer, service);
978 } else {
979 return ServiceLoader.load(service, getClassLoader(location));
980 }
981 }
982
983 @Override @DefinedBy(Api.COMPILER)
984 public Location getModuleLocation(Location location, JavaFileObject fo, String pkgName) throws IOException {
985 nullCheck(location);
986 if (!(fo instanceof PathFileObject))
987 throw new IllegalArgumentException(fo.getName());
988 int depth = 1; // allow 1 for filename
989 if (pkgName != null && !pkgName.isEmpty()) {
990 depth += 1;
991 for (int i = 0; i < pkgName.length(); i++) {
992 switch (pkgName.charAt(i)) {
993 case '/': case '.':
994 depth++;
995 }
|