Fri, 11 Feb 2011 17:10:26 -0800
6505047: javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter
Reviewed-by: darcy
jjg@875 | 1 | /* |
jjg@875 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
jjg@875 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@875 | 4 | * |
jjg@875 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@875 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@875 | 7 | * published by the Free Software Foundation. |
jjg@875 | 8 | * |
jjg@875 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@875 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@875 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@875 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@875 | 13 | * accompanied this code). |
jjg@875 | 14 | * |
jjg@875 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@875 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@875 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@875 | 18 | * |
jjg@875 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@875 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@875 | 21 | * questions. |
jjg@875 | 22 | */ |
jjg@875 | 23 | |
jjg@875 | 24 | /* |
jjg@875 | 25 | * @test |
jjg@875 | 26 | * @bug 6505047 |
jjg@875 | 27 | * @summary javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter |
jjg@875 | 28 | * @library ../../../lib |
jjg@875 | 29 | * @build JavacTestingAbstractProcessor TestTypeParameter |
jjg@875 | 30 | * @compile -processor TestTypeParameter -proc:only TestTypeParameter.java |
jjg@875 | 31 | */ |
jjg@875 | 32 | |
jjg@875 | 33 | import java.util.*; |
jjg@875 | 34 | import javax.annotation.processing.*; |
jjg@875 | 35 | import javax.lang.model.element.*; |
jjg@875 | 36 | import javax.lang.model.util.*; |
jjg@875 | 37 | import javax.tools.*; |
jjg@875 | 38 | |
jjg@875 | 39 | public class TestTypeParameter<T> extends JavacTestingAbstractProcessor { |
jjg@875 | 40 | int round = 0; |
jjg@875 | 41 | |
jjg@875 | 42 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jjg@875 | 43 | if (++round == 1) { |
jjg@875 | 44 | int found = (new Scanner()).scan(roundEnv.getRootElements(), null); |
jjg@875 | 45 | if (found == expect) { |
jjg@875 | 46 | note("generic elements found and verified: " + found); |
jjg@875 | 47 | } else { |
jjg@875 | 48 | error("unexpected number of results: expected " + expect |
jjg@875 | 49 | + ", found " + found); |
jjg@875 | 50 | } |
jjg@875 | 51 | |
jjg@875 | 52 | } |
jjg@875 | 53 | return true; |
jjg@875 | 54 | } |
jjg@875 | 55 | |
jjg@875 | 56 | class Scanner extends ElementScanner7<Integer,Void> { |
jjg@875 | 57 | @Override |
jjg@875 | 58 | public Integer visitExecutable(ExecutableElement e, Void p) { |
jjg@875 | 59 | super.visitExecutable(e, p); |
jjg@875 | 60 | found += check(e, e.getTypeParameters()); |
jjg@875 | 61 | return found; |
jjg@875 | 62 | } |
jjg@875 | 63 | |
jjg@875 | 64 | @Override |
jjg@875 | 65 | public Integer visitType(TypeElement e, Void p) { |
jjg@875 | 66 | super.visitType(e, p); |
jjg@875 | 67 | found += check(e, e.getTypeParameters()); |
jjg@875 | 68 | return found; |
jjg@875 | 69 | } |
jjg@875 | 70 | |
jjg@875 | 71 | int found; |
jjg@875 | 72 | } |
jjg@875 | 73 | |
jjg@875 | 74 | /** |
jjg@875 | 75 | * Check if type parameters, if any, have expected owner. |
jjg@875 | 76 | * Return 1 if typarams not empty and all have expected owner, else return 0. |
jjg@875 | 77 | */ |
jjg@875 | 78 | int check(Element e, List<? extends TypeParameterElement> typarams) { |
jjg@875 | 79 | note("checking " + e, e); |
jjg@875 | 80 | if (typarams.isEmpty()) { |
jjg@875 | 81 | note("no type parameters found", e); |
jjg@875 | 82 | return 0; |
jjg@875 | 83 | } |
jjg@875 | 84 | for (TypeParameterElement tpe: typarams) { |
jjg@875 | 85 | note("checking type parameter " + tpe, tpe); |
jjg@875 | 86 | if (tpe.getEnclosingElement() != e) { |
jjg@875 | 87 | error("unexpected owner; expected: " + e |
jjg@875 | 88 | + ", found " + tpe.getEnclosingElement(), |
jjg@875 | 89 | tpe); |
jjg@875 | 90 | return 0; |
jjg@875 | 91 | } |
jjg@875 | 92 | if (tpe.getEnclosingElement() != tpe.getGenericElement()) { |
jjg@875 | 93 | error("unexpected generic element; expected: " + tpe.getGenericElement() |
jjg@875 | 94 | + ", found " + tpe.getEnclosingElement(), |
jjg@875 | 95 | tpe); |
jjg@875 | 96 | return 0; |
jjg@875 | 97 | } |
jjg@875 | 98 | } |
jjg@875 | 99 | note("verified " + e, e); |
jjg@875 | 100 | return 1; |
jjg@875 | 101 | } |
jjg@875 | 102 | |
jjg@875 | 103 | void note(String msg) { |
jjg@875 | 104 | messager.printMessage(Diagnostic.Kind.NOTE, msg); |
jjg@875 | 105 | } |
jjg@875 | 106 | |
jjg@875 | 107 | void note(String msg, Element e) { |
jjg@875 | 108 | messager.printMessage(Diagnostic.Kind.NOTE, msg, e); |
jjg@875 | 109 | } |
jjg@875 | 110 | |
jjg@875 | 111 | void error(String msg, Element e) { |
jjg@875 | 112 | messager.printMessage(Diagnostic.Kind.ERROR, msg, e); |
jjg@875 | 113 | } |
jjg@875 | 114 | |
jjg@875 | 115 | void error(String msg) { |
jjg@875 | 116 | messager.printMessage(Diagnostic.Kind.ERROR, msg); |
jjg@875 | 117 | } |
jjg@875 | 118 | |
jjg@875 | 119 | // additional generic elements to test |
jjg@875 | 120 | <X> X m(X x) { return x; } |
jjg@875 | 121 | |
jjg@875 | 122 | interface Intf<X> { X m() ; } |
jjg@875 | 123 | |
jjg@875 | 124 | class Class<X> { |
jjg@875 | 125 | <Y> Class() { } |
jjg@875 | 126 | } |
jjg@875 | 127 | |
jjg@875 | 128 | final int expect = 5; // top level class, plus preceding examples |
jjg@875 | 129 | } |