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