test/tools/javac/diags/FileManager.java

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

mercurial