WrappingJavaFileManager.java revision 2571:10fc81ac75b4
1/*
2 * Copyright (c) 2006, 2012, 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
26package com.sun.tools.javac.api;
27
28import java.io.IOException;
29import java.net.URI;
30import java.util.ArrayList;
31import java.util.Collections;
32import java.util.List;
33import java.util.Set;
34
35import javax.tools.*;
36import javax.tools.JavaFileObject.Kind;
37
38/**
39 * Wraps all calls to a given file manager.  Subclasses of this class
40 * might override some of these methods and might also provide
41 * additional fields and methods.
42 *
43 * <p>This class might be moved to {@link javax.tools} in a future
44 * release.
45 *
46 * <p><b>This is NOT part of any supported API.
47 * If you write code that depends on this, you do so at your own
48 * risk.  This code and its internal interfaces are subject to change
49 * or deletion without notice.</b></p>
50 *
51 * @param <M> the type of file manager wrapped to by this object
52 *
53 * @author Peter von der Ah&eacute;
54 * @since 1.6
55 */
56public class WrappingJavaFileManager<M extends JavaFileManager> extends ForwardingJavaFileManager<M> {
57
58    /**
59     * Creates a new instance of WrappingJavaFileManager.
60     * @param fileManager file manager to be wrapped
61     */
62    protected WrappingJavaFileManager(M fileManager) {
63        super(fileManager);
64    }
65
66    /**
67     * This implementation returns the given file object.  Subclasses
68     * may override this behavior.
69     *
70     * @param fileObject a file object
71     */
72    protected FileObject wrap(FileObject fileObject) {
73        return fileObject;
74    }
75
76    /**
77     * This implementation forwards to {@link #wrap(FileObject)}.
78     * Subclasses may override this behavior.
79     *
80     * @param fileObject a file object
81     * @throws ClassCastException if the file object returned from the
82     * forwarded call is not a subtype of {@linkplain JavaFileObject}
83     */
84    protected JavaFileObject wrap(JavaFileObject fileObject) {
85        return (JavaFileObject)wrap((FileObject)fileObject);
86    }
87
88    /**
89     * This implementation returns the given file object.  Subclasses
90     * may override this behavior.
91     *
92     * @param fileObject a file object
93     */
94    protected FileObject unwrap(FileObject fileObject) {
95        return fileObject;
96    }
97
98    /**
99     * This implementation forwards to {@link #unwrap(FileObject)}.
100     * Subclasses may override this behavior.
101     *
102     * @param fileObject a file object
103     * @throws ClassCastException if the file object returned from the
104     * forwarded call is not a subtype of {@linkplain JavaFileObject}
105     */
106    protected JavaFileObject unwrap(JavaFileObject fileObject) {
107        return (JavaFileObject)unwrap((FileObject)fileObject);
108    }
109
110    /**
111     * This implementation maps the given list of file objects by
112     * calling wrap on each.  Subclasses may override this behavior.
113     *
114     * @param fileObjects a list of file objects
115     * @return the mapping
116     */
117    protected Iterable<JavaFileObject> wrap(Iterable<JavaFileObject> fileObjects) {
118        List<JavaFileObject> mapped = new ArrayList<>();
119        for (JavaFileObject fileObject : fileObjects)
120            mapped.add(wrap(fileObject));
121        return Collections.unmodifiableList(mapped);
122    }
123
124    /**
125     * This implementation returns the given URI.  Subclasses may
126     * override this behavior.
127     *
128     * @param uri a URI
129     */
130    protected URI unwrap(URI uri) {
131        return uri;
132    }
133
134    /**
135     * @throws IllegalStateException {@inheritDoc}
136     */
137    public Iterable<JavaFileObject> list(Location location,
138                                         String packageName,
139                                         Set<Kind> kinds,
140                                         boolean recurse)
141        throws IOException
142    {
143        return wrap(super.list(location, packageName, kinds, recurse));
144    }
145
146    /**
147     * @throws IllegalStateException {@inheritDoc}
148     */
149    public String inferBinaryName(Location location, JavaFileObject file) {
150        return super.inferBinaryName(location, unwrap(file));
151    }
152
153    /**
154     * @throws IllegalArgumentException {@inheritDoc}
155     * @throws UnsupportedOperationException {@inheritDoc}
156     * @throws IllegalStateException {@inheritDoc}
157     */
158    public JavaFileObject getJavaFileForInput(Location location,
159                                              String className,
160                                              Kind kind)
161        throws IOException
162    {
163        return wrap(super.getJavaFileForInput(location, className, kind));
164    }
165
166    /**
167     * @throws IllegalArgumentException {@inheritDoc}
168     * @throws UnsupportedOperationException {@inheritDoc}
169     * @throws IllegalStateException {@inheritDoc}
170     */
171    public JavaFileObject getJavaFileForOutput(Location location,
172                                               String className,
173                                               Kind kind,
174                                               FileObject sibling)
175        throws IOException
176    {
177        return wrap(super.getJavaFileForOutput(location, className, kind, unwrap(sibling)));
178    }
179
180    /**
181     * @throws IllegalArgumentException {@inheritDoc}
182     * @throws IllegalStateException {@inheritDoc}
183     */
184    public FileObject getFileForInput(Location location,
185                                      String packageName,
186                                      String relativeName)
187        throws IOException
188    {
189        return wrap(super.getFileForInput(location, packageName, relativeName));
190    }
191
192    /**
193     * @throws IllegalArgumentException {@inheritDoc}
194     * @throws IllegalStateException {@inheritDoc}
195     */
196    public FileObject getFileForOutput(Location location,
197                                       String packageName,
198                                       String relativeName,
199                                       FileObject sibling)
200        throws IOException
201    {
202        return wrap(super.getFileForOutput(location,
203                                           packageName,
204                                           relativeName,
205                                           unwrap(sibling)));
206    }
207
208}
209