View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.apt.processor;
21  
22  import javax.annotation.processing.AbstractProcessor;
23  import javax.annotation.processing.RoundEnvironment;
24  import javax.annotation.processing.SupportedSourceVersion;
25  import javax.lang.model.SourceVersion;
26  import javax.lang.model.element.Element;
27  import javax.lang.model.element.PackageElement;
28  import javax.lang.model.element.TypeElement;
29  import javax.tools.Diagnostic;
30  import java.io.PrintWriter;
31  import java.io.StringWriter;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.Comparator;
36  import java.util.List;
37  import java.util.Set;
38  
39  @SupportedSourceVersion(SourceVersion.RELEASE_6)
40  public abstract class AbstractGenerator extends AbstractProcessor {
41  
42    private List<TypeElement> types;
43    private List<PackageElement> packages;
44  
45    @Override
46    public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
47  
48      info("**********************************************************************************");
49      info("* Starting generator: " + getClass().getName());
50      info("* Number of annotations:   " + annotations.size());
51      for (final TypeElement typeElement : annotations) {
52        info("* Type element: " + typeElement.getQualifiedName());
53      }
54  
55      if (annotations.size() == 0) {
56        // TDB Why this case happen?
57        return false;
58      }
59  
60      types = new ArrayList<TypeElement>();
61      packages = new ArrayList<PackageElement>();
62      for (final TypeElement element : annotations) {
63        final Collection<? extends Element> elementsAnnotatedWith = roundEnv
64            .getElementsAnnotatedWith(element);
65        for (final Element e : elementsAnnotatedWith) {
66          if (e instanceof TypeElement) {
67            if (!types.contains(e)) { // todo: optimize, O(n^2)?
68              types.add((TypeElement) e);
69            }
70          }
71          if (e instanceof PackageElement) {
72            if (!packages.contains(e)) {
73              packages.add((PackageElement) e);
74            }
75          }
76        }
77      }
78  
79      Collections.sort(types, new Comparator<TypeElement>() {
80        @Override
81        public int compare(final TypeElement d1, final TypeElement d2) {
82          return d1.getSimpleName().toString().compareTo(d2.getSimpleName().toString());
83        }
84      });
85  
86      configure();
87  
88      try {
89        generate();
90      } catch (final Exception e) {
91        error(e);
92      }
93  
94      return false;
95    }
96  
97    protected abstract void configure();
98  
99    protected abstract void generate() throws Exception;
100 
101   protected void info(final String message) {
102     processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
103         "<" + getClass().getSimpleName() + "> " + message);
104   }
105 
106   protected void warn(final String message) {
107     processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
108         "<" + getClass().getSimpleName() + "> " + message);
109   }
110 
111   protected void error(final Exception e) {
112     final StringWriter out = new StringWriter();
113     e.printStackTrace(new PrintWriter(out));
114     processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
115         "<" + getClass().getSimpleName() + "> " + e.getMessage() + "\n" + out.toString());
116   }
117 
118   public List<TypeElement> getTypes() {
119     return types;
120   }
121 
122   public List<PackageElement> getPackages() {
123     return packages;
124   }
125 }