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;
21  
22  import com.sun.mirror.declaration.ClassDeclaration;
23  import com.sun.mirror.declaration.InterfaceDeclaration;
24  import com.sun.mirror.declaration.MethodDeclaration;
25  import com.sun.mirror.declaration.PackageDeclaration;
26  import com.sun.mirror.util.SimpleDeclarationVisitor;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Comparator;
31  import java.util.List;
32  
33  /*
34   * Created: Mar 22, 2005 8:12:24 PM
35   * $Id: AnnotationDeclarationVisitorCollector.java 1368242 2012-08-01 20:47:33Z lofwyr $
36   */
37  public class AnnotationDeclarationVisitorCollector extends SimpleDeclarationVisitor {
38  
39    private List<MethodDeclaration> collectedMethodDeclarations = new ArrayList<MethodDeclaration>();
40    private List<ClassDeclaration> collectedClassDeclarations = new ArrayList<ClassDeclaration>();
41    private List<InterfaceDeclaration> collectedInterfaceDeclarations = new ArrayList<InterfaceDeclaration>();
42    private List<PackageDeclaration> collectedPackageDeclarations = new ArrayList<PackageDeclaration>();
43  
44    public List<MethodDeclaration> getCollectedMethodDeclarations() {
45      Collections.sort(collectedMethodDeclarations, new Comparator<MethodDeclaration>() {
46        public int compare(MethodDeclaration d1, MethodDeclaration d2) {
47            return d1.getSimpleName().compareTo(d2.getSimpleName());
48        }
49      });
50      return collectedMethodDeclarations;
51    }
52  
53    public List<ClassDeclaration> getCollectedClassDeclarations() {
54      Collections.sort(collectedClassDeclarations, new Comparator<ClassDeclaration>() {
55        public int compare(ClassDeclaration d1, ClassDeclaration d2) {
56          return d1.getSimpleName().compareTo(d2.getSimpleName());
57        }
58      });
59      return collectedClassDeclarations;
60    }
61  
62    public List<InterfaceDeclaration> getCollectedInterfaceDeclarations() {
63      Collections.sort(collectedInterfaceDeclarations, new Comparator<InterfaceDeclaration>() {
64        public int compare(InterfaceDeclaration d1, InterfaceDeclaration d2) {
65          return d1.getSimpleName().compareTo(d2.getSimpleName());
66        }
67      });
68      return collectedInterfaceDeclarations;
69    }
70  
71    public List<PackageDeclaration> getCollectedPackageDeclarations() {
72      return collectedPackageDeclarations;
73    }
74  
75  
76    public void visitMethodDeclaration(MethodDeclaration d) {
77      if (!collectedMethodDeclarations.contains(d)
78          && !d.getAnnotationMirrors().isEmpty()) {
79        collectedMethodDeclarations.add(d);
80      }
81    }
82  
83    public void visitPackageDeclaration(PackageDeclaration d) {
84      if (!collectedPackageDeclarations.contains(d)
85          && !d.getAnnotationMirrors().isEmpty()) {
86        collectedPackageDeclarations.add(d);
87      }
88    }
89  
90    public void visitInterfaceDeclaration(InterfaceDeclaration d) {
91      // TODO why this needed????
92      visitPackageDeclaration(d.getPackage());
93      if (!collectedInterfaceDeclarations.contains(d)
94          && !d.getAnnotationMirrors().isEmpty()) {
95        collectedInterfaceDeclarations.add(d);
96      }
97    }
98  
99    public void visitClassDeclaration(ClassDeclaration d) {
100     // TODO why this needed????
101     visitPackageDeclaration(d.getPackage());
102     if (!collectedClassDeclarations.contains(d)
103         && !d.getAnnotationMirrors().isEmpty()) {
104       collectedClassDeclarations.add(d);
105     }
106   }
107 }