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

Mon, 11 Mar 2013 19:03:35 -0700

author
ohrstrom
date
Mon, 11 Mar 2013 19:03:35 -0700
changeset 1625
fbb6e470079d
parent 1504
22e417cdddee
child 2227
998b10c43157
permissions
-rw-r--r--

8009843: sjavac should accept -cp as synonym for -classpath
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    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  */
    26 package com.sun.tools.sjavac.comp;
    28 import com.sun.tools.javac.util.ListBuffer;
    29 import java.io.IOException;
    30 import java.io.PrintWriter;
    31 import java.net.URI;
    32 import java.util.Set;
    33 import java.util.HashSet;
    34 import java.util.Map;
    35 import java.util.HashMap;
    36 import javax.tools.*;
    37 import javax.tools.JavaFileObject.Kind;
    39 /**
    40  * Intercepts reads and writes to the file system to gather
    41  * information about what artifacts are generated.
    42  *
    43  * Traps writes to certain files, if the content written is identical
    44  * to the existing file.
    45  *
    46  * Can also blind out the filemanager from seeing certain files in the file system.
    47  * Necessary to prevent javac from seeing some sources where the source path points.
    48  *
    49  * <p><b>This is NOT part of any supported API.
    50  * If you write code that depends on this, you do so at your own
    51  * risk.  This code and its internal interfaces are subject to change
    52  * or deletion without notice.</b></p>
    53  */
    54 public class SmartFileManager extends ForwardingJavaFileManager<JavaFileManager> {
    56     // Set of sources that can be seen by javac.
    57     Set<URI> visibleSources = new HashSet<URI>();
    58     // Map from modulename:packagename to artifacts.
    59     Map<String,Set<URI>> packageArtifacts = new HashMap<String,Set<URI>>();
    60     // Where to print informational messages.
    61     PrintWriter stdout;
    63     public SmartFileManager(JavaFileManager fileManager) {
    64         super(fileManager);
    65     }
    67     public void setVisibleSources(Set<URI> s) {
    68         visibleSources = s;
    69     }
    71     public void cleanArtifacts() {
    72         packageArtifacts = new HashMap<String,Set<URI>>();
    73     }
    75     public void setLog(PrintWriter pw) {
    76         stdout = pw;
    77     }
    79     public Map<String,Set<URI>> getPackageArtifacts() {
    80         return packageArtifacts;
    81     }
    83     @Override
    84     public Iterable<JavaFileObject> list(Location location,
    85                                          String packageName,
    86                                          Set<Kind> kinds,
    87                                          boolean recurse)
    88         throws IOException
    89     {
    90         // Acquire the list of files.
    91         Iterable<JavaFileObject> files = super.list(location, packageName, kinds, recurse);
    92         if (visibleSources.isEmpty()) {
    93             return files;
    94         }
    95         // Now filter!
    96         ListBuffer<JavaFileObject> filteredFiles = new ListBuffer<JavaFileObject>();
    97         for (JavaFileObject f : files) {
    98             URI uri = f.toUri();
    99             String t = uri.toString();
   100             if (t.startsWith("jar:")
   101                 || t.endsWith(".class")
   102                 || visibleSources.contains(uri))
   103             {
   104                 filteredFiles.add(f);
   105             }
   106         }
   107         return filteredFiles;
   108     }
   110     @Override
   111     public boolean hasLocation(Location location) {
   112         return super.hasLocation(location);
   113     }
   115     @Override
   116     public JavaFileObject getJavaFileForInput(Location location,
   117                                               String className,
   118                                               Kind kind)
   119         throws IOException
   120     {
   121         JavaFileObject file = super.getJavaFileForInput(location, className, kind);
   122         if (file == null || visibleSources.isEmpty()) {
   123             return file;
   124         }
   126         if (visibleSources.contains(file.toUri())) {
   127             return file;
   128         }
   129         return null;
   130     }
   132     @Override
   133     public JavaFileObject getJavaFileForOutput(Location location,
   134                                                String className,
   135                                                Kind kind,
   136                                                FileObject sibling)
   137         throws IOException
   138     {
   139         JavaFileObject file = super.getJavaFileForOutput(location, className, kind, sibling);
   140         if (file == null) return file;
   141         int dp = className.lastIndexOf('.');
   142         String pkg_name = "";
   143         if (dp != -1) {
   144             pkg_name = className.substring(0, dp);
   145         }
   146         // When modules are in use, then the mod_name might be something like "jdk_base"
   147         String mod_name = "";
   148         addArtifact(mod_name+":"+pkg_name, file.toUri());
   149         return file;
   150     }
   152     @Override
   153     public FileObject getFileForInput(Location location,
   154                                       String packageName,
   155                                       String relativeName)
   156         throws IOException
   157     {
   158         FileObject file =  super.getFileForInput(location, packageName, relativeName);
   159         if (file == null || visibleSources.isEmpty()) {
   160             return file;
   161         }
   163         if (visibleSources.contains(file.toUri())) {
   164             return file;
   165         }
   166         return null;
   167     }
   169     @Override
   170     public FileObject getFileForOutput(Location location,
   171                                        String packageName,
   172                                        String relativeName,
   173                                        FileObject sibling)
   174         throws IOException
   175     {
   176         FileObject file = super.getFileForOutput(location, packageName, relativeName, sibling);
   177         if (file == null) return file;
   178         if (location.equals(StandardLocation.NATIVE_HEADER_OUTPUT) &&
   179                 file instanceof JavaFileObject) {
   180            file = new SmartFileObject((JavaFileObject)file, stdout);
   181            packageName = ":" + packageNameFromFileName(relativeName);
   182         }
   183         if (packageName.equals("")) {
   184             packageName = ":";
   185         }
   186         addArtifact(packageName, file.toUri());
   187         return file;
   188     }
   190     private String packageNameFromFileName(String fn) {
   191         StringBuilder sb = new StringBuilder();
   192         int p = fn.indexOf('_'), pp = 0;
   193         while (p != -1) {
   194             if (sb.length() > 0) sb.append('.');
   195             sb.append(fn.substring(pp,p));
   196             if (p == fn.length()-1) break;
   197             pp = p+1;
   198             p = fn.indexOf('_',pp);
   199         }
   200         return sb.toString();
   201     }
   203     @Override
   204     public void flush() throws IOException {
   205         super.flush();
   206     }
   208     @Override
   209     public void close() throws IOException {
   210         super.close();
   211     }
   213     void addArtifact(String pkgName, URI art) {
   214         Set<URI> s = packageArtifacts.get(pkgName);
   215         if (s == null) {
   216             s = new HashSet<URI>();
   217             packageArtifacts.put(pkgName, s);
   218         }
   219         s.add(art);
   220     }
   221 }

mercurial