Thu, 07 Jun 2012 20:30:57 -0700
7170079: Adjustments to build-infra makefiles
Reviewed-by: ohair, ohrstrom, ihse, jonas
Contributed-by: jonas <jonas.oreland@oracle.com>, erikj <erik.joelsson@oracle.com>, ihse <magnus.ihse.bursie@oracle.com>, tgranat <torbjorn.granat@oracle.com>, ykantser <yekaterina.kantserova@oracle.com>
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 */
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;
41 import com.sun.tools.javac.api.WrappingJavaFileManager;
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:";
53 private Pattern cantRead;
54 private Pattern cantWrite;
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 }
68 @Override
69 protected JavaFileObject wrap(JavaFileObject fo) {
70 return new WrappedFileObject(fo);
71 }
73 @Override
74 protected JavaFileObject unwrap(JavaFileObject fo) {
75 if (fo instanceof WrappedFileObject)
76 return ((WrappedFileObject) fo).delegate;
77 else
78 return fo;
79 }
81 public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
82 return wrap2(fileManager.getJavaFileObjectsFromFiles(files));
83 }
85 public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
86 return wrap2(fileManager.getJavaFileObjects(files));
87 }
89 public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
90 return wrap2(fileManager.getJavaFileObjectsFromStrings(names));
91 }
93 public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
94 return wrap2(fileManager.getJavaFileObjects(names));
95 }
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 }
109 public void setLocation(Location location, Iterable<? extends File> path) throws IOException {
110 fileManager.setLocation(location, path);
111 }
113 public Iterable<? extends File> getLocation(Location location) {
114 return fileManager.getLocation(location);
115 }
117 class WrappedFileObject implements JavaFileObject {
118 WrappedFileObject(JavaFileObject fileObject) {
119 delegate = fileObject;
120 }
122 public Kind getKind() {
123 return delegate.getKind();
124 }
126 public boolean isNameCompatible(String simpleName, Kind kind) {
127 return delegate.isNameCompatible(simpleName, kind);
128 }
130 public NestingKind getNestingKind() {
131 return delegate.getNestingKind();
132 }
134 public Modifier getAccessLevel() {
135 return delegate.getAccessLevel();
136 }
138 public URI toUri() {
139 return delegate.toUri();
140 }
142 public String getName() {
143 return delegate.getName();
144 }
146 public InputStream openInputStream() throws IOException {
147 checkRead();
148 return delegate.openInputStream();
149 }
151 public OutputStream openOutputStream() throws IOException {
152 checkWrite();
153 return delegate.openOutputStream();
154 }
156 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
157 checkRead();
158 return delegate.openReader(ignoreEncodingErrors);
159 }
161 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
162 checkRead();
163 return delegate.getCharContent(ignoreEncodingErrors);
164 }
166 public Writer openWriter() throws IOException {
167 checkWrite();
168 return delegate.openWriter();
169 }
171 public long getLastModified() {
172 return delegate.getLastModified();
173 }
175 public boolean delete() {
176 return delegate.delete();
177 }
179 void checkRead() throws IOException {
180 String canonName = getName().replace(File.separatorChar, '/');
181 if (cantRead != null && cantRead.matcher(canonName).matches())
182 throw new IOException("FileManager: Can't read");
183 }
185 void checkWrite() throws IOException {
186 String canonName = getName().replace(File.separatorChar, '/');
187 if (cantWrite != null && cantWrite.matcher(canonName).matches())
188 throw new IOException("FileManager: Can't write");
189 }
191 JavaFileObject delegate;
192 }
193 }