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.

ExtensionsIndexer.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2013 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j;
  14. import java.io.BufferedReader;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.io.Reader;
  18. import java.io.Writer;
  19. import java.util.ArrayList;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;
  23. import javax.annotation.processing.AbstractProcessor;
  24. import javax.annotation.processing.RoundEnvironment;
  25. import javax.lang.model.SourceVersion;
  26. import javax.lang.model.element.Element;
  27. import javax.lang.model.element.TypeElement;
  28. import javax.tools.Diagnostic.Kind;
  29. import javax.tools.FileObject;
  30. import javax.tools.StandardLocation;
  31. /**
  32. * @author Decebal Suiu
  33. */
  34. public class ExtensionsIndexer extends AbstractProcessor {
  35. public static final String EXTENSIONS_RESOURCE = "META-INF/extensions.idx";
  36. private List<TypeElement> extensions = new ArrayList<TypeElement>();
  37. @Override
  38. public SourceVersion getSupportedSourceVersion() {
  39. return SourceVersion.latest();
  40. }
  41. @Override
  42. public Set<String> getSupportedAnnotationTypes() {
  43. Set<String> annotationTypes = new HashSet<String>();
  44. annotationTypes.add(Extension.class.getName());
  45. return annotationTypes;
  46. }
  47. @Override
  48. public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  49. if (roundEnv.processingOver()) {
  50. return false;
  51. }
  52. for (Element element : roundEnv.getElementsAnnotatedWith(Extension.class)) {
  53. if (!(element instanceof TypeElement)) {
  54. continue;
  55. }
  56. TypeElement typeElement = (TypeElement) element;
  57. String message = "Extension found in " + processingEnv.getElementUtils().getBinaryName(typeElement).toString();
  58. processingEnv.getMessager().printMessage(Kind.NOTE, message);
  59. extensions.add(typeElement);
  60. }
  61. /*
  62. if (!roundEnv.processingOver()) {
  63. return false;
  64. }
  65. */
  66. write();
  67. return false;
  68. // return true; // no further processing of this annotation type
  69. }
  70. private void write() {
  71. Set<String> entries = new HashSet<String>();
  72. for (TypeElement typeElement : extensions) {
  73. entries.add(processingEnv.getElementUtils().getBinaryName(typeElement).toString());
  74. }
  75. read(entries); // read old entries
  76. write(entries); // write entries
  77. }
  78. private void write(Set<String> entries) {
  79. try {
  80. FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
  81. Writer writer = file.openWriter();
  82. for (String entry : entries) {
  83. writer.write(entry);
  84. writer.write("\n");
  85. }
  86. writer.close();
  87. } catch (FileNotFoundException e) {
  88. // it's the first time, create the file
  89. } catch (IOException e) {
  90. processingEnv.getMessager().printMessage(Kind.ERROR, e.toString());
  91. }
  92. }
  93. private void read(Set<String> entries) {
  94. try {
  95. FileObject file = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
  96. readIndex(file.openReader(true), entries);
  97. } catch (FileNotFoundException e) {
  98. } catch (IOException e) {
  99. // thrown by Eclipse JDT when not found
  100. } catch (UnsupportedOperationException e) {
  101. // java6 does not support reading old index files
  102. }
  103. }
  104. public static void readIndex(Reader reader, Set<String> entries) throws IOException {
  105. BufferedReader bufferedReader = new BufferedReader(reader);
  106. String line;
  107. while ((line = bufferedReader.readLine()) != null) {
  108. entries.add(line);
  109. }
  110. reader.close();
  111. }
  112. }