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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

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

mercurial