test/tools/javac/diags/FileManager.java

changeset 610
3640b60bd0f6
child 708
c8b4a1e76089
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/diags/FileManager.java	Thu Jul 22 11:02:54 2010 -0700
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 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.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import java.io.File;
    1.28 +import java.io.IOException;
    1.29 +import java.io.InputStream;
    1.30 +import java.io.OutputStream;
    1.31 +import java.io.Reader;
    1.32 +import java.io.Writer;
    1.33 +import java.net.URI;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.Collections;
    1.36 +import java.util.List;
    1.37 +import java.util.regex.Pattern;
    1.38 +import javax.lang.model.element.Modifier;
    1.39 +import javax.lang.model.element.NestingKind;
    1.40 +import javax.tools.JavaFileManager.Location;
    1.41 +import javax.tools.JavaFileObject;
    1.42 +import javax.tools.StandardJavaFileManager;
    1.43 +
    1.44 +import com.sun.tools.javac.api.WrappingJavaFileManager;
    1.45 +
    1.46 +/**
    1.47 + * A JavaFileManager that can throw IOException on attempting to read or write
    1.48 + * selected files that match a regular expression.
    1.49 + */
    1.50 +public class FileManager
    1.51 +        extends WrappingJavaFileManager<StandardJavaFileManager>
    1.52 +        implements StandardJavaFileManager {
    1.53 +    private static final String CANT_READ = "cantRead:";
    1.54 +    private static final String CANT_WRITE = "cantWrite:";
    1.55 +
    1.56 +    private Pattern cantRead;
    1.57 +    private Pattern cantWrite;
    1.58 +
    1.59 +    public FileManager(StandardJavaFileManager fm, List<String> opts) {
    1.60 +        super(fm);
    1.61 +        for (String opt: opts) {
    1.62 +            if (opt.startsWith(CANT_READ))
    1.63 +                cantRead = Pattern.compile(opt.substring(CANT_READ.length()));
    1.64 +            else if (opt.startsWith(CANT_WRITE))
    1.65 +                cantWrite = Pattern.compile(opt.substring(CANT_WRITE.length()));
    1.66 +            else
    1.67 +                throw new IllegalArgumentException(opt);
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    @Override
    1.72 +    protected JavaFileObject wrap(JavaFileObject fo) {
    1.73 +        return new WrappedFileObject(fo);
    1.74 +    }
    1.75 +
    1.76 +    @Override
    1.77 +    protected JavaFileObject unwrap(JavaFileObject fo) {
    1.78 +        if (fo instanceof WrappedFileObject)
    1.79 +            return ((WrappedFileObject) fo).delegate;
    1.80 +        else
    1.81 +            return fo;
    1.82 +    }
    1.83 +
    1.84 +    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
    1.85 +        return wrap2(fileManager.getJavaFileObjectsFromFiles(files));
    1.86 +    }
    1.87 +
    1.88 +    public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
    1.89 +        return wrap2(fileManager.getJavaFileObjects(files));
    1.90 +    }
    1.91 +
    1.92 +    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
    1.93 +        return wrap2(fileManager.getJavaFileObjectsFromStrings(names));
    1.94 +    }
    1.95 +
    1.96 +    public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
    1.97 +        return wrap2(fileManager.getJavaFileObjects(names));
    1.98 +    }
    1.99 +
   1.100 +    /* This method is regrettably necessary because WrappingJavaFileManager.wrap takes
   1.101 +     *          Iterable<JavaFileObject> fileObjects
   1.102 +     * instead of
   1.103 +     *          Iterable<? extends JavaFileObject> fileObjects
   1.104 +     */
   1.105 +    protected Iterable<JavaFileObject> wrap2(Iterable<? extends JavaFileObject> fileObjects) {
   1.106 +        List<JavaFileObject> mapped = new ArrayList<JavaFileObject>();
   1.107 +        for (JavaFileObject fileObject : fileObjects)
   1.108 +            mapped.add(wrap(fileObject));
   1.109 +        return Collections.unmodifiableList(mapped);
   1.110 +    }
   1.111 +
   1.112 +    public void setLocation(Location location, Iterable<? extends File> path) throws IOException {
   1.113 +        fileManager.setLocation(location, path);
   1.114 +    }
   1.115 +
   1.116 +    public Iterable<? extends File> getLocation(Location location) {
   1.117 +        return fileManager.getLocation(location);
   1.118 +    }
   1.119 +
   1.120 +    class WrappedFileObject implements JavaFileObject {
   1.121 +        WrappedFileObject(JavaFileObject fileObject) {
   1.122 +            delegate = fileObject;
   1.123 +        }
   1.124 +
   1.125 +        public Kind getKind() {
   1.126 +            return delegate.getKind();
   1.127 +        }
   1.128 +
   1.129 +        public boolean isNameCompatible(String simpleName, Kind kind) {
   1.130 +            return delegate.isNameCompatible(simpleName, kind);
   1.131 +        }
   1.132 +
   1.133 +        public NestingKind getNestingKind() {
   1.134 +            return delegate.getNestingKind();
   1.135 +        }
   1.136 +
   1.137 +        public Modifier getAccessLevel() {
   1.138 +            return delegate.getAccessLevel();
   1.139 +        }
   1.140 +
   1.141 +        public URI toUri() {
   1.142 +            return delegate.toUri();
   1.143 +        }
   1.144 +
   1.145 +        public String getName() {
   1.146 +            return delegate.getName();
   1.147 +        }
   1.148 +
   1.149 +        public InputStream openInputStream() throws IOException {
   1.150 +            checkRead();
   1.151 +            return delegate.openInputStream();
   1.152 +        }
   1.153 +
   1.154 +        public OutputStream openOutputStream() throws IOException {
   1.155 +            checkWrite();
   1.156 +            return delegate.openOutputStream();
   1.157 +        }
   1.158 +
   1.159 +        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   1.160 +            checkRead();
   1.161 +            return delegate.openReader(ignoreEncodingErrors);
   1.162 +        }
   1.163 +
   1.164 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.165 +            checkRead();
   1.166 +            return delegate.getCharContent(ignoreEncodingErrors);
   1.167 +        }
   1.168 +
   1.169 +        public Writer openWriter() throws IOException {
   1.170 +            checkWrite();
   1.171 +            return delegate.openWriter();
   1.172 +        }
   1.173 +
   1.174 +        public long getLastModified() {
   1.175 +            return delegate.getLastModified();
   1.176 +        }
   1.177 +
   1.178 +        public boolean delete() {
   1.179 +            return delegate.delete();
   1.180 +        }
   1.181 +
   1.182 +        void checkRead() throws IOException {
   1.183 +            if (cantRead != null && cantRead.matcher(getName()).matches())
   1.184 +                throw new IOException("FileManager: Can't read");
   1.185 +        }
   1.186 +
   1.187 +        void checkWrite() throws IOException {
   1.188 +            if (cantWrite != null && cantWrite.matcher(getName()).matches())
   1.189 +                throw new IOException("FileManager: Can't write");
   1.190 +        }
   1.191 +
   1.192 +        JavaFileObject delegate;
   1.193 +    }
   1.194 +}

mercurial