1 package spoon.support; 2 3 import java.util.Collection ; 4 import java.util.LinkedList ; 5 import java.util.List ; 6 7 import spoon.processing.ProcessingManager; 8 import spoon.processing.Processor; 9 import spoon.processing.Severity; 10 import spoon.reflect.Factory; 11 import spoon.reflect.declaration.CtElement; 12 import spoon.reflect.declaration.CtNamedElement; 13 import spoon.support.util.Timer; 14 import spoon.support.visitor.ProcessingVisitor; 15 16 21 public class RuntimeProcessingManager implements ProcessingManager { 22 Processor<?> current; 23 24 Factory factory; 25 26 List <Processor> processors; 27 28 ProcessingVisitor visitor; 29 30 38 public RuntimeProcessingManager(Factory factory) { 39 super(); 40 setFactory(factory); 41 } 42 43 public void addProcessor(Class <? extends Processor> type) { 44 try { 45 Processor<?> p = type.newInstance(); 46 p.setFactory(factory); 47 p.init(); 48 addProcessor(p); 49 } catch (Exception e) { 50 factory 51 .getEnvironment() 52 .report( 53 null, 54 Severity.ERROR, 55 "Unable to instantiate processor \"" 56 + type.getName() 57 + "\" - Your processor should have a constructor with no arguments"); 58 } 59 } 60 61 public boolean addProcessor(Processor p) { 62 p.setFactory(getFactory()); 63 return getProcessors().add(p); 64 } 65 66 @SuppressWarnings ("unchecked") 67 public void addProcessor(String qualifiedName) { 68 try { 69 addProcessor((Class <? extends Processor>) Class 70 .forName(qualifiedName)); 71 } catch (ClassNotFoundException e) { 72 factory.getEnvironment().report( 73 null, 74 Severity.ERROR, 75 "Unable to load processor \"" + qualifiedName 76 + "\" - Check your classpath"); 77 } 78 } 79 80 public Processor<?> getCurrentProcessor() { 81 return current; 82 } 83 84 public Factory getFactory() { 85 return factory; 86 } 87 88 public List <Processor> getProcessors() { 89 if (processors == null) { 90 processors = new LinkedList <Processor>(); 91 } 92 return processors; 93 } 94 95 private ProcessingVisitor getVisitor() { 96 if (visitor == null) 97 visitor = new ProcessingVisitor(getFactory()); 98 return visitor; 99 } 100 101 public boolean isToBeApplied(Class <? extends Processor> type) { 102 for (Processor p : getProcessors()) { 103 if (p.getClass() == type) { 104 return true; 105 } 106 } 107 return false; 108 } 109 110 public void process() { 111 Timer.start("process"); 112 process(getFactory().Package().getAllRoots()); 113 Timer.stop("process"); 114 } 115 116 public void process(Collection <? extends CtElement> elements) { 117 for (Processor p : getProcessors()) { 118 current = p; 119 process(elements, p); 120 } 121 } 122 123 126 public void process(Collection <? extends CtElement> elements, 127 Processor processor) { 128 getFactory().getEnvironment().debugMessage( 129 "Processing with " + processor.getClass().getName()); 130 current = processor; 131 Timer.start(processor.getClass().getName()); 132 for (CtElement e : elements) 133 process(e, processor); 134 Timer.stop(processor.getClass().getName()); 135 } 136 137 public void process(CtElement element) { 138 for (Processor p : getProcessors()) { 139 current = p; 140 process(element, p); 141 } 142 } 143 144 147 public void process(CtElement element, Processor<?> processor) { 148 getFactory() 149 .getEnvironment() 150 .debugMessage( 151 "Processing " 152 + ((element instanceof CtNamedElement) ? ((CtNamedElement) element) 153 .getSimpleName() 154 : element.toString()) + " with " 155 + processor.getClass().getName()); 156 processor.init(); 157 getVisitor().setProcessor(processor); 158 getVisitor().scan(element); 159 processor.processingDone(); 160 } 161 162 public void setFactory(Factory factory) { 163 this.factory = factory; 164 factory.getEnvironment().setManager(this); 165 } 166 167 } 168 | Popular Tags |