1 /* 2 * Copyright (c) 2006, 2008, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javac.api; 27 28 import java.io.IOException; 29 import java.net.URI; 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Set; 34 import javax.tools.JavaFileObject.Kind; 35 import javax.tools; 36 37 /** 38 * Wraps all calls to a given file manager. Subclasses of this class 39 * might override some of these methods and might also provide 40 * additional fields and methods. 41 * 42 * <p>This class might be moved to {@link javax.tools} in a future 43 * release. 44 * 45 * <p><b>This is NOT part of any supported API. 46 * If you write code that depends on this, you do so at your own 47 * risk. This code and its internal interfaces are subject to change 48 * or deletion without notice.</b></p> 49 * 50 * @param <M> the type of file manager wrapped to by this object 51 * 52 * @author Peter von der Ahé 53 * @since 1.6 54 */ 55 public class WrappingJavaFileManager<M extends JavaFileManager> extends ForwardingJavaFileManager<M> { 56 57 /** 58 * Creates a new instance of WrappingJavaFileManager. 59 * @param fileManager file manager to be wrapped 60 */ 61 protected WrappingJavaFileManager(M fileManager) { 62 super(fileManager); 63 } 64 65 /** 66 * This implementation returns the given file object. Subclasses 67 * may override this behavior. 68 * 69 * @param fileObject a file object 70 */ 71 protected FileObject wrap(FileObject fileObject) { 72 return fileObject; 73 } 74 75 /** 76 * This implementation forwards to {@link #wrap(FileObject)}. 77 * Subclasses may override this behavior. 78 * 79 * @param fileObject a file object 80 * @throws ClassCastException if the file object returned from the 81 * forwarded call is not a subtype of {@linkplain JavaFileObject} 82 */ 83 protected JavaFileObject wrap(JavaFileObject fileObject) { 84 return (JavaFileObject)wrap((FileObject)fileObject); 85 } 86 87 /** 88 * This implementation returns the given file object. Subclasses 89 * may override this behavior. 90 * 91 * @param fileObject a file object 92 */ 93 protected FileObject unwrap(FileObject fileObject) { 94 return fileObject; 95 } 96 97 /** 98 * This implementation forwards to {@link #unwrap(FileObject)}. 99 * Subclasses may override this behavior. 100 * 101 * @param fileObject a file object 102 * @throws ClassCastException if the file object returned from the 103 * forwarded call is not a subtype of {@linkplain JavaFileObject} 104 */ 105 protected JavaFileObject unwrap(JavaFileObject fileObject) { 106 return (JavaFileObject)unwrap((FileObject)fileObject); 107 } 108 109 /** 110 * This implementation maps the given list of file objects by 111 * calling wrap on each. Subclasses may override this behavior. 112 * 113 * @param fileObjects a list of file objects 114 * @return the mapping 115 */ 116 protected Iterable<JavaFileObject> wrap(Iterable<JavaFileObject> fileObjects) { 117 List<JavaFileObject> mapped = new ArrayList<JavaFileObject>(); 118 for (JavaFileObject fileObject : fileObjects) 119 mapped.add(wrap(fileObject)); 120 return Collections.unmodifiableList(mapped); 121 } 122 123 /** 124 * This implementation returns the given URI. Subclasses may 125 * override this behavior. 126 * 127 * @param uri a URI 128 */ 129 protected URI unwrap(URI uri) { 130 return uri; 131 } 132 133 /** 134 * @throws IllegalStateException {@inheritDoc} 135 */ 136 public Iterable<JavaFileObject> list(Location location, 137 String packageName, 138 Set<Kind> kinds, 139 boolean recurse) 140 throws IOException 141 { 142 return wrap(super.list(location, packageName, kinds, recurse)); 143 } 144 145 /** 146 * @throws IllegalStateException {@inheritDoc} 147 */ 148 public String inferBinaryName(Location location, JavaFileObject file) { 149 return super.inferBinaryName(location, unwrap(file)); 150 } 151 152 /** 153 * @throws IllegalArgumentException {@inheritDoc} 154 * @throws UnsupportedOperationException {@inheritDoc} 155 * @throws IllegalStateException {@inheritDoc} 156 */ 157 public JavaFileObject getJavaFileForInput(Location location, 158 String className, 159 Kind kind) 160 throws IOException 161 { 162 return wrap(super.getJavaFileForInput(location, className, kind)); 163 } 164 165 /** 166 * @throws IllegalArgumentException {@inheritDoc} 167 * @throws UnsupportedOperationException {@inheritDoc} 168 * @throws IllegalStateException {@inheritDoc} 169 */ 170 public JavaFileObject getJavaFileForOutput(Location location, 171 String className, 172 Kind kind, 173 FileObject sibling) 174 throws IOException 175 { 176 return wrap(super.getJavaFileForOutput(location, className, kind, unwrap(sibling))); 177 } 178 179 /** 180 * @throws IllegalArgumentException {@inheritDoc} 181 * @throws IllegalStateException {@inheritDoc} 182 */ 183 public FileObject getFileForInput(Location location, 184 String packageName, 185 String relativeName) 186 throws IOException 187 { 188 return wrap(super.getFileForInput(location, packageName, relativeName)); 189 } 190 191 /** 192 * @throws IllegalArgumentException {@inheritDoc} 193 * @throws IllegalStateException {@inheritDoc} 194 */ 195 public FileObject getFileForOutput(Location location, 196 String packageName, 197 String relativeName, 198 FileObject sibling) 199 throws IOException 200 { 201 return wrap(super.getFileForOutput(location, 202 packageName, 203 relativeName, 204 unwrap(sibling))); 205 } 206 207 }