KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > apt > pluggable > core > filer > IdeFilerImpl


1 /*******************************************************************************
2  * Copyright (c) 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.apt.pluggable.core.filer;
14
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javax.annotation.processing.Filer;
21 import javax.lang.model.element.Element;
22 import javax.tools.FileObject;
23 import javax.tools.JavaFileObject;
24 import javax.tools.StandardLocation;
25 import javax.tools.JavaFileManager.Location;
26
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.IWorkspace;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedSourceFolderManager;
34 import org.eclipse.jdt.core.JavaModelException;
35 import org.eclipse.jdt.internal.apt.pluggable.core.Apt6Plugin;
36 import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnnotationProcessorManager;
37 import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl;
38
39 /**
40  * Implementation of the Filer interface that is used in IDE mode.
41  * @see org.eclipse.jdt.internal.compiler.apt.dispatch.BatchFilerImpl
42  * @since 3.3
43  */

44 public class IdeFilerImpl implements Filer {
45     
46     //private final IdeAnnotationProcessorManager _dispatchManager;
47
private final IdeProcessingEnvImpl _env;
48
49     public IdeFilerImpl(IdeAnnotationProcessorManager dispatchManager,
50             IdeProcessingEnvImpl env) {
51         //_dispatchManager = dispatchManager;
52
_env = env;
53     }
54
55     /* (non-Javadoc)
56      * @see javax.annotation.processing.Filer#createClassFile(java.lang.CharSequence, javax.lang.model.element.Element[])
57      */

58     @Override JavaDoc
59     public JavaFileObject createClassFile(CharSequence JavaDoc name, Element... originatingElements)
60             throws IOException JavaDoc {
61         //TODO
62
throw new UnsupportedOperationException JavaDoc("Creating class files is not yet implemented"); //$NON-NLS-1$
63
}
64
65     /* (non-Javadoc)
66      * @see javax.annotation.processing.Filer#createResource(javax.tools.JavaFileManager.Location, java.lang.CharSequence, java.lang.CharSequence, javax.lang.model.element.Element[])
67      * In the IDE implementation, we support only two Locations: SOURCE_OUTPUT, which means the APT generated source folder,
68      * and CLASS_OUTPUT, which means the binary output folder associated with the APT generated source folder.
69      */

70     @Override JavaDoc
71     public FileObject createResource(Location location, CharSequence JavaDoc pkg,
72             CharSequence JavaDoc relativeName, Element... originatingElements) throws IOException JavaDoc
73     {
74         // Pre-emptively check parameters here, rather than later on when the resource is written and closed.
75
if (null == location) {
76             throw new IllegalArgumentException JavaDoc("Location is null");
77         }
78         if (!location.isOutputLocation()) {
79             throw new IllegalArgumentException JavaDoc("Location " + location.getName() + " is not an output location");
80         }
81         
82         if (null == pkg) {
83             throw new IllegalArgumentException JavaDoc("Package is null");
84         }
85         if (null == relativeName) {
86             throw new IllegalArgumentException JavaDoc("Relative name is null");
87         }
88         if ( relativeName.length() == 0) {
89             throw new IllegalArgumentException JavaDoc("Relative name is zero length");
90         }
91         IFile file = getOutputFileForLocation(location, pkg, relativeName);
92         
93         //TODO: check whether file has already been generated in this run
94
Set JavaDoc<IFile> parentFiles = new HashSet JavaDoc<IFile>(originatingElements.length);
95         for (Element elem : originatingElements) {
96             IFile enclosing = _env.getEnclosingIFile(elem);
97             if (null != enclosing) {
98                 parentFiles.add(enclosing);
99             }
100         }
101         return new IdeOutputFileObject(_env, file, parentFiles);
102     }
103
104     /**
105      * @param originatingElements should all be source types; binary types (ie elements in jar files)
106      * will be ignored.
107      * @see javax.annotation.processing.Filer#createSourceFile(java.lang.CharSequence, javax.lang.model.element.Element[])
108      */

109     @Override JavaDoc
110     public JavaFileObject createSourceFile(CharSequence JavaDoc name, Element... originatingElements)
111             throws IOException JavaDoc
112     {
113         // Pre-emptively check parameters here, rather than later on when the resource is written and closed.
114
if (null == name) {
115             throw new IllegalArgumentException JavaDoc("Name is null");
116         }
117         //TODO: check whether file has already been generated in this run
118
Set JavaDoc<IFile> parentFiles = new HashSet JavaDoc<IFile>(originatingElements.length);
119         for (Element elem : originatingElements) {
120             IFile enclosing = _env.getEnclosingIFile(elem);
121             if (null != enclosing) {
122                 parentFiles.add(enclosing);
123             }
124         }
125         return new IdeOutputJavaFileObject(_env, name, parentFiles);
126     }
127
128     /* (non-Javadoc)
129      * @see javax.annotation.processing.Filer#getResource(javax.tools.JavaFileManager.Location, java.lang.CharSequence, java.lang.CharSequence)
130      */

131     @Override JavaDoc
132     public FileObject getResource(Location location, CharSequence JavaDoc pkg, CharSequence JavaDoc relativeName)
133             throws IOException JavaDoc {
134         //TODO
135
throw new UnsupportedOperationException JavaDoc("Reading resource files is not yet implemented"); //$NON-NLS-1$
136
}
137
138     /**
139      * Return a project-relative path. This does not create the file nor its parent directories,
140      * but it does validate the path.
141      * @param pkg must be non-null but can be empty.
142      * @param relPath must be non-null and non-empty.
143      * @throws IOException if the path is not valid.
144      */

145     protected IFile getOutputFileForLocation( Location loc, CharSequence JavaDoc pkg, CharSequence JavaDoc relPath )
146         throws IOException JavaDoc
147     {
148         GeneratedSourceFolderManager gsfm = _env.getAptProject().getGeneratedSourceFolderManager();
149         IPath path = null;
150         if ( loc == StandardLocation.CLASS_OUTPUT )
151         {
152             try
153             {
154                 path = gsfm.getBinaryOutputLocation();
155             }
156             catch ( JavaModelException e )
157             {
158                 Apt6Plugin.log(e, "Failure getting the binary output location"); //$NON-NLS-1$
159
IOException JavaDoc ioe = new IOException JavaDoc();
160                 ioe.initCause(e);
161                 throw ioe;
162             }
163         }
164         else if ( loc == StandardLocation.SOURCE_OUTPUT ) {
165             path = gsfm.getFolder().getProjectRelativePath();
166         }
167         else {
168             throw new IllegalArgumentException JavaDoc("Unsupported location: " + loc);
169         }
170         
171         if( pkg.length() > 0 )
172             path = path.append(pkg.toString().replace('.', File.separatorChar) );
173
174         path = path.append(relPath.toString());
175         
176         IFile file = _env.getProject().getFile(path);
177
178         validatePath(file);
179         
180         return file;
181     }
182     
183     /**
184      * Validate that a path fits the rules for being created.
185      * @see IWorkspace#validatePath()
186      * @throws IOException
187      */

188     private void validatePath(IFile file) throws IOException JavaDoc
189     {
190         IStatus status = _env.getProject().getWorkspace().validatePath(file.getFullPath().toOSString(), IResource.FILE);
191         if (!status.isOK()) {
192             CoreException ce = new CoreException(status);
193             IOException JavaDoc ioe = new IOException JavaDoc("Invalid path: " + file.toString()); //$NON-NLS-1$
194
ioe.initCause(ce);
195             throw ioe;
196         }
197     }
198
199 }
200
Popular Tags