test/tools/javac/processing/model/element/TestTypeParameter.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2011, 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 */
23
24 /*
25 * @test
26 * @bug 6505047
27 * @summary javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter
28 * @library /tools/javac/lib
29 * @build JavacTestingAbstractProcessor TestTypeParameter
30 * @compile -processor TestTypeParameter -proc:only TestTypeParameter.java
31 */
32
33 import java.util.*;
34 import javax.annotation.processing.*;
35 import javax.lang.model.element.*;
36 import javax.lang.model.util.*;
37 import javax.tools.*;
38
39 public class TestTypeParameter<T> extends JavacTestingAbstractProcessor {
40 int round = 0;
41
42 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
43 if (++round == 1) {
44 int found = (new Scanner()).scan(roundEnv.getRootElements(), null);
45 if (found == expect) {
46 note("generic elements found and verified: " + found);
47 } else {
48 error("unexpected number of results: expected " + expect
49 + ", found " + found);
50 }
51
52 }
53 return true;
54 }
55
56 class Scanner extends ElementScanner7<Integer,Void> {
57 @Override
58 public Integer visitExecutable(ExecutableElement e, Void p) {
59 super.visitExecutable(e, p);
60 found += check(e, e.getTypeParameters());
61 return found;
62 }
63
64 @Override
65 public Integer visitType(TypeElement e, Void p) {
66 super.visitType(e, p);
67 found += check(e, e.getTypeParameters());
68 return found;
69 }
70
71 int found;
72 }
73
74 /**
75 * Check if type parameters, if any, have expected owner.
76 * Return 1 if typarams not empty and all have expected owner, else return 0.
77 */
78 int check(Element e, List<? extends TypeParameterElement> typarams) {
79 note("checking " + e, e);
80 if (typarams.isEmpty()) {
81 note("no type parameters found", e);
82 return 0;
83 }
84 for (TypeParameterElement tpe: typarams) {
85 note("checking type parameter " + tpe, tpe);
86 if (tpe.getEnclosingElement() != e) {
87 error("unexpected owner; expected: " + e
88 + ", found " + tpe.getEnclosingElement(),
89 tpe);
90 return 0;
91 }
92 if (tpe.getEnclosingElement() != tpe.getGenericElement()) {
93 error("unexpected generic element; expected: " + tpe.getGenericElement()
94 + ", found " + tpe.getEnclosingElement(),
95 tpe);
96 return 0;
97 }
98 }
99 note("verified " + e, e);
100 return 1;
101 }
102
103 void note(String msg) {
104 messager.printMessage(Diagnostic.Kind.NOTE, msg);
105 }
106
107 void note(String msg, Element e) {
108 messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
109 }
110
111 void error(String msg, Element e) {
112 messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
113 }
114
115 void error(String msg) {
116 messager.printMessage(Diagnostic.Kind.ERROR, msg);
117 }
118
119 // additional generic elements to test
120 <X> X m(X x) { return x; }
121
122 interface Intf<X> { X m() ; }
123
124 class Class<X> {
125 <Y> Class() { }
126 }
127
128 final int expect = 5; // top level class, plus preceding examples
129 }

mercurial