src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SmartFileManager.java

Print this page
rev 2819 : imported patch my-classpath-deps-00


  74         visibleSources = s;
  75     }
  76 
  77     public void cleanArtifacts() {
  78         packageArtifacts = new HashMap<>();
  79     }
  80 
  81     public void setLog(PrintWriter pw) {
  82         stdout = pw;
  83     }
  84 
  85     /**
  86      * Set whether or not to use ct.sym as an alternate to rt.jar.
  87      */
  88     public void setSymbolFileEnabled(boolean b) {
  89         if (!(fileManager instanceof JavacFileManager))
  90             throw new IllegalStateException();
  91         ((JavacFileManager) fileManager).setSymbolFileEnabled(b);
  92     }
  93 






  94     public Map<String,Set<URI>> getPackageArtifacts() {
  95         return packageArtifacts;
  96     }
  97 
  98     @Override @DefinedBy(Api.COMPILER)
  99     public Iterable<JavaFileObject> list(Location location,
 100                                          String packageName,
 101                                          Set<Kind> kinds,
 102                                          boolean recurse) throws IOException {

 103         // Acquire the list of files.
 104         Iterable<JavaFileObject> files = super.list(location, packageName, kinds, recurse);
 105         if (visibleSources.isEmpty()) {
 106             return files;
 107         }
 108         // Now filter!
 109         ListBuffer<JavaFileObject> filteredFiles = new ListBuffer<>();
 110         for (JavaFileObject f : files) {
 111             URI uri = f.toUri();
 112             String t = uri.toString();
 113             if (t.startsWith("jar:")
 114                 || t.endsWith(".class")
 115                 || visibleSources.contains(uri)) {
 116                 filteredFiles.add(f);
 117             }
 118         }
 119         return filteredFiles;
 120     }
 121 
 122     @Override @DefinedBy(Api.COMPILER)
 123     public boolean hasLocation(Location location) {
 124         return super.hasLocation(location);
 125     }
 126 
 127     @Override @DefinedBy(Api.COMPILER)
 128     public JavaFileObject getJavaFileForInput(Location location,
 129                                               String className,
 130                                               Kind kind) throws IOException {
 131         JavaFileObject file = super.getJavaFileForInput(location, className, kind);

 132         if (file == null || visibleSources.isEmpty()) {
 133             return file;
 134         }
 135 
 136         if (visibleSources.contains(file.toUri())) {
 137             return file;
 138         }
 139         return null;
 140     }
 141 
 142     @Override @DefinedBy(Api.COMPILER)
 143     public JavaFileObject getJavaFileForOutput(Location location,
 144                                                String className,
 145                                                Kind kind,
 146                                                FileObject sibling) throws IOException {
 147         JavaFileObject file = super.getJavaFileForOutput(location, className, kind, sibling);

 148         if (file == null) return file;
 149         int dp = className.lastIndexOf('.');
 150         String pkg_name = "";
 151         if (dp != -1) {
 152             pkg_name = className.substring(0, dp);
 153         }
 154         // When modules are in use, then the mod_name might be something like "jdk_base"
 155         String mod_name = "";
 156         addArtifact(mod_name+":"+pkg_name, file.toUri());
 157         return file;
 158     }
 159 
 160     @Override @DefinedBy(Api.COMPILER)
 161     public FileObject getFileForInput(Location location,
 162                                       String packageName,
 163                                       String relativeName) throws IOException {
 164         FileObject file =  super.getFileForInput(location, packageName, relativeName);

 165         if (file == null || visibleSources.isEmpty()) {
 166             return file;
 167         }
 168 
 169         if (visibleSources.contains(file.toUri())) {
 170             return file;
 171         }
 172         return null;
 173     }
 174 
 175     @Override @DefinedBy(Api.COMPILER)
 176     public FileObject getFileForOutput(Location location,
 177                                        String packageName,
 178                                        String relativeName,
 179                                        FileObject sibling) throws IOException {
 180         FileObject file = super.getFileForOutput(location, packageName, relativeName, sibling);

 181         if (file == null) return file;
 182         if (location.equals(StandardLocation.NATIVE_HEADER_OUTPUT) &&
 183                 file instanceof JavaFileObject) {
 184            file = new SmartFileObject((JavaFileObject)file, stdout);
 185            packageName = ":" + packageNameFromFileName(relativeName);
 186         }
 187         if (packageName.equals("")) {
 188             packageName = ":";
 189         }
 190         addArtifact(packageName, file.toUri());
 191         return file;
 192     }
 193 
 194     private String packageNameFromFileName(String fn) {
 195         StringBuilder sb = new StringBuilder();
 196         int p = fn.indexOf('_'), pp = 0;
 197         while (p != -1) {
 198             if (sb.length() > 0) sb.append('.');
 199             sb.append(fn.substring(pp,p));
 200             if (p == fn.length()-1) break;
 201             pp = p+1;
 202             p = fn.indexOf('_',pp);
 203         }
 204         return sb.toString();
 205     }
 206 
 207     @Override @DefinedBy(Api.COMPILER)
 208     public void flush() throws IOException {
 209         super.flush();
 210     }
 211 
 212     @Override @DefinedBy(Api.COMPILER)
 213     public void close() throws IOException {
 214         super.close();
 215     }
 216 
 217     void addArtifact(String pkgName, URI art) {
 218         Set<URI> s = packageArtifacts.get(pkgName);
 219         if (s == null) {
 220             s = new HashSet<>();
 221             packageArtifacts.put(pkgName, s);
 222         }
 223         s.add(art);






































 224     }
 225 }


  74         visibleSources = s;
  75     }
  76 
  77     public void cleanArtifacts() {
  78         packageArtifacts = new HashMap<>();
  79     }
  80 
  81     public void setLog(PrintWriter pw) {
  82         stdout = pw;
  83     }
  84 
  85     /**
  86      * Set whether or not to use ct.sym as an alternate to rt.jar.
  87      */
  88     public void setSymbolFileEnabled(boolean b) {
  89         if (!(fileManager instanceof JavacFileManager))
  90             throw new IllegalStateException();
  91         ((JavacFileManager) fileManager).setSymbolFileEnabled(b);
  92     }
  93 
  94     @DefinedBy(Api.COMPILER)
  95     public String inferBinaryName(Location location, JavaFileObject file) {
  96         return super.inferBinaryName(location, locUnwrap(file));
  97     }
  98 
  99 
 100     public Map<String,Set<URI>> getPackageArtifacts() {
 101         return packageArtifacts;
 102     }
 103 
 104     @Override @DefinedBy(Api.COMPILER)
 105     public Iterable<JavaFileObject> list(Location location,
 106                                          String packageName,
 107                                          Set<Kind> kinds,
 108                                          boolean recurse) throws IOException {
 109         // TODO: Do this lazily by returning an iterable with a filtering Iterator
 110         // Acquire the list of files.
 111         Iterable<JavaFileObject> files = super.list(location, packageName, kinds, recurse);
 112         if (visibleSources.isEmpty()) {
 113             return locWrapMany(files, location);
 114         }
 115         // Now filter!
 116         ListBuffer<JavaFileObject> filteredFiles = new ListBuffer<>();
 117         for (JavaFileObject f : files) {
 118             URI uri = f.toUri();
 119             String t = uri.toString();
 120             if (t.startsWith("jar:")
 121                 || t.endsWith(".class")
 122                 || visibleSources.contains(uri)) {
 123                 filteredFiles.add(f);
 124             }
 125         }


 126 
 127         return locWrapMany(filteredFiles, location);


 128     }
 129 
 130     @Override @DefinedBy(Api.COMPILER)
 131     public JavaFileObject getJavaFileForInput(Location location,
 132                                               String className,
 133                                               Kind kind) throws IOException {
 134         JavaFileObject file = super.getJavaFileForInput(location, className, kind);
 135         file = locWrap(file, location);
 136         if (file == null || visibleSources.isEmpty()) {
 137             return file;
 138         }
 139 
 140         if (visibleSources.contains(file.toUri())) {
 141             return file;
 142         }
 143         return null;
 144     }
 145 
 146     @Override @DefinedBy(Api.COMPILER)
 147     public JavaFileObject getJavaFileForOutput(Location location,
 148                                                String className,
 149                                                Kind kind,
 150                                                FileObject sibling) throws IOException {
 151         JavaFileObject file = super.getJavaFileForOutput(location, className, kind, sibling);
 152         file = locWrap(file, location);
 153         if (file == null) return file;
 154         int dp = className.lastIndexOf('.');
 155         String pkg_name = "";
 156         if (dp != -1) {
 157             pkg_name = className.substring(0, dp);
 158         }
 159         // When modules are in use, then the mod_name might be something like "jdk_base"
 160         String mod_name = "";
 161         addArtifact(mod_name+":"+pkg_name, file.toUri());
 162         return file;
 163     }
 164 
 165     @Override @DefinedBy(Api.COMPILER)
 166     public FileObject getFileForInput(Location location,
 167                                       String packageName,
 168                                       String relativeName) throws IOException {
 169         FileObject file =  super.getFileForInput(location, packageName, relativeName);
 170         file = locWrap(file, location);
 171         if (file == null || visibleSources.isEmpty()) {
 172             return file;
 173         }
 174 
 175         if (visibleSources.contains(file.toUri())) {
 176             return file;
 177         }
 178         return null;
 179     }
 180 
 181     @Override @DefinedBy(Api.COMPILER)
 182     public FileObject getFileForOutput(Location location,
 183                                        String packageName,
 184                                        String relativeName,
 185                                        FileObject sibling) throws IOException {
 186         FileObject superFile = super.getFileForOutput(location, packageName, relativeName, sibling);
 187         FileObject file = locWrap(superFile, location);
 188         if (file == null) return file;
 189 
 190         if (location.equals(StandardLocation.NATIVE_HEADER_OUTPUT) && superFile instanceof JavaFileObject) {
 191            file = new SmartFileObject((JavaFileObject) file, stdout);
 192            packageName = ":" + packageNameFromFileName(relativeName);
 193         }
 194         if (packageName.equals("")) {
 195             packageName = ":";
 196         }
 197         addArtifact(packageName, file.toUri());
 198         return file;
 199     }
 200 
 201     private static String packageNameFromFileName(String fn) {
 202         StringBuilder sb = new StringBuilder();
 203         int p = fn.indexOf('_'), pp = 0;
 204         while (p != -1) {
 205             if (sb.length() > 0) sb.append('.');
 206             sb.append(fn.substring(pp,p));
 207             if (p == fn.length()-1) break;
 208             pp = p+1;
 209             p = fn.indexOf('_',pp);
 210         }
 211         return sb.toString();
 212     }
 213 










 214     void addArtifact(String pkgName, URI art) {
 215         Set<URI> s = packageArtifacts.get(pkgName);
 216         if (s == null) {
 217             s = new HashSet<>();
 218             packageArtifacts.put(pkgName, s);
 219         }
 220         s.add(art);
 221     }
 222 
 223     private static JavaFileObject locWrap(JavaFileObject jfo, Location loc) {
 224         return jfo == null ? null : new JavaFileObjectWithLocation<>(jfo, loc);
 225     }
 226 
 227     private static FileObject locWrap(FileObject fo, Location loc) {
 228         if (fo instanceof JavaFileObject)
 229             return locWrap((JavaFileObject) fo, loc);
 230         return fo == null ? null : new FileObjectWithLocation<>(fo, loc);
 231     }
 232 
 233     @DefinedBy(Api.COMPILER)
 234     @Override
 235     public boolean isSameFile(FileObject a, FileObject b) {
 236         return super.isSameFile(locUnwrap(a), locUnwrap(b));
 237     }
 238 
 239     private static ListBuffer<JavaFileObject> locWrapMany(Iterable<JavaFileObject> jfos,
 240                                                           Location loc) {
 241         ListBuffer<JavaFileObject> locWrapped = new ListBuffer<>();
 242         for (JavaFileObject f : jfos)
 243             locWrapped.add(locWrap(f, loc));
 244         return locWrapped;
 245     }
 246 
 247     private static FileObject locUnwrap(FileObject fo) {
 248         if (fo instanceof FileObjectWithLocation<?>)
 249             return ((FileObjectWithLocation<?>) fo).getDelegate();
 250         if (fo instanceof JavaFileObjectWithLocation<?>)
 251             return ((JavaFileObjectWithLocation<?>) fo).getDelegate();
 252         return fo;
 253     }
 254 
 255     private static JavaFileObject locUnwrap(JavaFileObject fo) {
 256         if (fo instanceof JavaFileObjectWithLocation<?>)
 257             return ((JavaFileObjectWithLocation<?>) fo).getDelegate();
 258         return fo;
 259     }
 260 }