You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExtensionAnnotationProcessor.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2013 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.processor;
  17. import org.pf4j.Extension;
  18. import org.pf4j.ExtensionPoint;
  19. import javax.annotation.processing.AbstractProcessor;
  20. import javax.annotation.processing.ProcessingEnvironment;
  21. import javax.annotation.processing.RoundEnvironment;
  22. import javax.lang.model.SourceVersion;
  23. import javax.lang.model.element.Element;
  24. import javax.lang.model.element.TypeElement;
  25. import javax.lang.model.type.DeclaredType;
  26. import javax.lang.model.type.TypeKind;
  27. import javax.lang.model.type.TypeMirror;
  28. import javax.tools.Diagnostic;
  29. import java.lang.reflect.Constructor;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.HashSet;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import java.util.TreeSet;
  37. /**
  38. * @author Decebal Suiu
  39. */
  40. public class ExtensionAnnotationProcessor extends AbstractProcessor {
  41. private static final String STORAGE_CLASS_NAME = "pf4j.storageClassName";
  42. private Map<String, Set<String>> extensions = new HashMap<>(); // the key is the extension point
  43. private Map<String, Set<String>> oldExtensions = new HashMap<>(); // the key is the extension point
  44. private ExtensionStorage storage;
  45. @Override
  46. public synchronized void init(ProcessingEnvironment processingEnv) {
  47. super.init(processingEnv);
  48. info("%s init", ExtensionAnnotationProcessor.class);
  49. storage = createStorage();
  50. }
  51. @Override
  52. public SourceVersion getSupportedSourceVersion() {
  53. return SourceVersion.latest();
  54. }
  55. @Override
  56. public Set<String> getSupportedAnnotationTypes() {
  57. Set<String> annotationTypes = new HashSet<>();
  58. annotationTypes.add(Extension.class.getName());
  59. return annotationTypes;
  60. }
  61. @Override
  62. public Set<String> getSupportedOptions() {
  63. Set<String> options = new HashSet<>();
  64. options.add(STORAGE_CLASS_NAME);
  65. return options;
  66. }
  67. @Override
  68. public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  69. if (roundEnv.processingOver()) {
  70. return false;
  71. }
  72. info("Processing @%s", Extension.class);
  73. for (Element element : roundEnv.getElementsAnnotatedWith(Extension.class)) {
  74. // check if @Extension is put on class and not on method or constructor
  75. if (!(element instanceof TypeElement)) {
  76. continue;
  77. }
  78. // check if class extends/implements an extension point
  79. if (!isExtension(element.asType())) {
  80. continue;
  81. }
  82. TypeElement extensionElement = (TypeElement) element;
  83. // Extension annotation = element.getAnnotation(Extension.class);
  84. List<TypeElement> extensionPointElements = findExtensionPoints(extensionElement);
  85. if (extensionPointElements.isEmpty()) {
  86. // TODO throw error ?
  87. continue;
  88. }
  89. String extension = getBinaryName(extensionElement);
  90. for (TypeElement extensionPointElement : extensionPointElements) {
  91. String extensionPoint = getBinaryName(extensionPointElement);
  92. Set<String> extensionPoints = extensions.get(extensionPoint);
  93. if (extensionPoints == null) {
  94. extensionPoints = new TreeSet<>();
  95. extensions.put(extensionPoint, extensionPoints);
  96. }
  97. extensionPoints.add(extension);
  98. }
  99. }
  100. // read old extensions
  101. oldExtensions = storage.read();
  102. for (Map.Entry<String, Set<String>> entry : oldExtensions.entrySet()) {
  103. String extensionPoint = entry.getKey();
  104. if (extensions.containsKey(extensionPoint)) {
  105. extensions.get(extensionPoint).addAll(entry.getValue());
  106. } else {
  107. extensions.put(extensionPoint, entry.getValue());
  108. }
  109. }
  110. // write extensions
  111. storage.write(extensions);
  112. return false;
  113. }
  114. public ProcessingEnvironment getProcessingEnvironment() {
  115. return processingEnv;
  116. }
  117. public void error(String message, Object... args) {
  118. processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format(message, args));
  119. }
  120. public void error(Element element, String message, Object... args) {
  121. processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format(message, args), element);
  122. }
  123. public void info(String message, Object... args) {
  124. processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format(message, args));
  125. }
  126. public void info(Element element, String message, Object... args) {
  127. processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format(message, args), element);
  128. }
  129. public String getBinaryName(TypeElement element) {
  130. return processingEnv.getElementUtils().getBinaryName(element).toString();
  131. }
  132. public Map<String, Set<String>> getExtensions() {
  133. return extensions;
  134. }
  135. public Map<String, Set<String>> getOldExtensions() {
  136. return oldExtensions;
  137. }
  138. private List<TypeElement> findExtensionPoints(TypeElement extensionElement) {
  139. List<TypeElement> extensionPointElements = new ArrayList<>();
  140. // search in interfaces
  141. for (TypeMirror item : extensionElement.getInterfaces()) {
  142. boolean isExtensionPoint = processingEnv.getTypeUtils().isSubtype(item, getExtensionPointType());
  143. if (isExtensionPoint) {
  144. TypeElement extensionPointElement = (TypeElement) ((DeclaredType) item).asElement();
  145. extensionPointElements.add(extensionPointElement);
  146. }
  147. }
  148. // search in superclass
  149. TypeMirror superclass = extensionElement.getSuperclass();
  150. if (superclass.getKind() != TypeKind.NONE) {
  151. boolean isExtensionPoint = processingEnv.getTypeUtils().isSubtype(superclass, getExtensionPointType());
  152. if (isExtensionPoint) {
  153. TypeElement extensionPointElement = (TypeElement) ((DeclaredType) superclass).asElement();
  154. extensionPointElements.add(extensionPointElement);
  155. }
  156. }
  157. return extensionPointElements;
  158. }
  159. private boolean isExtension(TypeMirror typeMirror) {
  160. return processingEnv.getTypeUtils().isAssignable(typeMirror, getExtensionPointType());
  161. }
  162. private TypeMirror getExtensionPointType() {
  163. return processingEnv.getElementUtils().getTypeElement(ExtensionPoint.class.getName()).asType();
  164. }
  165. @SuppressWarnings("unchecked")
  166. private ExtensionStorage createStorage() {
  167. ExtensionStorage storage = null;
  168. // search in processing options
  169. String storageClassName = processingEnv.getOptions().get(STORAGE_CLASS_NAME);
  170. if (storageClassName == null) {
  171. // search in system properties
  172. storageClassName = System.getProperty(STORAGE_CLASS_NAME);
  173. }
  174. if (storageClassName != null) {
  175. // use reflection to create the storage instance
  176. try {
  177. Class storageClass = getClass().getClassLoader().loadClass(storageClassName);
  178. Constructor constructor = storageClass.getConstructor(ExtensionAnnotationProcessor.class);
  179. storage = (ExtensionStorage) constructor.newInstance(this);
  180. } catch (Exception e) {
  181. error(e.getMessage());
  182. }
  183. }
  184. if (storage == null) {
  185. // default storage
  186. storage = new LegacyExtensionStorage(this);
  187. }
  188. return storage;
  189. }
  190. }