1 7 8 package javax.annotation.processing; 9 10 import java.util.Set ; 11 import java.util.HashSet ; 12 import java.util.Collections ; 13 import javax.lang.model.element.*; 14 import javax.lang.model.SourceVersion; 15 import javax.tools.Diagnostic; 16 17 42 public abstract class AbstractProcessor implements Processor { 43 46 protected ProcessingEnvironment processingEnv; 47 private boolean initialized = false; 48 49 52 protected AbstractProcessor() {} 53 54 63 public Set <String > getSupportedOptions() { 64 SupportedOptions so = this.getClass().getAnnotation(SupportedOptions.class); 65 if (so == null) 66 return Collections.emptySet(); 67 else 68 return arrayToSet(so.value()); 69 } 70 71 80 public Set <String > getSupportedAnnotationTypes() { 81 SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class); 82 if (sat == null) { 83 if (isInitialized()) 84 processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, 85 "No SupportedAnnotationTypes annotation " + 86 "found on " + this.getClass().getName() + 87 ", returning an empty set."); 88 return Collections.emptySet(); 89 } 90 else 91 return arrayToSet(sat.value()); 92 } 93 94 102 public SourceVersion getSupportedSourceVersion() { 103 SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class); 104 SourceVersion sv = null; 105 if (ssv == null) { 106 sv = SourceVersion.RELEASE_6; 107 if (isInitialized()) 108 processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, 109 "No SupportedSourceVersion annotation " + 110 "found on " + this.getClass().getName() + 111 ", returning " + sv + "."); 112 } else 113 sv = ssv.value(); 114 return sv; 115 } 116 117 118 129 public synchronized void init(ProcessingEnvironment processingEnv) { 130 if (initialized) 131 throw new IllegalStateException ("Cannot call init more than once."); 132 if (processingEnv == null) 133 throw new NullPointerException ("Tool provided null ProcessingEnvironment"); 134 135 this.processingEnv = processingEnv; 136 initialized = true; 137 } 138 139 142 public abstract boolean process(Set <? extends TypeElement> annotations, 143 RoundEnvironment roundEnv); 144 145 153 public Iterable <? extends Completion> getCompletions(Element element, 154 AnnotationMirror annotation, 155 ExecutableElement member, 156 String userText) { 157 return Collections.emptyList(); 158 } 159 160 167 protected synchronized boolean isInitialized() { 168 return initialized; 169 } 170 171 private static Set <String > arrayToSet(String [] array) { 172 assert array != null; 173 Set <String > set = new HashSet <String >(array.length); 174 for (String s : array) 175 set.add(s); 176 return Collections.unmodifiableSet(set); 177 } 178 } 179 | Popular Tags |