test/tools/javac/diags/FileManager.java

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 708
c8b4a1e76089
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

jjg@610 1 /*
jjg@610 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
jjg@610 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@610 4 *
jjg@610 5 * This code is free software; you can redistribute it and/or modify it
jjg@610 6 * under the terms of the GNU General Public License version 2 only, as
jjg@610 7 * published by the Free Software Foundation.
jjg@610 8 *
jjg@610 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@610 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@610 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@610 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@610 13 * accompanied this code).
jjg@610 14 *
jjg@610 15 * You should have received a copy of the GNU General Public License version
jjg@610 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@610 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@610 18 *
jjg@610 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@610 20 * or visit www.oracle.com if you need additional information or have any
jjg@610 21 * questions.
jjg@610 22 */
jjg@610 23
jjg@610 24 import java.io.File;
jjg@610 25 import java.io.IOException;
jjg@610 26 import java.io.InputStream;
jjg@610 27 import java.io.OutputStream;
jjg@610 28 import java.io.Reader;
jjg@610 29 import java.io.Writer;
jjg@610 30 import java.net.URI;
jjg@610 31 import java.util.ArrayList;
jjg@610 32 import java.util.Collections;
jjg@610 33 import java.util.List;
jjg@610 34 import java.util.regex.Pattern;
jjg@610 35 import javax.lang.model.element.Modifier;
jjg@610 36 import javax.lang.model.element.NestingKind;
jjg@610 37 import javax.tools.JavaFileManager.Location;
jjg@610 38 import javax.tools.JavaFileObject;
jjg@610 39 import javax.tools.StandardJavaFileManager;
jjg@610 40
jjg@610 41 import com.sun.tools.javac.api.WrappingJavaFileManager;
jjg@610 42
jjg@610 43 /**
jjg@610 44 * A JavaFileManager that can throw IOException on attempting to read or write
jjg@610 45 * selected files that match a regular expression.
jjg@610 46 */
jjg@610 47 public class FileManager
jjg@610 48 extends WrappingJavaFileManager<StandardJavaFileManager>
jjg@610 49 implements StandardJavaFileManager {
jjg@610 50 private static final String CANT_READ = "cantRead:";
jjg@610 51 private static final String CANT_WRITE = "cantWrite:";
jjg@610 52
jjg@610 53 private Pattern cantRead;
jjg@610 54 private Pattern cantWrite;
jjg@610 55
jjg@610 56 public FileManager(StandardJavaFileManager fm, List<String> opts) {
jjg@610 57 super(fm);
jjg@610 58 for (String opt: opts) {
jjg@610 59 if (opt.startsWith(CANT_READ))
jjg@610 60 cantRead = Pattern.compile(opt.substring(CANT_READ.length()));
jjg@610 61 else if (opt.startsWith(CANT_WRITE))
jjg@610 62 cantWrite = Pattern.compile(opt.substring(CANT_WRITE.length()));
jjg@610 63 else
jjg@610 64 throw new IllegalArgumentException(opt);
jjg@610 65 }
jjg@610 66 }
jjg@610 67
jjg@610 68 @Override
jjg@610 69 protected JavaFileObject wrap(JavaFileObject fo) {
jjg@610 70 return new WrappedFileObject(fo);
jjg@610 71 }
jjg@610 72
jjg@610 73 @Override
jjg@610 74 protected JavaFileObject unwrap(JavaFileObject fo) {
jjg@610 75 if (fo instanceof WrappedFileObject)
jjg@610 76 return ((WrappedFileObject) fo).delegate;
jjg@610 77 else
jjg@610 78 return fo;
jjg@610 79 }
jjg@610 80
jjg@610 81 public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
jjg@610 82 return wrap2(fileManager.getJavaFileObjectsFromFiles(files));
jjg@610 83 }
jjg@610 84
jjg@610 85 public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
jjg@610 86 return wrap2(fileManager.getJavaFileObjects(files));
jjg@610 87 }
jjg@610 88
jjg@610 89 public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
jjg@610 90 return wrap2(fileManager.getJavaFileObjectsFromStrings(names));
jjg@610 91 }
jjg@610 92
jjg@610 93 public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
jjg@610 94 return wrap2(fileManager.getJavaFileObjects(names));
jjg@610 95 }
jjg@610 96
jjg@610 97 /* This method is regrettably necessary because WrappingJavaFileManager.wrap takes
jjg@610 98 * Iterable<JavaFileObject> fileObjects
jjg@610 99 * instead of
jjg@610 100 * Iterable<? extends JavaFileObject> fileObjects
jjg@610 101 */
jjg@610 102 protected Iterable<JavaFileObject> wrap2(Iterable<? extends JavaFileObject> fileObjects) {
jjg@610 103 List<JavaFileObject> mapped = new ArrayList<JavaFileObject>();
jjg@610 104 for (JavaFileObject fileObject : fileObjects)
jjg@610 105 mapped.add(wrap(fileObject));
jjg@610 106 return Collections.unmodifiableList(mapped);
jjg@610 107 }
jjg@610 108
jjg@610 109 public void setLocation(Location location, Iterable<? extends File> path) throws IOException {
jjg@610 110 fileManager.setLocation(location, path);
jjg@610 111 }
jjg@610 112
jjg@610 113 public Iterable<? extends File> getLocation(Location location) {
jjg@610 114 return fileManager.getLocation(location);
jjg@610 115 }
jjg@610 116
jjg@610 117 class WrappedFileObject implements JavaFileObject {
jjg@610 118 WrappedFileObject(JavaFileObject fileObject) {
jjg@610 119 delegate = fileObject;
jjg@610 120 }
jjg@610 121
jjg@610 122 public Kind getKind() {
jjg@610 123 return delegate.getKind();
jjg@610 124 }
jjg@610 125
jjg@610 126 public boolean isNameCompatible(String simpleName, Kind kind) {
jjg@610 127 return delegate.isNameCompatible(simpleName, kind);
jjg@610 128 }
jjg@610 129
jjg@610 130 public NestingKind getNestingKind() {
jjg@610 131 return delegate.getNestingKind();
jjg@610 132 }
jjg@610 133
jjg@610 134 public Modifier getAccessLevel() {
jjg@610 135 return delegate.getAccessLevel();
jjg@610 136 }
jjg@610 137
jjg@610 138 public URI toUri() {
jjg@610 139 return delegate.toUri();
jjg@610 140 }
jjg@610 141
jjg@610 142 public String getName() {
jjg@610 143 return delegate.getName();
jjg@610 144 }
jjg@610 145
jjg@610 146 public InputStream openInputStream() throws IOException {
jjg@610 147 checkRead();
jjg@610 148 return delegate.openInputStream();
jjg@610 149 }
jjg@610 150
jjg@610 151 public OutputStream openOutputStream() throws IOException {
jjg@610 152 checkWrite();
jjg@610 153 return delegate.openOutputStream();
jjg@610 154 }
jjg@610 155
jjg@610 156 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
jjg@610 157 checkRead();
jjg@610 158 return delegate.openReader(ignoreEncodingErrors);
jjg@610 159 }
jjg@610 160
jjg@610 161 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@610 162 checkRead();
jjg@610 163 return delegate.getCharContent(ignoreEncodingErrors);
jjg@610 164 }
jjg@610 165
jjg@610 166 public Writer openWriter() throws IOException {
jjg@610 167 checkWrite();
jjg@610 168 return delegate.openWriter();
jjg@610 169 }
jjg@610 170
jjg@610 171 public long getLastModified() {
jjg@610 172 return delegate.getLastModified();
jjg@610 173 }
jjg@610 174
jjg@610 175 public boolean delete() {
jjg@610 176 return delegate.delete();
jjg@610 177 }
jjg@610 178
jjg@610 179 void checkRead() throws IOException {
jjg@708 180 String canonName = getName().replace(File.separatorChar, '/');
jjg@708 181 if (cantRead != null && cantRead.matcher(canonName).matches())
jjg@610 182 throw new IOException("FileManager: Can't read");
jjg@610 183 }
jjg@610 184
jjg@610 185 void checkWrite() throws IOException {
jjg@708 186 String canonName = getName().replace(File.separatorChar, '/');
jjg@708 187 if (cantWrite != null && cantWrite.matcher(canonName).matches())
jjg@610 188 throw new IOException("FileManager: Can't write");
jjg@610 189 }
jjg@610 190
jjg@610 191 JavaFileObject delegate;
jjg@610 192 }
jjg@610 193 }

mercurial