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