1 /*
   2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 import java.io.*;
  25 import javax.annotation.processing.*;
  26 import javax.lang.model.*;
  27 import javax.lang.model.element.*;
  28 import javax.lang.model.type.*;
  29 import javax.lang.model.util.*;
  30 import java.util.*;
  31 import com.sun.source.tree.*;
  32 import com.sun.source.util.*;
  33 import static javax.tools.Diagnostic.Kind.*;
  34 
  35 /**
  36  * Using the tree API, retrieve element representations of anonymous
  37  * classes are verify their names are as specified.
  38  */
  39 @SupportedAnnotationTypes("*")
  40 public class TestAnonSourceNames extends AbstractProcessor {
  41 
  42    public boolean process(Set<? extends TypeElement> annotations,
  43                           RoundEnvironment roundEnv) {
  44        if (!roundEnv.processingOver()) {
  45            Trees trees = Trees.instance(processingEnv);
  46 
  47            for(Element rootElement : roundEnv.getRootElements()) {
  48                TreePath treePath = trees.getPath(rootElement);
  49 
  50                (new ClassTreeScanner(trees)).
  51                    scan(trees.getTree(rootElement),
  52                         treePath.getCompilationUnit());
  53            }
  54        }
  55        return true;
  56    }
  57 
  58    class ClassTreeScanner extends TreeScanner<Void, CompilationUnitTree> {
  59        private Trees trees;
  60 
  61        public ClassTreeScanner(Trees trees) {
  62            super();
  63            this.trees = trees;
  64        }
  65        @Override
  66        public Void visitClass(ClassTree node, CompilationUnitTree cu) {
  67                      Element element = trees.getElement(trees.getPath(cu, node));
  68            if (element == null) {
  69                processingEnv.getMessager().printMessage(ERROR,
  70                                                         "No element retreived for node named ''" +
  71                                                         node.getSimpleName() + "''.");
  72            } else {
  73 
  74                System.out.println("\nVisiting class ``" + element.getSimpleName() +
  75                                   "'' of kind " + element.getKind());
  76                          if (element instanceof TypeElement) {
  77                    TypeElement typeElement = (TypeElement) element;
  78                    String s = typeElement.getQualifiedName().toString();
  79                    System.out.println("\tqualified name:" + s);
  80                } else {
  81                    throw new RuntimeException("TypeElement not gotten from ClassTree.");
  82                }
  83            }
  84            return super.visitClass(node, cu);
  85        }
  86    }
  87 
  88    @Override
  89    public SourceVersion getSupportedSourceVersion() {
  90        return SourceVersion.latest();
  91    }
  92 }