1 12 13 package org.eclipse.jdt.internal.compiler.apt.dispatch; 14 15 import java.io.PrintWriter ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import javax.annotation.processing.RoundEnvironment; 22 import javax.lang.model.element.Element; 23 import javax.lang.model.element.TypeElement; 24 25 28 public class RoundDispatcher { 29 30 private final Set <TypeElement> _unclaimedAnnotations; 31 private final RoundEnvironment _roundEnv; 32 private final IProcessorProvider _provider; 33 private boolean _searchForStar = false; 34 private final PrintWriter _traceProcessorInfo; 35 private final PrintWriter _traceRounds; 36 37 41 private final List <ProcessorInfo> _processors; 42 43 51 public RoundDispatcher( 52 IProcessorProvider provider, 53 RoundEnvironment env, 54 Set <TypeElement> rootAnnotations, 55 PrintWriter traceProcessorInfo, 56 PrintWriter traceRounds) 57 { 58 _provider = provider; 59 _processors = provider.getDiscoveredProcessors(); 60 _roundEnv = env; 61 _unclaimedAnnotations = new HashSet <TypeElement>(rootAnnotations); 62 _traceProcessorInfo = traceProcessorInfo; 63 _traceRounds = traceRounds; 64 } 65 66 69 public void round() 70 { 71 if (null != _traceRounds) { 72 StringBuilder sbElements = new StringBuilder (); 73 sbElements.append("\tinput files: {"); Iterator <? extends Element> iElements = _roundEnv.getRootElements().iterator(); 75 boolean hasNext = iElements.hasNext(); 76 while (hasNext) { 77 sbElements.append(iElements.next()); 78 hasNext = iElements.hasNext(); 79 if (hasNext) { 80 sbElements.append(','); 81 } 82 } 83 sbElements.append('}'); 84 _traceRounds.println(sbElements.toString()); 85 86 StringBuilder sbAnnots = new StringBuilder (); 87 sbAnnots.append("\tannotations: ["); Iterator <TypeElement> iAnnots = _unclaimedAnnotations.iterator(); 89 hasNext = iAnnots.hasNext(); 90 while (hasNext) { 91 sbAnnots.append(iAnnots.next()); 92 hasNext = iAnnots.hasNext(); 93 if (hasNext) { 94 sbAnnots.append(','); 95 } 96 } 97 sbAnnots.append(']'); 98 _traceRounds.println(sbAnnots.toString()); 99 100 _traceRounds.println("\tlast round: " + _roundEnv.processingOver()); } 102 103 _searchForStar = _unclaimedAnnotations.isEmpty(); 105 106 for (ProcessorInfo pi : _processors) { 110 handleProcessor(pi); 111 } 112 113 while (_searchForStar || !_unclaimedAnnotations.isEmpty()) { 116 ProcessorInfo pi = _provider.discoverNextProcessor(); 117 if (null == pi) { 118 break; 120 } 121 handleProcessor(pi); 122 } 123 124 } 126 127 132 private void handleProcessor(ProcessorInfo pi) 133 { 134 try { 135 Set <TypeElement> annotationsToProcess = new HashSet <TypeElement>(); 136 boolean shouldCall = pi.computeSupportedAnnotations( 137 _unclaimedAnnotations, annotationsToProcess); 138 if (shouldCall) { 139 boolean claimed = pi._processor.process(annotationsToProcess, _roundEnv); 140 if (null != _traceProcessorInfo && !_roundEnv.processingOver()) { 141 StringBuilder sb = new StringBuilder (); 142 sb.append("Processor "); sb.append(pi._processor.getClass().getName()); 144 sb.append(" matches ["); Iterator <TypeElement> i = annotationsToProcess.iterator(); 146 boolean hasNext = i.hasNext(); 147 while (hasNext) { 148 sb.append(i.next()); 149 hasNext = i.hasNext(); 150 if (hasNext) { 151 sb.append(' '); 152 } 153 } 154 sb.append("] and returns "); sb.append(claimed); 156 _traceProcessorInfo.println(sb.toString()); 157 } 158 if (claimed) { 159 _unclaimedAnnotations.removeAll(annotationsToProcess); 161 if (pi.supportsStar()) { 162 _searchForStar = false; 163 } 164 } 165 } 166 } catch (Exception e) { 167 _provider.reportProcessorException(pi._processor, e); 170 } 171 } 172 173 } 174 | Popular Tags |