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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/comp/SmartFileManager.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,221 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.sjavac.comp;
    1.30 +
    1.31 +import com.sun.tools.javac.util.ListBuffer;
    1.32 +import java.io.IOException;
    1.33 +import java.io.PrintWriter;
    1.34 +import java.net.URI;
    1.35 +import java.util.Set;
    1.36 +import java.util.HashSet;
    1.37 +import java.util.Map;
    1.38 +import java.util.HashMap;
    1.39 +import javax.tools.*;
    1.40 +import javax.tools.JavaFileObject.Kind;
    1.41 +
    1.42 +/**
    1.43 + * Intercepts reads and writes to the file system to gather
    1.44 + * information about what artifacts are generated.
    1.45 + *
    1.46 + * Traps writes to certain files, if the content written is identical
    1.47 + * to the existing file.
    1.48 + *
    1.49 + * Can also blind out the filemanager from seeing certain files in the file system.
    1.50 + * Necessary to prevent javac from seeing some sources where the source path points.
    1.51 + *
    1.52 + * <p><b>This is NOT part of any supported API.
    1.53 + * If you write code that depends on this, you do so at your own
    1.54 + * risk.  This code and its internal interfaces are subject to change
    1.55 + * or deletion without notice.</b></p>
    1.56 + */
    1.57 +public class SmartFileManager extends ForwardingJavaFileManager<JavaFileManager> {
    1.58 +
    1.59 +    // Set of sources that can be seen by javac.
    1.60 +    Set<URI> visibleSources = new HashSet<URI>();
    1.61 +    // Map from modulename:packagename to artifacts.
    1.62 +    Map<String,Set<URI>> packageArtifacts = new HashMap<String,Set<URI>>();
    1.63 +    // Where to print informational messages.
    1.64 +    PrintWriter stdout;
    1.65 +
    1.66 +    public SmartFileManager(JavaFileManager fileManager) {
    1.67 +        super(fileManager);
    1.68 +    }
    1.69 +
    1.70 +    public void setVisibleSources(Set<URI> s) {
    1.71 +        visibleSources = s;
    1.72 +    }
    1.73 +
    1.74 +    public void cleanArtifacts() {
    1.75 +        packageArtifacts = new HashMap<String,Set<URI>>();
    1.76 +    }
    1.77 +
    1.78 +    public void setLog(PrintWriter pw) {
    1.79 +        stdout = pw;
    1.80 +    }
    1.81 +
    1.82 +    public Map<String,Set<URI>> getPackageArtifacts() {
    1.83 +        return packageArtifacts;
    1.84 +    }
    1.85 +
    1.86 +    @Override
    1.87 +    public Iterable<JavaFileObject> list(Location location,
    1.88 +                                         String packageName,
    1.89 +                                         Set<Kind> kinds,
    1.90 +                                         boolean recurse)
    1.91 +        throws IOException
    1.92 +    {
    1.93 +        // Acquire the list of files.
    1.94 +        Iterable<JavaFileObject> files = super.list(location, packageName, kinds, recurse);
    1.95 +        if (visibleSources.isEmpty()) {
    1.96 +            return files;
    1.97 +        }
    1.98 +        // Now filter!
    1.99 +        ListBuffer<JavaFileObject> filteredFiles = new ListBuffer<JavaFileObject>();
   1.100 +        for (JavaFileObject f : files) {
   1.101 +            URI uri = f.toUri();
   1.102 +            String t = uri.toString();
   1.103 +            if (t.startsWith("jar:")
   1.104 +                || t.endsWith(".class")
   1.105 +                || visibleSources.contains(uri))
   1.106 +            {
   1.107 +                filteredFiles.add(f);
   1.108 +            }
   1.109 +        }
   1.110 +        return filteredFiles;
   1.111 +    }
   1.112 +
   1.113 +    @Override
   1.114 +    public boolean hasLocation(Location location) {
   1.115 +        return super.hasLocation(location);
   1.116 +    }
   1.117 +
   1.118 +    @Override
   1.119 +    public JavaFileObject getJavaFileForInput(Location location,
   1.120 +                                              String className,
   1.121 +                                              Kind kind)
   1.122 +        throws IOException
   1.123 +    {
   1.124 +        JavaFileObject file = super.getJavaFileForInput(location, className, kind);
   1.125 +        if (file == null || visibleSources.isEmpty()) {
   1.126 +            return file;
   1.127 +        }
   1.128 +
   1.129 +        if (visibleSources.contains(file.toUri())) {
   1.130 +            return file;
   1.131 +        }
   1.132 +        return null;
   1.133 +    }
   1.134 +
   1.135 +    @Override
   1.136 +    public JavaFileObject getJavaFileForOutput(Location location,
   1.137 +                                               String className,
   1.138 +                                               Kind kind,
   1.139 +                                               FileObject sibling)
   1.140 +        throws IOException
   1.141 +    {
   1.142 +        JavaFileObject file = super.getJavaFileForOutput(location, className, kind, sibling);
   1.143 +        if (file == null) return file;
   1.144 +        int dp = className.lastIndexOf('.');
   1.145 +        String pkg_name = "";
   1.146 +        if (dp != -1) {
   1.147 +            pkg_name = className.substring(0, dp);
   1.148 +        }
   1.149 +        // When modules are in use, then the mod_name might be something like "jdk_base"
   1.150 +        String mod_name = "";
   1.151 +        addArtifact(mod_name+":"+pkg_name, file.toUri());
   1.152 +        return file;
   1.153 +    }
   1.154 +
   1.155 +    @Override
   1.156 +    public FileObject getFileForInput(Location location,
   1.157 +                                      String packageName,
   1.158 +                                      String relativeName)
   1.159 +        throws IOException
   1.160 +    {
   1.161 +        FileObject file =  super.getFileForInput(location, packageName, relativeName);
   1.162 +        if (file == null || visibleSources.isEmpty()) {
   1.163 +            return file;
   1.164 +        }
   1.165 +
   1.166 +        if (visibleSources.contains(file.toUri())) {
   1.167 +            return file;
   1.168 +        }
   1.169 +        return null;
   1.170 +    }
   1.171 +
   1.172 +    @Override
   1.173 +    public FileObject getFileForOutput(Location location,
   1.174 +                                       String packageName,
   1.175 +                                       String relativeName,
   1.176 +                                       FileObject sibling)
   1.177 +        throws IOException
   1.178 +    {
   1.179 +        FileObject file = super.getFileForOutput(location, packageName, relativeName, sibling);
   1.180 +        if (file == null) return file;
   1.181 +        if (location.equals(StandardLocation.NATIVE_HEADER_OUTPUT) &&
   1.182 +                file instanceof JavaFileObject) {
   1.183 +           file = new SmartFileObject((JavaFileObject)file, stdout);
   1.184 +           packageName = ":" + packageNameFromFileName(relativeName);
   1.185 +        }
   1.186 +        if (packageName.equals("")) {
   1.187 +            packageName = ":";
   1.188 +        }
   1.189 +        addArtifact(packageName, file.toUri());
   1.190 +        return file;
   1.191 +    }
   1.192 +
   1.193 +    private String packageNameFromFileName(String fn) {
   1.194 +        StringBuilder sb = new StringBuilder();
   1.195 +        int p = fn.indexOf('_'), pp = 0;
   1.196 +        while (p != -1) {
   1.197 +            if (sb.length() > 0) sb.append('.');
   1.198 +            sb.append(fn.substring(pp,p));
   1.199 +            if (p == fn.length()-1) break;
   1.200 +            pp = p+1;
   1.201 +            p = fn.indexOf('_',pp);
   1.202 +        }
   1.203 +        return sb.toString();
   1.204 +    }
   1.205 +
   1.206 +    @Override
   1.207 +    public void flush() throws IOException {
   1.208 +        super.flush();
   1.209 +    }
   1.210 +
   1.211 +    @Override
   1.212 +    public void close() throws IOException {
   1.213 +        super.close();
   1.214 +    }
   1.215 +
   1.216 +    void addArtifact(String pkgName, URI art) {
   1.217 +        Set<URI> s = packageArtifacts.get(pkgName);
   1.218 +        if (s == null) {
   1.219 +            s = new HashSet<URI>();
   1.220 +            packageArtifacts.put(pkgName, s);
   1.221 +        }
   1.222 +        s.add(art);
   1.223 +    }
   1.224 +}

mercurial