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