001    /*
002     * Copyright 2006-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.api;
027    
028    import java.io.IOException;
029    import java.net.URI;
030    import java.util.ArrayList;
031    import java.util.Collections;
032    import java.util.List;
033    import java.util.Set;
034    import javax.tools.JavaFileObject.Kind;
035    import javax.tools.*;
036    
037    /**
038     * Wraps all calls to a given file manager.  Subclasses of this class
039     * might override some of these methods and might also provide
040     * additional fields and methods.
041     *
042     * <p>This class might be moved to {@link javax.tools} in a future
043     * release.
044     *
045     * <p><b>This is NOT part of any API supported by Sun Microsystems.
046     * If you write code that depends on this, you do so at your own
047     * risk.  This code and its internal interfaces are subject to change
048     * or deletion without notice.</b></p>
049     *
050     * @param <M> the type of file manager wrapped to by this object
051     *
052     * @author Peter von der Ah&eacute;
053     * @since 1.6
054     */
055    public class WrappingJavaFileManager<M extends JavaFileManager> extends ForwardingJavaFileManager<M> {
056    
057        /**
058         * Creates a new instance of WrappingJavaFileManager.
059         * @param fileManager file manager to be wrapped
060         */
061        protected WrappingJavaFileManager(M fileManager) {
062            super(fileManager);
063        }
064    
065        /**
066         * This implementation returns the given file object.  Subclasses
067         * may override this behavior.
068         *
069         * @param fileObject a file object
070         */
071        protected FileObject wrap(FileObject fileObject) {
072            return fileObject;
073        }
074    
075        /**
076         * This implementation forwards to {@link #wrap(FileObject)}.
077         * Subclasses may override this behavior.
078         *
079         * @param fileObject a file object
080         * @throws ClassCastException if the file object returned from the
081         * forwarded call is not a subtype of {@linkplain JavaFileObject}
082         */
083        protected JavaFileObject wrap(JavaFileObject fileObject) {
084            return (JavaFileObject)wrap((FileObject)fileObject);
085        }
086    
087        /**
088         * This implementation returns the given file object.  Subclasses
089         * may override this behavior.
090         *
091         * @param fileObject a file object
092         */
093        protected FileObject unwrap(FileObject fileObject) {
094            return fileObject;
095        }
096    
097        /**
098         * This implementation forwards to {@link #unwrap(FileObject)}.
099         * 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    }