1 12 13 package org.eclipse.jdt.apt.core.internal; 14 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.LinkedHashSet ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IMarker; 24 import org.eclipse.core.resources.IProject; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.IResourceChangeEvent; 27 import org.eclipse.core.resources.IResourceChangeListener; 28 import org.eclipse.core.resources.ResourcesPlugin; 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.jdt.apt.core.internal.util.FactoryPath; 31 import org.eclipse.jdt.apt.core.util.AptConfig; 32 import org.eclipse.jdt.core.ICompilationUnit; 33 import org.eclipse.jdt.core.IJavaProject; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.compiler.BuildContext; 36 import org.eclipse.jdt.core.compiler.CategorizedProblem; 37 import org.eclipse.jdt.core.compiler.CompilationParticipant; 38 import org.eclipse.jdt.core.compiler.ReconcileContext; 39 40 import com.sun.mirror.apt.AnnotationProcessorFactory; 41 42 46 public class AptCompilationParticipant extends CompilationParticipant 47 { 48 54 private Set <AnnotationProcessorFactory> _previousRoundsBatchFactories = new LinkedHashSet <AnnotationProcessorFactory>(); 55 private int _buildRound = 0; 56 private boolean _isBatch = false; 57 private static AptCompilationParticipant INSTANCE; 58 64 private Map <IFile, CategorizedProblem[]> _processedFiles = null; 65 66 77 private HashSet <IFile> _java6GeneratedFiles = null; 78 79 public static AptCompilationParticipant getInstance() { 80 return INSTANCE; 81 } 82 83 88 public AptCompilationParticipant() 89 { 90 INSTANCE = this; 91 92 IResourceChangeListener listener = new IResourceChangeListener() { 95 public void resourceChanged(IResourceChangeEvent event) { 96 buildComplete(); 97 } 98 }; 99 ResourcesPlugin.getWorkspace().addResourceChangeListener(listener, IResourceChangeEvent.POST_BUILD); 100 } 101 102 public boolean isAnnotationProcessor(){ 103 return true; 104 } 105 106 public void buildStarting(BuildContext[] files, boolean isBatch){ 107 if( _buildRound == 0 ) 113 _isBatch = isBatch; 114 } 115 116 public void processAnnotations(BuildContext[] allfiles) { 117 final int total = allfiles == null ? 0 : allfiles.length; 120 if( total == 0 ) 121 return; 122 123 final IProject project = allfiles[0].getFile().getProject(); 124 final IJavaProject javaProject = JavaCore.create(project); 125 String javaVersion = javaProject.getOption("org.eclipse.jdt.core.compiler.source", true); if ("1.3".equals(javaVersion) || "1.4".equals(javaVersion)) { return; 131 } 132 133 if ( _isBatch && _buildRound == 0 ) { 134 AnnotationProcessorFactoryLoader.getLoader().resetBatchProcessors(javaProject); 135 _previousRoundsBatchFactories.clear(); 136 } 137 138 try { 139 140 int annoFileCount = 0; 143 int noAnnoFileCount = 0; 144 for( int i=0; i<total; i++ ){ 145 BuildContext bc = allfiles[i]; 146 if( _buildRound > 0 && _processedFiles.containsKey( bc.getFile() )){ 147 CategorizedProblem[] problems = _processedFiles.get(bc.getFile()); 151 if (null != problems && problems.length > 0) { 152 bc.recordNewProblems(problems); 153 } 154 continue; 155 } 156 if( bc.hasAnnotations() ) 157 annoFileCount ++; 158 else 159 noAnnoFileCount ++; 160 } 161 if( annoFileCount == 0 && noAnnoFileCount == 0 ) 165 return; 166 167 BuildContext[] withAnnotation = null; 168 BuildContext[] withoutAnnotation = null; 169 170 if( annoFileCount != 0 ) 171 withAnnotation = new BuildContext[annoFileCount]; 172 if(noAnnoFileCount != 0 ) 173 withoutAnnotation = new BuildContext[noAnnoFileCount]; 174 int wIndex = 0; int woIndex = 0; for( int i=0; i<total; i++ ){ 177 if( _processedFiles.containsKey( allfiles[i].getFile() ) ) 178 continue; 179 if( allfiles[i].hasAnnotations() ) 180 withAnnotation[wIndex ++] = allfiles[i]; 181 else 182 withoutAnnotation[woIndex ++] = allfiles[i]; 183 } 184 185 for( BuildContext file : allfiles ) 186 _processedFiles.put(file.getFile(), null); 187 188 Map <AnnotationProcessorFactory, FactoryPath.Attributes> factories = 189 AnnotationProcessorFactoryLoader.getLoader().getJava5FactoriesAndAttributesForProject(javaProject); 190 191 AptProject aptProject = AptPlugin.getAptProject(javaProject); 192 Set <AnnotationProcessorFactory> dispatchedBatchFactories = 193 APTDispatchRunnable.runAPTDuringBuild( 194 withAnnotation, 195 withoutAnnotation, 196 _processedFiles, 197 aptProject, 198 factories, 199 _previousRoundsBatchFactories, 200 _isBatch); 201 _previousRoundsBatchFactories.addAll(dispatchedBatchFactories); 202 } 203 finally { 204 _buildRound ++; 205 } 206 } 207 208 public void reconcile(ReconcileContext context){ 209 final ICompilationUnit workingCopy = context.getWorkingCopy(); 210 if( workingCopy == null ) 211 return; 212 IJavaProject javaProject = workingCopy.getJavaProject(); 213 if( javaProject == null ) 214 return; 215 if (!AptConfig.shouldProcessDuringReconcile(javaProject)) { 216 AptPlugin.trace("Reconcile-time processing is disabled for project: " + javaProject.getElementName()); return; 218 } 219 AptProject aptProject = AptPlugin.getAptProject(javaProject); 220 221 Map <AnnotationProcessorFactory, FactoryPath.Attributes> factories = 222 AnnotationProcessorFactoryLoader.getLoader().getJava5FactoriesAndAttributesForProject( javaProject ); 223 APTDispatchRunnable.runAPTDuringReconcile(context, aptProject, factories); 224 } 225 226 public void cleanStarting(IJavaProject javaProject){ 227 IProject p = javaProject.getProject(); 228 229 AptPlugin.getAptProject(javaProject).projectClean( true ); 230 try{ 231 IMarker[] markers = p.findMarkers(AptPlugin.APT_BATCH_PROCESSOR_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE); 233 if( markers != null ){ 234 for( IMarker marker : markers ) 235 marker.delete(); 236 } 237 } 238 catch(CoreException e){ 239 AptPlugin.log(e, "Unable to delete batch annotation processor markers"); } 241 } 242 243 248 public boolean isActive(IJavaProject project){ 249 return AptConfig.isEnabled(project); 250 } 251 252 public int aboutToBuild(IJavaProject project) { 253 if (AptConfig.isEnabled(project)) { 254 AptPlugin.getAptProject(project).compilationStarted(); 256 } 257 _buildRound = 0; _processedFiles = new HashMap <IFile, CategorizedProblem[]>(); 260 _java6GeneratedFiles = new HashSet <IFile>(); 261 return CompilationParticipant.READY_FOR_BUILD; 263 } 264 265 271 public void addJava6GeneratedFile(IFile file) { 272 _java6GeneratedFiles.add(file); 273 } 274 275 280 public Set <IFile> getJava6GeneratedFiles() { 281 if (null == _java6GeneratedFiles) { 282 return Collections.emptySet(); 283 } 284 return Collections.unmodifiableSet(_java6GeneratedFiles); 285 } 286 287 private void buildComplete() { 288 _processedFiles = null; 289 _java6GeneratedFiles = null; 290 } 291 } 292 | Popular Tags |