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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  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 org.pf4j.util.ClassUtils;
  20. import javax.annotation.processing.AbstractProcessor;
  21. import javax.annotation.processing.ProcessingEnvironment;
  22. import javax.annotation.processing.RoundEnvironment;
  23. import javax.lang.model.SourceVersion;
  24. import javax.lang.model.element.AnnotationValue;
  25. import javax.lang.model.element.Element;
  26. import javax.lang.model.element.TypeElement;
  27. import javax.lang.model.type.DeclaredType;
  28. import javax.lang.model.type.TypeKind;
  29. import javax.lang.model.type.TypeMirror;
  30. import javax.tools.Diagnostic;
  31. import java.lang.reflect.Constructor;
  32. import java.util.ArrayList;
  33. import java.util.HashMap;
  34. import java.util.HashSet;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.Set;
  38. import java.util.TreeSet;
  39. /**
  40. * @author Decebal Suiu
  41. */
  42. public class ExtensionAnnotationProcessor extends AbstractProcessor {
  43. private static final String STORAGE_CLASS_NAME = "pf4j.storageClassName";
  44. private Map<String, Set<String>> extensions = new HashMap<>(); // the key is the extension point
  45. private Map<String, Set<String>> oldExtensions = new HashMap<>(); // the key is the extension point
  46. private ExtensionStorage storage;
  47. @Override
  48. public synchronized void init(ProcessingEnvironment processingEnv) {
  49. super.init(processingEnv);
  50. info("%s init", ExtensionAnnotationProcessor.class);
  51. storage = createStorage();
  52. }
  53. @Override
  54. public SourceVersion getSupportedSourceVersion() {
  55. return SourceVersion.latest();
  56. }
  57. @Override
  58. public Set<String> getSupportedAnnotationTypes() {
  59. Set<String> annotationTypes = new HashSet<>();
  60. annotationTypes.add(Extension.class.getName());
  61. return annotationTypes;
  62. }
  63. @Override
  64. public Set<String> getSupportedOptions() {
  65. Set<String> options = new HashSet<>();
  66. options.add(STORAGE_CLASS_NAME);
  67. return options;
  68. }
  69. @Override
  70. public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  71. if (roundEnv.processingOver()) {
  72. return false;
  73. }
  74. info("Processing @%s", Extension.class);
  75. for (Element element : roundEnv.getElementsAnnotatedWith(Extension.class)) {
  76. // check if @Extension is put on class and not on method or constructor
  77. if (!(element instanceof TypeElement)) {
  78. continue;
  79. }
  80. // check if class extends/implements an extension point
  81. if (!isExtension(element.asType())) {
  82. continue;
  83. }
  84. TypeElement extensionElement = (TypeElement) element;
  85. // Extension annotation = element.getAnnotation(Extension.class);
  86. List<TypeElement> extensionPointElements = findExtensionPoints(extensionElement);
  87. if (extensionPointElements.isEmpty()) {
  88. // TODO throw error ?
  89. continue;
  90. }
  91. String extension = getBinaryName(extensionElement);
  92. for (TypeElement extensionPointElement : extensionPointElements) {
  93. String extensionPoint = getBinaryName(extensionPointElement);
  94. Set<String> extensionPoints = extensions.computeIfAbsent(extensionPoint, k -> new TreeSet<>());
  95. extensionPoints.add(extension);
  96. }
  97. }
  98. // read old extensions
  99. oldExtensions = storage.read();
  100. for (Map.Entry<String, Set<String>> entry : oldExtensions.entrySet()) {
  101. String extensionPoint = entry.getKey();
  102. if (extensions.containsKey(extensionPoint)) {
  103. extensions.get(extensionPoint).addAll(entry.getValue());
  104. } else {
  105. extensions.put(extensionPoint, entry.getValue());
  106. }
  107. }
  108. // write extensions
  109. storage.write(extensions);
  110. return false;
  111. }
  112. public ProcessingEnvironment getProcessingEnvironment() {
  113. return processingEnv;
  114. }
  115. public void error(String message, Object... args) {
  116. processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format(message, args));
  117. }
  118. public void error(Element element, String message, Object... args) {
  119. processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format(message, args), element);
  120. }
  121. public void info(String message, Object... args) {
  122. processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format(message, args));
  123. }
  124. public void info(Element element, String message, Object... args) {
  125. processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.format(message, args), element);
  126. }
  127. public String getBinaryName(TypeElement element) {
  128. return processingEnv.getElementUtils().getBinaryName(element).toString();
  129. }
  130. public Map<String, Set<String>> getExtensions() {
  131. return extensions;
  132. }
  133. public Map<String, Set<String>> getOldExtensions() {
  134. return oldExtensions;
  135. }
  136. private List<TypeElement> findExtensionPoints(TypeElement extensionElement) {
  137. List<TypeElement> extensionPointElements = new ArrayList<>();
  138. // use extension points, that were explicitly set in the extension annotation
  139. AnnotationValue annotatedExtensionPoints = ClassUtils.getAnnotationValue(extensionElement, Extension.class, "points");
  140. List<? extends AnnotationValue> extensionPointClasses = (annotatedExtensionPoints != null) ?
  141. (List<? extends AnnotationValue>) annotatedExtensionPoints.getValue() :
  142. null;
  143. if (extensionPointClasses != null && !extensionPointClasses.isEmpty()) {
  144. for (AnnotationValue extensionPointClass : extensionPointClasses) {
  145. String extensionPointClassName = extensionPointClass.getValue().toString();
  146. TypeElement extensionPointElement = processingEnv.getElementUtils().getTypeElement(extensionPointClassName);
  147. extensionPointElements.add(extensionPointElement);
  148. }
  149. }
  150. // detect extension points automatically, if they are not explicitly configured (default behaviour)
  151. else {
  152. // search in interfaces
  153. for (TypeMirror item : extensionElement.getInterfaces()) {
  154. boolean isExtensionPoint = processingEnv.getTypeUtils().isSubtype(item, getExtensionPointType());
  155. if (isExtensionPoint) {
  156. TypeElement extensionPointElement = (TypeElement) ((DeclaredType) item).asElement();
  157. extensionPointElements.add(extensionPointElement);
  158. }
  159. }
  160. // search in superclass
  161. TypeMirror superclass = extensionElement.getSuperclass();
  162. if (superclass.getKind() != TypeKind.NONE) {
  163. boolean isExtensionPoint = processingEnv.getTypeUtils().isSubtype(superclass, getExtensionPointType());
  164. if (isExtensionPoint) {
  165. TypeElement extensionPointElement = (TypeElement) ((DeclaredType) superclass).asElement();
  166. extensionPointElements.add(extensionPointElement);
  167. }
  168. }
  169. }
  170. return extensionPointElements;
  171. }
  172. private boolean isExtension(TypeMirror typeMirror) {
  173. return processingEnv.getTypeUtils().isAssignable(typeMirror, getExtensionPointType());
  174. }
  175. private TypeMirror getExtensionPointType() {
  176. return processingEnv.getElementUtils().getTypeElement(ExtensionPoint.class.getName()).asType();
  177. }
  178. @SuppressWarnings("unchecked")
  179. private ExtensionStorage createStorage() {
  180. ExtensionStorage storage = null;
  181. // search in processing options
  182. String storageClassName = processingEnv.getOptions().get(STORAGE_CLASS_NAME);
  183. if (storageClassName == null) {
  184. // search in system properties
  185. storageClassName = System.getProperty(STORAGE_CLASS_NAME);
  186. }
  187. if (storageClassName != null) {
  188. // use reflection to create the storage instance
  189. try {
  190. Class storageClass = getClass().getClassLoader().loadClass(storageClassName);
  191. Constructor constructor = storageClass.getConstructor(ExtensionAnnotationProcessor.class);
  192. storage = (ExtensionStorage) constructor.newInstance(this);
  193. } catch (Exception e) {
  194. error(e.getMessage());
  195. }
  196. }
  197. if (storage == null) {
  198. // default storage
  199. storage = new LegacyExtensionStorage(this);
  200. }
  201. return storage;
  202. }
  203. }