1 package spoon.processing; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.lang.reflect.Field ; 6 import java.util.Set ; 7 8 import spoon.reflect.Factory; 9 import spoon.reflect.declaration.CtElement; 10 import spoon.support.util.RtHelper; 11 12 17 public abstract class AbstractManualProcessor implements Processor<CtElement> { 18 19 Factory factory; 20 21 private ProcessorProperties props; 22 23 26 @SuppressWarnings ("unchecked") 27 public AbstractManualProcessor() { 28 } 29 30 33 protected void addProcessedElementType( 34 Class <? extends CtElement> elementType) { 35 } 36 37 public Environment getEnvironment() { 38 return getFactory().getEnvironment(); 39 } 40 41 final public Factory getFactory() { 42 return this.factory; 43 } 44 45 48 public final Set <Class <? extends CtElement>> getProcessedElementTypes() { 49 return null; 50 } 51 52 private ProcessorProperties getProps() { 53 if (props == null) { 54 try { 55 props = getFactory().getEnvironment().getProcessorProperties( 56 getClass().getName()); 57 } catch (FileNotFoundException e) { 58 getFactory().getEnvironment().report( 59 this, 60 Severity.MESSAGE, 61 "Property file not found for processor " 62 + getClass().getName()); 63 } catch (IOException e) { 64 getFactory().getEnvironment().report( 65 this, 66 Severity.ERROR, 67 "Wrong properties file format for processor " 68 + getClass().getName()); 69 } 70 } 71 return props; 72 } 73 74 77 public final TraversalStrategy getTraversalStrategy() { 78 return TraversalStrategy.PRE_ORDER; 79 } 80 81 public void init() { 82 loadProperties(); 83 } 84 85 88 public final boolean isPrivileged() { 89 return false; 90 } 91 92 95 public final boolean isToBeProcessed(CtElement candidate) { 96 return false; 97 } 98 99 private void loadProperties() { 100 if (getProps() != null) { 101 for (Field f : RtHelper.getAllFields(getClass())) { 102 if (f.isAnnotationPresent(Property.class)) { 103 Object obj = getProps().get(f.getType(), f.getName()); 104 if (obj != null) { 105 f.setAccessible(true); 106 try { 107 f.set(this, obj); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } else { 112 getFactory().getEnvironment().report( 113 this, 114 Severity.WARNING, 115 "No value found for property " + f.getName() 116 + " in processor " 117 + getClass().getName()); 118 } 119 } 120 } 121 } 122 } 123 124 127 public final void process(CtElement element) { 128 } 129 130 public void processingDone() { 131 } 133 134 public final void setFactory(Factory factory) { 135 this.factory = factory; 136 } 137 138 } | Popular Tags |