KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > dispatch > BatchFilerImpl


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * wharley@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.compiler.apt.dispatch;
14
15 import java.io.IOException JavaDoc;
16 import java.net.URI JavaDoc;
17 import java.util.HashSet JavaDoc;
18
19 import javax.annotation.processing.Filer;
20 import javax.annotation.processing.FilerException;
21 import javax.lang.model.element.Element;
22 import javax.tools.FileObject;
23 import javax.tools.JavaFileManager;
24 import javax.tools.JavaFileObject;
25 import javax.tools.StandardLocation;
26 import javax.tools.JavaFileManager.Location;
27
28 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
29 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
30
31 /**
32  * Implementation of Filer used when compilation is driven by command line
33  * or by Tool interface. This version does not need to keep track of
34  * dependencies.
35  */

36 public class BatchFilerImpl implements Filer {
37     
38     protected final BaseAnnotationProcessorManager _dispatchManager;
39     protected final BaseProcessingEnvImpl _env;
40     protected final JavaFileManager _fileManager;
41     protected final HashSet JavaDoc<URI JavaDoc> _createdFiles;
42
43     public BatchFilerImpl(BaseAnnotationProcessorManager dispatchManager, BatchProcessingEnvImpl env)
44     {
45         _dispatchManager = dispatchManager;
46         _fileManager = env._fileManager;
47         _env = env;
48         _createdFiles = new HashSet JavaDoc<URI JavaDoc>();
49     }
50
51     public void addNewUnit(ICompilationUnit unit) {
52         _env.addNewUnit(unit);
53     }
54     
55     public void addNewClassFile(ReferenceBinding binding) {
56         _env.addNewClassFile(binding);
57     }
58
59     /* (non-Javadoc)
60      * @see javax.annotation.processing.Filer#createClassFile(java.lang.CharSequence, javax.lang.model.element.Element[])
61      */

62     @Override JavaDoc
63     public JavaFileObject createClassFile(CharSequence JavaDoc name,
64             Element... originatingElements) throws IOException JavaDoc
65     {
66         //TODO: do we need to check validity of 'name', or can we trust the filemanager to handle that?
67
JavaFileObject jfo = _fileManager.getJavaFileForOutput(
68                 StandardLocation.CLASS_OUTPUT, name.toString(), JavaFileObject.Kind.CLASS, null);
69         URI JavaDoc uri = jfo.toUri();
70         if (_createdFiles.contains(uri)) {
71             throw new FilerException("Class file already created : " + name); //$NON-NLS-1$
72
}
73
74         _createdFiles.add(uri);
75         return new HookedJavaFileObject(jfo, jfo.getName(), this);
76     }
77
78     /* (non-Javadoc)
79      * @see javax.annotation.processing.Filer#createResource(javax.tools.JavaFileManager.Location, java.lang.CharSequence, java.lang.CharSequence, javax.lang.model.element.Element[])
80      */

81     @Override JavaDoc
82     public FileObject createResource(Location location, CharSequence JavaDoc pkg,
83             CharSequence JavaDoc relativeName, Element... originatingElements)
84             throws IOException JavaDoc {
85         //TODO: do we need to check validity of 'name', or can we trust the filemanager to handle that?
86
FileObject fo = _fileManager.getFileForOutput(
87                 location, pkg.toString(), relativeName.toString(), null);
88         URI JavaDoc uri = fo.toUri();
89         if (_createdFiles.contains(uri)) {
90             throw new FilerException("Resource already created : " + location + '/' + pkg + '/' + relativeName); //$NON-NLS-1$
91
}
92
93         _createdFiles.add(uri);
94         return fo;
95     }
96
97     /* (non-Javadoc)
98      * @see javax.annotation.processing.Filer#createSourceFile(java.lang.CharSequence, javax.lang.model.element.Element[])
99      */

100     @Override JavaDoc
101     public JavaFileObject createSourceFile(CharSequence JavaDoc name,
102             Element... originatingElements) throws IOException JavaDoc {
103         //TODO: do we need to check validity of 'name', or can we trust the filemanager to handle that?
104
JavaFileObject jfo = _fileManager.getJavaFileForOutput(
105                 StandardLocation.SOURCE_OUTPUT, name.toString(), JavaFileObject.Kind.SOURCE, null);
106         URI JavaDoc uri = jfo.toUri();
107         if (_createdFiles.contains(uri)) {
108             throw new FilerException("Source file already created : " + name); //$NON-NLS-1$
109
}
110
111         _createdFiles.add(uri);
112         // hook the file object's writers to create compilation unit and add to addedUnits()
113
return new HookedJavaFileObject(jfo, jfo.getName(), this);
114     }
115     
116     /* (non-Javadoc)
117      * @see javax.annotation.processing.Filer#getResource(javax.tools.JavaFileManager.Location, java.lang.CharSequence, java.lang.CharSequence)
118      */

119     @Override JavaDoc
120     public FileObject getResource(Location location, CharSequence JavaDoc pkg,
121             CharSequence JavaDoc relativeName) throws IOException JavaDoc {
122         //TODO: do we need to check validity of 'name', or can we trust the filemanager to handle that?
123
FileObject fo = _fileManager.getFileForInput(
124                 location, pkg.toString(), relativeName.toString());
125         URI JavaDoc uri = fo.toUri();
126         if (_createdFiles.contains(uri)) {
127             throw new FilerException("Resource already created : " + location + '/' + pkg + '/' + relativeName); //$NON-NLS-1$
128
}
129
130         _createdFiles.add(uri);
131         return fo;
132     }
133
134 }
135
Popular Tags