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