1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/model/element/TestTypeParameter.java Fri Feb 11 17:10:26 2011 -0800 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6505047 1.30 + * @summary javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter 1.31 + * @library ../../../lib 1.32 + * @build JavacTestingAbstractProcessor TestTypeParameter 1.33 + * @compile -processor TestTypeParameter -proc:only TestTypeParameter.java 1.34 + */ 1.35 + 1.36 +import java.util.*; 1.37 +import javax.annotation.processing.*; 1.38 +import javax.lang.model.element.*; 1.39 +import javax.lang.model.util.*; 1.40 +import javax.tools.*; 1.41 + 1.42 +public class TestTypeParameter<T> extends JavacTestingAbstractProcessor { 1.43 + int round = 0; 1.44 + 1.45 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 1.46 + if (++round == 1) { 1.47 + int found = (new Scanner()).scan(roundEnv.getRootElements(), null); 1.48 + if (found == expect) { 1.49 + note("generic elements found and verified: " + found); 1.50 + } else { 1.51 + error("unexpected number of results: expected " + expect 1.52 + + ", found " + found); 1.53 + } 1.54 + 1.55 + } 1.56 + return true; 1.57 + } 1.58 + 1.59 + class Scanner extends ElementScanner7<Integer,Void> { 1.60 + @Override 1.61 + public Integer visitExecutable(ExecutableElement e, Void p) { 1.62 + super.visitExecutable(e, p); 1.63 + found += check(e, e.getTypeParameters()); 1.64 + return found; 1.65 + } 1.66 + 1.67 + @Override 1.68 + public Integer visitType(TypeElement e, Void p) { 1.69 + super.visitType(e, p); 1.70 + found += check(e, e.getTypeParameters()); 1.71 + return found; 1.72 + } 1.73 + 1.74 + int found; 1.75 + } 1.76 + 1.77 + /** 1.78 + * Check if type parameters, if any, have expected owner. 1.79 + * Return 1 if typarams not empty and all have expected owner, else return 0. 1.80 + */ 1.81 + int check(Element e, List<? extends TypeParameterElement> typarams) { 1.82 + note("checking " + e, e); 1.83 + if (typarams.isEmpty()) { 1.84 + note("no type parameters found", e); 1.85 + return 0; 1.86 + } 1.87 + for (TypeParameterElement tpe: typarams) { 1.88 + note("checking type parameter " + tpe, tpe); 1.89 + if (tpe.getEnclosingElement() != e) { 1.90 + error("unexpected owner; expected: " + e 1.91 + + ", found " + tpe.getEnclosingElement(), 1.92 + tpe); 1.93 + return 0; 1.94 + } 1.95 + if (tpe.getEnclosingElement() != tpe.getGenericElement()) { 1.96 + error("unexpected generic element; expected: " + tpe.getGenericElement() 1.97 + + ", found " + tpe.getEnclosingElement(), 1.98 + tpe); 1.99 + return 0; 1.100 + } 1.101 + } 1.102 + note("verified " + e, e); 1.103 + return 1; 1.104 + } 1.105 + 1.106 + void note(String msg) { 1.107 + messager.printMessage(Diagnostic.Kind.NOTE, msg); 1.108 + } 1.109 + 1.110 + void note(String msg, Element e) { 1.111 + messager.printMessage(Diagnostic.Kind.NOTE, msg, e); 1.112 + } 1.113 + 1.114 + void error(String msg, Element e) { 1.115 + messager.printMessage(Diagnostic.Kind.ERROR, msg, e); 1.116 + } 1.117 + 1.118 + void error(String msg) { 1.119 + messager.printMessage(Diagnostic.Kind.ERROR, msg); 1.120 + } 1.121 + 1.122 + // additional generic elements to test 1.123 + <X> X m(X x) { return x; } 1.124 + 1.125 + interface Intf<X> { X m() ; } 1.126 + 1.127 + class Class<X> { 1.128 + <Y> Class() { } 1.129 + } 1.130 + 1.131 + final int expect = 5; // top level class, plus preceding examples 1.132 +}