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_5)
40  public abstract class AbstractGenerator extends AbstractProcessor {
41  
42    static final String JSF_VERSION = "jsfVersion";
43  
44    private List<TypeElement> types;
45    private List<PackageElement> packages;
46  
47    public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
48  
49      info("**********************************************************************************");
50      info("* Starting generator: " + getClass().getName());
51      info("* Number of annotations:   " + annotations.size());
52      for (final TypeElement typeElement : annotations) {
53        info("* Type element: " + typeElement.getQualifiedName());
54      }
55  
56      if (annotations.size() == 0) {
57        // TDB Why this case happen?
58        return false;
59      }
60  
61      types = new ArrayList<TypeElement>();
62      packages = new ArrayList<PackageElement>();
63      for (final TypeElement element : annotations) {
64        final Collection<? extends Element> elementsAnnotatedWith = roundEnv
65            .getElementsAnnotatedWith(element);
66        for (final Element e : elementsAnnotatedWith) {
67          if (e instanceof TypeElement) {
68            if (!types.contains(e)) { // todo: optimize, O(n^2)?
69              types.add((TypeElement) e);
70            }
71          }
72          if (e instanceof PackageElement) {
73            if (!packages.contains(e)) {
74              packages.add((PackageElement) e);
75            }
76          }
77        }
78      }
79  
80      Collections.sort(types, new Comparator<TypeElement>() {
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 }