jjg@610: /* jjg@610: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@610: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@610: * jjg@610: * This code is free software; you can redistribute it and/or modify it jjg@610: * under the terms of the GNU General Public License version 2 only, as jjg@610: * published by the Free Software Foundation. jjg@610: * jjg@610: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@610: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@610: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@610: * version 2 for more details (a copy is included in the LICENSE file that jjg@610: * accompanied this code). jjg@610: * jjg@610: * You should have received a copy of the GNU General Public License version jjg@610: * 2 along with this work; if not, write to the Free Software Foundation, jjg@610: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@610: * jjg@610: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@610: * or visit www.oracle.com if you need additional information or have any jjg@610: * questions. jjg@610: */ jjg@610: jjg@610: import java.io.File; jjg@610: import java.io.IOException; jjg@610: import java.io.InputStream; jjg@610: import java.io.OutputStream; jjg@610: import java.io.Reader; jjg@610: import java.io.Writer; jjg@610: import java.net.URI; jjg@610: import java.util.ArrayList; jjg@610: import java.util.Collections; jjg@610: import java.util.List; jjg@610: import java.util.regex.Pattern; jjg@610: import javax.lang.model.element.Modifier; jjg@610: import javax.lang.model.element.NestingKind; jjg@610: import javax.tools.JavaFileManager.Location; jjg@610: import javax.tools.JavaFileObject; jjg@610: import javax.tools.StandardJavaFileManager; jjg@610: jjg@610: import com.sun.tools.javac.api.WrappingJavaFileManager; jjg@610: jjg@610: /** jjg@610: * A JavaFileManager that can throw IOException on attempting to read or write jjg@610: * selected files that match a regular expression. jjg@610: */ jjg@610: public class FileManager jjg@610: extends WrappingJavaFileManager jjg@610: implements StandardJavaFileManager { jjg@610: private static final String CANT_READ = "cantRead:"; jjg@610: private static final String CANT_WRITE = "cantWrite:"; jjg@610: jjg@610: private Pattern cantRead; jjg@610: private Pattern cantWrite; jjg@610: jjg@610: public FileManager(StandardJavaFileManager fm, List opts) { jjg@610: super(fm); jjg@610: for (String opt: opts) { jjg@610: if (opt.startsWith(CANT_READ)) jjg@610: cantRead = Pattern.compile(opt.substring(CANT_READ.length())); jjg@610: else if (opt.startsWith(CANT_WRITE)) jjg@610: cantWrite = Pattern.compile(opt.substring(CANT_WRITE.length())); jjg@610: else jjg@610: throw new IllegalArgumentException(opt); jjg@610: } jjg@610: } jjg@610: jjg@610: @Override jjg@610: protected JavaFileObject wrap(JavaFileObject fo) { jjg@610: return new WrappedFileObject(fo); jjg@610: } jjg@610: jjg@610: @Override jjg@610: protected JavaFileObject unwrap(JavaFileObject fo) { jjg@610: if (fo instanceof WrappedFileObject) jjg@610: return ((WrappedFileObject) fo).delegate; jjg@610: else jjg@610: return fo; jjg@610: } jjg@610: jjg@610: public Iterable getJavaFileObjectsFromFiles(Iterable files) { jjg@610: return wrap2(fileManager.getJavaFileObjectsFromFiles(files)); jjg@610: } jjg@610: jjg@610: public Iterable getJavaFileObjects(File... files) { jjg@610: return wrap2(fileManager.getJavaFileObjects(files)); jjg@610: } jjg@610: jjg@610: public Iterable getJavaFileObjectsFromStrings(Iterable names) { jjg@610: return wrap2(fileManager.getJavaFileObjectsFromStrings(names)); jjg@610: } jjg@610: jjg@610: public Iterable getJavaFileObjects(String... names) { jjg@610: return wrap2(fileManager.getJavaFileObjects(names)); jjg@610: } jjg@610: jjg@610: /* This method is regrettably necessary because WrappingJavaFileManager.wrap takes jjg@610: * Iterable fileObjects jjg@610: * instead of jjg@610: * Iterable fileObjects jjg@610: */ jjg@610: protected Iterable wrap2(Iterable fileObjects) { jjg@610: List mapped = new ArrayList(); jjg@610: for (JavaFileObject fileObject : fileObjects) jjg@610: mapped.add(wrap(fileObject)); jjg@610: return Collections.unmodifiableList(mapped); jjg@610: } jjg@610: jjg@610: public void setLocation(Location location, Iterable path) throws IOException { jjg@610: fileManager.setLocation(location, path); jjg@610: } jjg@610: jjg@610: public Iterable getLocation(Location location) { jjg@610: return fileManager.getLocation(location); jjg@610: } jjg@610: jjg@610: class WrappedFileObject implements JavaFileObject { jjg@610: WrappedFileObject(JavaFileObject fileObject) { jjg@610: delegate = fileObject; jjg@610: } jjg@610: jjg@610: public Kind getKind() { jjg@610: return delegate.getKind(); jjg@610: } jjg@610: jjg@610: public boolean isNameCompatible(String simpleName, Kind kind) { jjg@610: return delegate.isNameCompatible(simpleName, kind); jjg@610: } jjg@610: jjg@610: public NestingKind getNestingKind() { jjg@610: return delegate.getNestingKind(); jjg@610: } jjg@610: jjg@610: public Modifier getAccessLevel() { jjg@610: return delegate.getAccessLevel(); jjg@610: } jjg@610: jjg@610: public URI toUri() { jjg@610: return delegate.toUri(); jjg@610: } jjg@610: jjg@610: public String getName() { jjg@610: return delegate.getName(); jjg@610: } jjg@610: jjg@610: public InputStream openInputStream() throws IOException { jjg@610: checkRead(); jjg@610: return delegate.openInputStream(); jjg@610: } jjg@610: jjg@610: public OutputStream openOutputStream() throws IOException { jjg@610: checkWrite(); jjg@610: return delegate.openOutputStream(); jjg@610: } jjg@610: jjg@610: public Reader openReader(boolean ignoreEncodingErrors) throws IOException { jjg@610: checkRead(); jjg@610: return delegate.openReader(ignoreEncodingErrors); jjg@610: } jjg@610: jjg@610: public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { jjg@610: checkRead(); jjg@610: return delegate.getCharContent(ignoreEncodingErrors); jjg@610: } jjg@610: jjg@610: public Writer openWriter() throws IOException { jjg@610: checkWrite(); jjg@610: return delegate.openWriter(); jjg@610: } jjg@610: jjg@610: public long getLastModified() { jjg@610: return delegate.getLastModified(); jjg@610: } jjg@610: jjg@610: public boolean delete() { jjg@610: return delegate.delete(); jjg@610: } jjg@610: jjg@610: void checkRead() throws IOException { jjg@708: String canonName = getName().replace(File.separatorChar, '/'); jjg@708: if (cantRead != null && cantRead.matcher(canonName).matches()) jjg@610: throw new IOException("FileManager: Can't read"); jjg@610: } jjg@610: jjg@610: void checkWrite() throws IOException { jjg@708: String canonName = getName().replace(File.separatorChar, '/'); jjg@708: if (cantWrite != null && cantWrite.matcher(canonName).matches()) jjg@610: throw new IOException("FileManager: Can't write"); jjg@610: } jjg@610: jjg@610: JavaFileObject delegate; jjg@610: } jjg@610: }