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.AnnotationMirror;
23  import com.sun.mirror.declaration.AnnotationTypeDeclaration;
24  import com.sun.mirror.declaration.AnnotationTypeElementDeclaration;
25  import com.sun.mirror.declaration.AnnotationValue;
26  import com.sun.mirror.declaration.ClassDeclaration;
27  import com.sun.mirror.declaration.MethodDeclaration;
28  import com.sun.tools.apt.mirror.declaration.EnumConstantDeclarationImpl;
29  
30  import java.util.Collection;
31  import java.util.Map;
32  
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /*
37   * Created: Mar 22, 2005 8:10:05 PM
38   * User: bommel
39   * $Id: PrintAnnotationVisitor.java 1368242 2012-08-01 20:47:33Z lofwyr $
40   */
41  public class PrintAnnotationVisitor extends AnnotationDeclarationVisitorCollector {
42    private static final Logger LOG = LoggerFactory.getLogger(PrintAnnotationVisitor.class);
43    public void print() {
44      for (ClassDeclaration decl : getCollectedClassDeclarations()) {
45        printClassDeclaration(decl);
46      }
47    }
48  
49    private void printClassDeclaration(ClassDeclaration d) {
50      LOG.error("Class simpleName    " + d.getSimpleName());
51      LOG.error("Class package       " + d.getPackage());
52      LOG.error("Class qualifiedName " + d.getQualifiedName());
53      LOG.error("Class docComment    " + d.getDocComment());
54      LOG.error("Class superclass    " + d.getSuperclass());
55      printAnnotationMirrors(d.getAnnotationMirrors());
56      printMethods(d);
57      LOG.error("++++++++++++++++++++++++++++++++++++++");
58    }
59  
60    private void printMethodDeclaration(MethodDeclaration d) {
61      LOG.error("Method simpleName    " + d.getSimpleName());
62      LOG.error("Method docComment    " + d.getDocComment());
63      LOG.error("Method returnType    " + d.getReturnType());
64      LOG.error("Method parameter     " + d.getParameters());
65      LOG.error("Method declaringType " + d.getDeclaringType());
66      printAnnotationMirrors(d.getAnnotationMirrors());
67    }
68  
69    private void printAnnotationMirrors(Collection<AnnotationMirror> mirrors) {
70      for (AnnotationMirror mirror : mirrors) {
71        LOG.error("========================");
72        Map<AnnotationTypeElementDeclaration,
73            AnnotationValue> elementValues = mirror.getElementValues();
74        printAnnotationTypeDeclaration(mirror.getAnnotationType().getDeclaration());
75        for (AnnotationTypeElementDeclaration decl : mirror.getAnnotationType().getDeclaration().getMethods()) {
76          LOG.error("-------------------");
77          printAnnotationTypeElementDeclaration(decl);
78          if (elementValues.containsKey(decl)) {
79            AnnotationValue value = elementValues.get(decl);
80            LOG.error("Type Element value=" + value.getValue());
81          }
82  
83        }
84  
85      }
86    }
87  
88    public void printMethods(ClassDeclaration d) {
89      for (MethodDeclaration decl : getCollectedMethodDeclarations()) {
90        if (d.getQualifiedName().
91            equals(decl.getDeclaringType().getQualifiedName())) {
92          LOG.error("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
93          printMethodDeclaration(decl);
94        }
95      }
96      if (d.getSuperclass() != null) {
97        printMethods(d.getSuperclass().getDeclaration());
98      }
99    }
100 
101   public void printAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
102     LOG.error("Type qualifiedName " + d.getQualifiedName());
103   }
104 
105   public void printAnnotationTypeElementDeclaration(AnnotationTypeElementDeclaration d) {
106 
107     LOG.error("Type Element simpleName    " + d.getSimpleName());
108     LOG.error("Type Element returnType    " + d.getReturnType());
109     if (d.getDefaultValue() != null) {
110       LOG.error("Type Element defaultValue  " + d.getDefaultValue());
111       if (d.getDefaultValue().getValue() instanceof EnumConstantDeclarationImpl) {
112         EnumConstantDeclarationImpl impl = ((EnumConstantDeclarationImpl) d.getDefaultValue().getValue());
113         LOG.error("Type Element Enum simple Name " + impl.getSimpleName());
114         LOG.error("Type Element Enum type " + impl.getType());
115       }
116     }
117   }
118 }