Wed, 02 Mar 2011 21:13:55 -0800
6639645: Modeling type implementing missing interfaces
Reviewed-by: darcy, mcimadamore
1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
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.*;
35 /**
36 * Using the tree API, retrieve element representations of anonymous
37 * classes and verify their names are as specified.
38 */
39 public class TestAnonSourceNames extends JavacTestingAbstractProcessor {
41 public boolean process(Set<? extends TypeElement> annotations,
42 RoundEnvironment roundEnv) {
43 if (!roundEnv.processingOver()) {
44 Trees trees = Trees.instance(processingEnv);
46 for(Element rootElement : roundEnv.getRootElements()) {
47 TreePath treePath = trees.getPath(rootElement);
49 (new ClassTreeScanner(trees)).
50 scan(trees.getTree(rootElement),
51 treePath.getCompilationUnit());
52 }
53 }
54 return true;
55 }
57 class ClassTreeScanner extends TreeScanner<Void, CompilationUnitTree> {
58 private Trees trees;
60 public ClassTreeScanner(Trees trees) {
61 super();
62 this.trees = trees;
63 }
64 @Override
65 public Void visitClass(ClassTree node, CompilationUnitTree cu) {
66 Element element = trees.getElement(trees.getPath(cu, node));
67 if (element == null) {
68 processingEnv.getMessager().printMessage(ERROR,
69 "No element retrieved for node named ''" +
70 node.getSimpleName() + "''.");
71 } else {
73 System.out.println("\nVisiting class ``" + element.getSimpleName() +
74 "'' of kind " + element.getKind());
75 if (element instanceof TypeElement) {
76 TypeElement typeElement = (TypeElement) element;
77 String s = typeElement.getQualifiedName().toString();
78 System.out.println("\tqualified name:" + s);
79 } else {
80 throw new RuntimeException("TypeElement not gotten from ClassTree.");
81 }
82 }
83 return super.visitClass(node, cu);
84 }
85 }
86 }