001    /*
002     * Copyright 2005-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.processing;
027    
028    import com.sun.tools.javac.util.*;
029    import javax.annotation.processing.*;
030    import javax.lang.model.SourceVersion;
031    import javax.lang.model.element.NestingKind;
032    import javax.lang.model.element.Modifier;
033    import javax.lang.model.element.Element;
034    import java.util.*;
035    
036    import java.io.Closeable;
037    import java.io.InputStream;
038    import java.io.OutputStream;
039    import java.io.FilterOutputStream;
040    import java.io.Reader;
041    import java.io.Writer;
042    import java.io.FilterWriter;
043    import java.io.PrintWriter;
044    import java.io.IOException;
045    
046    import javax.tools.*;
047    import static java.util.Collections.*;
048    
049    import javax.tools.JavaFileManager.Location;
050    import static javax.tools.StandardLocation.SOURCE_OUTPUT;
051    import static javax.tools.StandardLocation.CLASS_OUTPUT;
052    
053    /**
054     * The FilerImplementation class must maintain a number of
055     * constraints.  First, multiple attempts to open the same path within
056     * the same invocation of the tool results in an IOException being
057     * thrown.  For example, trying to open the same source file twice:
058     *
059     * <pre>
060     * createSourceFile("foo.Bar")
061     * ...
062     * createSourceFile("foo.Bar")
063     * </pre>
064     *
065     * is disallowed as is opening a text file that happens to have
066     * the same name as a source file:
067     *
068     * <pre>
069     * createSourceFile("foo.Bar")
070     * ...
071     * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
072     * </pre>
073     *
074     * <p>Additionally, creating a source file that corresponds to an
075     * already created class file (or vice versa) also results in an
076     * IOException since each type can only be created once.  However, if
077     * the Filer is used to create a text file named *.java that happens
078     * to correspond to an existing class file, a warning is *not*
079     * generated.  Similarly, a warning is not generated for a binary file
080     * named *.class and an existing source file.
081     *
082     * <p>The reason for this difference is that source files and class
083     * files are registered with the tool and can get passed on as
084     * declarations to the next round of processing.  Files that are just
085     * named *.java and *.class are not processed in that manner; although
086     * having extra source files and class files on the source path and
087     * class path can alter the behavior of the tool and any final
088     * compile.
089     *
090     * <p><b>This is NOT part of any API supported by Sun Microsystems.
091     * If you write code that depends on this, you do so at your own risk.
092     * This code and its internal interfaces are subject to change or
093     * deletion without notice.</b>
094     */
095    public class JavacFiler implements Filer, Closeable {
096        // TODO: Implement different transaction model for updating the
097        // Filer's record keeping on file close.
098    
099        private static final String ALREADY_OPENED =
100            "Output stream or writer has already been opened.";
101        private static final String NOT_FOR_READING =
102            "FileObject was not opened for reading.";
103        private static final String NOT_FOR_WRITING =
104            "FileObject was not opened for writing.";
105    
106        /**
107         * Wrap a JavaFileObject to manage writing by the Filer.
108         */
109        private class FilerOutputFileObject extends ForwardingFileObject<FileObject> {
110            private boolean opened = false;
111            private String name;
112    
113            FilerOutputFileObject(String name, FileObject fileObject) {
114                super(fileObject);
115                this.name = name;
116            }
117    
118            @Override
119            public synchronized OutputStream openOutputStream() throws IOException {
120                if (opened)
121                    throw new IOException(ALREADY_OPENED);
122                opened = true;
123                return new FilerOutputStream(name, fileObject);
124            }
125    
126            @Override
127            public synchronized Writer openWriter() throws IOException {
128                if (opened)
129                    throw new IOException(ALREADY_OPENED);
130                opened = true;
131                return new FilerWriter(name, fileObject);
132            }
133    
134            // Three anti-literacy methods
135            @Override
136            public InputStream openInputStream() throws IOException {
137                throw new IllegalStateException(NOT_FOR_READING);
138            }
139    
140            @Override
141            public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
142                throw new IllegalStateException(NOT_FOR_READING);
143            }
144    
145            @Override
146            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
147                throw new IllegalStateException(NOT_FOR_READING);
148            }
149    
150            @Override
151            public boolean delete() {
152                return false;
153            }
154        }
155    
156        private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject {
157            private final JavaFileObject javaFileObject;
158            FilerOutputJavaFileObject(String name, JavaFileObject javaFileObject) {
159                super(name, javaFileObject);
160                this.javaFileObject = javaFileObject;
161            }
162    
163            public JavaFileObject.Kind getKind() {
164                return javaFileObject.getKind();
165            }
166    
167            public boolean isNameCompatible(String simpleName,
168                                            JavaFileObject.Kind kind) {
169                return javaFileObject.isNameCompatible(simpleName, kind);
170            }
171    
172            public NestingKind getNestingKind() {
173                return javaFileObject.getNestingKind();
174            }
175    
176            public Modifier getAccessLevel() {
177                return javaFileObject.getAccessLevel();
178            }
179        }
180    
181        /**
182         * Wrap a JavaFileObject to manage reading by the Filer.
183         */
184        private class FilerInputFileObject extends ForwardingFileObject<FileObject> {
185            FilerInputFileObject(FileObject fileObject) {
186                super(fileObject);
187            }
188    
189            @Override
190            public OutputStream openOutputStream() throws IOException {
191                throw new IllegalStateException(NOT_FOR_WRITING);
192            }
193    
194            @Override
195            public Writer openWriter() throws IOException {
196                throw new IllegalStateException(NOT_FOR_WRITING);
197            }
198    
199            @Override
200            public boolean delete() {
201                return false;
202            }
203        }
204    
205        private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject {
206            private final JavaFileObject javaFileObject;
207            FilerInputJavaFileObject(JavaFileObject javaFileObject) {
208                super(javaFileObject);
209                this.javaFileObject = javaFileObject;
210            }
211    
212            public JavaFileObject.Kind getKind() {
213                return javaFileObject.getKind();
214            }
215    
216            public boolean isNameCompatible(String simpleName,
217                                            JavaFileObject.Kind kind) {
218                return javaFileObject.isNameCompatible(simpleName, kind);
219            }
220    
221            public NestingKind getNestingKind() {
222                return javaFileObject.getNestingKind();
223            }
224    
225            public Modifier getAccessLevel() {
226                return javaFileObject.getAccessLevel();
227            }
228        }
229    
230    
231        /**
232         * Wrap a {@code OutputStream} returned from the {@code
233         * JavaFileManager} to properly register source or class files
234         * when they are closed.
235         */
236        private class FilerOutputStream extends FilterOutputStream {
237            String typeName;
238            FileObject fileObject;
239            boolean closed = false;
240    
241            /**
242             * @param typeName name of class or {@code null} if just a
243             * binary file
244             */
245            FilerOutputStream(String typeName, FileObject fileObject) throws IOException {
246                super(fileObject.openOutputStream());
247                this.typeName = typeName;
248                this.fileObject = fileObject;
249            }
250    
251            public synchronized void close() throws IOException {
252                if (!closed) {
253                    closed = true;
254                    /*
255                     * If an IOException occurs when closing the underlying
256                     * stream, still try to process the file.
257                     */
258    
259                    closeFileObject(typeName, fileObject);
260                    out.close();
261                }
262            }
263        }
264    
265        /**
266         * Wrap a {@code Writer} returned from the {@code JavaFileManager}
267         * to properly register source or class files when they are
268         * closed.
269         */
270        private class FilerWriter extends FilterWriter {
271            String typeName;
272            FileObject fileObject;
273            boolean closed = false;
274    
275            /**
276             * @param fileObject the fileObject to be written to
277             * @param typeName name of source file or {@code null} if just a
278             * text file
279             */
280            FilerWriter(String typeName, FileObject fileObject) throws IOException {
281                super(fileObject.openWriter());
282                this.typeName = typeName;
283                this.fileObject = fileObject;
284            }
285    
286            public synchronized void close() throws IOException {
287                if (!closed) {
288                    closed = true;
289                    /*
290                     * If an IOException occurs when closing the underlying
291                     * Writer, still try to process the file.
292                     */
293    
294                    closeFileObject(typeName, fileObject);
295                    out.close();
296                }
297            }
298        }
299    
300        JavaFileManager fileManager;
301        Log log;
302        Context context;
303        boolean lastRound;
304    
305        private final boolean lint;
306    
307        /**
308         * Logical names of all created files.  This set must be
309         * synchronized.
310         */
311        private final Set<FileObject> fileObjectHistory;
312    
313        /**
314         * Names of types that have had files created but not closed.
315         */
316        private final Set<String> openTypeNames;
317    
318        /**
319         * Names of source files closed in this round.  This set must be
320         * synchronized.  Its iterators should preserve insertion order.
321         */
322        private Set<String> generatedSourceNames;
323    
324        /**
325         * Names and class files of the class files closed in this round.
326         * This set must be synchronized.  Its iterators should preserve
327         * insertion order.
328         */
329        private final Map<String, JavaFileObject> generatedClasses;
330    
331        /**
332         * JavaFileObjects for source files closed in this round.  This
333         * set must be synchronized.  Its iterators should preserve
334         * insertion order.
335         */
336        private Set<JavaFileObject> generatedSourceFileObjects;
337    
338        /**
339         * Names of all created source files.  Its iterators should
340         * preserve insertion order.
341         */
342        private final Set<String> aggregateGeneratedSourceNames;
343    
344        /**
345         * Names of all created class files.  Its iterators should
346         * preserve insertion order.
347         */
348        private final Set<String> aggregateGeneratedClassNames;
349    
350    
351        JavacFiler(Context context) {
352            this.context = context;
353            fileManager = context.get(JavaFileManager.class);
354    
355            log = Log.instance(context);
356    
357            fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>());
358            generatedSourceNames = synchronizedSet(new LinkedHashSet<String>());
359            generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>());
360    
361            generatedClasses = synchronizedMap(new LinkedHashMap<String, JavaFileObject>());
362    
363            openTypeNames  = synchronizedSet(new LinkedHashSet<String>());
364    
365            aggregateGeneratedSourceNames = new LinkedHashSet<String>();
366            aggregateGeneratedClassNames  = new LinkedHashSet<String>();
367    
368            lint = (Options.instance(context)).lint("processing");
369        }
370    
371        public JavaFileObject createSourceFile(CharSequence name,
372                                               Element... originatingElements) throws IOException {
373            return createSourceOrClassFile(true, name.toString());
374        }
375    
376        public JavaFileObject createClassFile(CharSequence name,
377                                               Element... originatingElements) throws IOException {
378            return createSourceOrClassFile(false, name.toString());
379        }
380    
381        private JavaFileObject createSourceOrClassFile(boolean isSourceFile, String name) throws IOException {
382            checkNameAndExistence(name, isSourceFile);
383            Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);
384            JavaFileObject.Kind kind = (isSourceFile ?
385                                        JavaFileObject.Kind.SOURCE :
386                                        JavaFileObject.Kind.CLASS);
387    
388            JavaFileObject fileObject =
389                fileManager.getJavaFileForOutput(loc, name, kind, null);
390            checkFileReopening(fileObject, true);
391    
392            if (lastRound)
393                log.warning("proc.file.create.last.round", name);
394    
395            if (isSourceFile)
396                aggregateGeneratedSourceNames.add(name);
397            else
398                aggregateGeneratedClassNames.add(name);
399            openTypeNames.add(name);
400    
401            return new FilerOutputJavaFileObject(name, fileObject);
402        }
403    
404        public FileObject createResource(JavaFileManager.Location location,
405                                         CharSequence pkg,
406                                         CharSequence relativeName,
407                                         Element... originatingElements) throws IOException {
408            locationCheck(location);
409    
410            String strPkg = pkg.toString();
411            if (strPkg.length() > 0)
412                checkName(strPkg);
413    
414            FileObject fileObject =
415                fileManager.getFileForOutput(location, strPkg,
416                                             relativeName.toString(), null);
417            checkFileReopening(fileObject, true);
418    
419            if (fileObject instanceof JavaFileObject)
420                return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject);
421            else
422                return new FilerOutputFileObject(null, fileObject);
423        }
424    
425        private void locationCheck(JavaFileManager.Location location) {
426            if (location instanceof StandardLocation) {
427                StandardLocation stdLoc = (StandardLocation) location;
428                if (!stdLoc.isOutputLocation())
429                    throw new IllegalArgumentException("Resource creation not supported in location " +
430                                                       stdLoc);
431            }
432        }
433    
434        public FileObject getResource(JavaFileManager.Location location,
435                                      CharSequence pkg,
436                                      CharSequence relativeName) throws IOException {
437            String strPkg = pkg.toString();
438            if (strPkg.length() > 0)
439                checkName(strPkg);
440    
441            // TODO: Only support reading resources in selected output
442            // locations?  Only allow reading of non-source, non-class
443            // files from the supported input locations?
444            FileObject fileObject = fileManager.getFileForOutput(location,
445                                                                 pkg.toString(),
446                                                                 relativeName.toString(),
447                                                                 null);
448            // If the path was already opened for writing, throw an exception.
449            checkFileReopening(fileObject, false);
450            return new FilerInputFileObject(fileObject);
451        }
452    
453        private void checkName(String name) throws FilerException {
454            checkName(name, false);
455        }
456    
457        private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException {
458            if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) {
459                if (lint)
460                    log.warning("proc.illegal.file.name", name);
461                throw new FilerException("Illegal name " + name);
462            }
463        }
464    
465        private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) {
466            // Is the name of the form "package-info" or
467            // "foo.bar.package-info"?
468            final String PKG_INFO = "package-info";
469            int periodIndex = name.lastIndexOf(".");
470            if (periodIndex == -1) {
471                return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false;
472            } else {
473                // "foo.bar.package-info." illegal
474                String prefix = name.substring(0, periodIndex);
475                String simple = name.substring(periodIndex+1);
476                return SourceVersion.isName(prefix) && simple.equals(PKG_INFO);
477            }
478        }
479    
480        private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException {
481            // TODO: Check if type already exists on source or class path?
482            // If so, use warning message key proc.type.already.exists
483            checkName(typename, allowUnnamedPackageInfo);
484            if (aggregateGeneratedSourceNames.contains(typename) ||
485                aggregateGeneratedClassNames.contains(typename)) {
486                if (lint)
487                    log.warning("proc.type.recreate", typename);
488                throw new FilerException("Attempt to recreate a file for type " + typename);
489            }
490        }
491    
492        /**
493         * Check to see if the file has already been opened; if so, throw
494         * an exception, otherwise add it to the set of files.
495         */
496        private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException {
497            for(FileObject veteran : fileObjectHistory) {
498                if (fileManager.isSameFile(veteran, fileObject)) {
499                    if (lint)
500                        log.warning("proc.file.reopening", fileObject.getName());
501                    throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());
502                }
503            }
504            if (addToHistory)
505                fileObjectHistory.add(fileObject);
506        }
507    
508        public boolean newFiles() {
509            return (!generatedSourceNames.isEmpty())
510                || (!generatedClasses.isEmpty());
511        }
512    
513        public Set<String> getGeneratedSourceNames() {
514            return generatedSourceNames;
515        }
516    
517        public Set<JavaFileObject> getGeneratedSourceFileObjects() {
518            return generatedSourceFileObjects;
519        }
520    
521        public Map<String, JavaFileObject> getGeneratedClasses() {
522            return generatedClasses;
523        }
524    
525        public void warnIfUnclosedFiles() {
526            if (!openTypeNames.isEmpty())
527                log.warning("proc.unclosed.type.files", openTypeNames.toString());
528        }
529    
530        /**
531         * Update internal state for a new round.
532         */
533        public void newRound(Context context, boolean lastRound) {
534            this.context = context;
535            this.log = Log.instance(context);
536            this.lastRound = lastRound;
537            clearRoundState();
538        }
539    
540        public void close() {
541            clearRoundState();
542            // Cross-round state
543            fileObjectHistory.clear();
544            openTypeNames.clear();
545            aggregateGeneratedSourceNames.clear();
546            aggregateGeneratedClassNames.clear();
547        }
548    
549        private void clearRoundState() {
550            generatedSourceNames.clear();
551            generatedSourceFileObjects.clear();
552            generatedClasses.clear();
553        }
554    
555        /**
556         * Debugging function to display internal state.
557         */
558        public void displayState() {
559            PrintWriter xout = context.get(Log.outKey);
560            xout.println("File Object History : " +  fileObjectHistory);
561            xout.println("Open Type Names     : " +  openTypeNames);
562            xout.println("Gen. Src Names      : " +  generatedSourceNames);
563            xout.println("Gen. Cls Names      : " +  generatedClasses.keySet());
564            xout.println("Agg. Gen. Src Names : " +  aggregateGeneratedSourceNames);
565            xout.println("Agg. Gen. Cls Names : " +  aggregateGeneratedClassNames);
566        }
567    
568        public String toString() {
569            return "javac Filer";
570        }
571    
572        /**
573         * Upon close, register files opened by create{Source, Class}File
574         * for annotation processing.
575         */
576        private void closeFileObject(String typeName, FileObject fileObject) {
577            /*
578             * If typeName is non-null, the file object was opened as a
579             * source or class file by the user.  If a file was opened as
580             * a resource, typeName will be null and the file is *not*
581             * subject to annotation processing.
582             */
583            if ((typeName != null)) {
584                if (!(fileObject instanceof JavaFileObject))
585                    throw new AssertionError("JavaFileOject not found for " + fileObject);
586                JavaFileObject javaFileObject = (JavaFileObject)fileObject;
587                switch(javaFileObject.getKind()) {
588                case SOURCE:
589                    generatedSourceNames.add(typeName);
590                    generatedSourceFileObjects.add(javaFileObject);
591                    openTypeNames.remove(typeName);
592                    break;
593    
594                case CLASS:
595                    generatedClasses.put(typeName, javaFileObject);
596                    openTypeNames.remove(typeName);
597                    break;
598    
599                default:
600                    break;
601                }
602            }
603        }
604    
605    }