1 package spoon.processing; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.lang.reflect.Field ; 6 import java.lang.reflect.Method ; 7 import java.util.HashSet ; 8 import java.util.Set ; 9 10 import spoon.reflect.Factory; 11 import spoon.reflect.declaration.CtClass; 12 import spoon.reflect.declaration.CtElement; 13 import spoon.support.util.RtHelper; 14 15 19 public abstract class AbstractProcessor<E extends CtElement> implements 20 Processor<E> { 21 22 Factory factory; 23 24 Set <Class <? extends CtElement>> processedElementTypes = new HashSet <Class <? extends CtElement>>(); 25 26 private ProcessorProperties props; 27 28 31 @SuppressWarnings ("unchecked") 32 public AbstractProcessor() { 33 for (Method m : getClass().getMethods()) { 34 if (m.getName().equals("process") 35 && m.getParameterTypes().length == 1) { 36 Class c = m.getParameterTypes()[0]; 37 if (CtElement.class != c) { 38 addProcessedElementType((Class <? extends CtElement>) m 39 .getParameterTypes()[0]); 40 } 41 } 42 } 43 if (processedElementTypes.isEmpty()) { 44 addProcessedElementType(CtElement.class); 45 } 46 } 47 48 52 protected void addProcessedElementType( 53 Class <? extends CtElement> elementType) { 54 processedElementTypes.add(elementType); 55 } 56 57 60 protected void clearProcessedElementType() { 61 processedElementTypes.clear(); 62 } 63 64 public Environment getEnvironment() { 65 return getFactory().getEnvironment(); 66 } 67 68 public Factory getFactory() { 69 return this.factory; 70 } 71 72 public Set <Class <? extends CtElement>> getProcessedElementTypes() { 73 return processedElementTypes; 74 } 75 76 private ProcessorProperties getProps() { 77 if (props == null) { 78 try { 79 props = getFactory().getEnvironment().getProcessorProperties( 80 getClass().getName()); 81 } catch (FileNotFoundException e) { 82 getFactory().getEnvironment().report( 83 this, 84 Severity.MESSAGE, 85 "Property file not found for processor " 86 + getClass().getName()); 87 } catch (IOException e) { 88 getFactory().getEnvironment().report( 89 this, 90 Severity.ERROR, 91 "Wrong properties file format for processor " 92 + getClass().getName()); 93 } 94 } 95 return props; 96 } 97 98 public TraversalStrategy getTraversalStrategy() { 99 return TraversalStrategy.PRE_ORDER; 100 } 101 102 public void init() { 103 loadProperties(); 104 } 105 106 public boolean isToBeProcessed(E candidate) { 107 if (candidate instanceof CtClass) { 108 if (factory.Template().getAll().containsKey( 109 ((CtClass) candidate).getQualifiedName())) { 110 return false; 111 } 112 } 113 return true; 114 } 115 116 private void loadProperties() { 117 if (getProps() != null) { 118 for (Field f : RtHelper.getAllFields(getClass())) { 119 if (f.isAnnotationPresent(Property.class)) { 120 Object obj = getProps().get(f.getType(), f.getName()); 121 if (obj != null) { 122 f.setAccessible(true); 123 try { 124 f.set(this, obj); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 } else { 129 getFactory().getEnvironment().report( 130 this, 131 Severity.WARNING, 132 "No value found for property " + f.getName() 133 + " in processor " 134 + getClass().getName()); 135 } 136 } 137 } 138 } 139 } 140 141 145 public final void process() { 146 } 147 148 public void processingDone() { 149 } 151 152 155 protected void removeProcessedElementType( 156 Class <? extends CtElement> elementType) { 157 processedElementTypes.remove(elementType); 158 } 159 160 public final void setFactory(Factory factory) { 161 this.factory = factory; 162 } 163 164 } 165 | Popular Tags |