1 12 13 package org.eclipse.jdt.internal.apt.pluggable.core.filer; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.util.HashSet ; 18 import java.util.Set ; 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 44 public class IdeFilerImpl implements Filer { 45 46 private final IdeProcessingEnvImpl _env; 48 49 public IdeFilerImpl(IdeAnnotationProcessorManager dispatchManager, 50 IdeProcessingEnvImpl env) { 51 _env = env; 53 } 54 55 58 @Override 59 public JavaFileObject createClassFile(CharSequence name, Element... originatingElements) 60 throws IOException { 61 throw new UnsupportedOperationException ("Creating class files is not yet implemented"); } 64 65 70 @Override 71 public FileObject createResource(Location location, CharSequence pkg, 72 CharSequence relativeName, Element... originatingElements) throws IOException 73 { 74 if (null == location) { 76 throw new IllegalArgumentException ("Location is null"); 77 } 78 if (!location.isOutputLocation()) { 79 throw new IllegalArgumentException ("Location " + location.getName() + " is not an output location"); 80 } 81 82 if (null == pkg) { 83 throw new IllegalArgumentException ("Package is null"); 84 } 85 if (null == relativeName) { 86 throw new IllegalArgumentException ("Relative name is null"); 87 } 88 if ( relativeName.length() == 0) { 89 throw new IllegalArgumentException ("Relative name is zero length"); 90 } 91 IFile file = getOutputFileForLocation(location, pkg, relativeName); 92 93 Set <IFile> parentFiles = new HashSet <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 109 @Override 110 public JavaFileObject createSourceFile(CharSequence name, Element... originatingElements) 111 throws IOException 112 { 113 if (null == name) { 115 throw new IllegalArgumentException ("Name is null"); 116 } 117 Set <IFile> parentFiles = new HashSet <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 131 @Override 132 public FileObject getResource(Location location, CharSequence pkg, CharSequence relativeName) 133 throws IOException { 134 throw new UnsupportedOperationException ("Reading resource files is not yet implemented"); } 137 138 145 protected IFile getOutputFileForLocation( Location loc, CharSequence pkg, CharSequence relPath ) 146 throws IOException 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"); IOException ioe = new IOException (); 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 ("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 188 private void validatePath(IFile file) throws IOException 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 ioe = new IOException ("Invalid path: " + file.toString()); ioe.initCause(ce); 195 throw ioe; 196 } 197 } 198 199 } 200 | Popular Tags |