874N/A/*
874N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
874N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
874N/A *
874N/A * This code is free software; you can redistribute it and/or modify it
874N/A * under the terms of the GNU General Public License version 2 only, as
874N/A * published by the Free Software Foundation.
874N/A *
874N/A * This code is distributed in the hope that it will be useful, but WITHOUT
874N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
874N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
874N/A * version 2 for more details (a copy is included in the LICENSE file that
874N/A * accompanied this code).
874N/A *
874N/A * You should have received a copy of the GNU General Public License version
874N/A * 2 along with this work; if not, write to the Free Software Foundation,
874N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
874N/A *
874N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
874N/A * or visit www.oracle.com if you need additional information or have any
874N/A * questions.
874N/A */
874N/A
874N/A/*
874N/A * @test
874N/A * @bug 6505047
874N/A * @summary javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter
874N/A * @library ../../../lib
874N/A * @build JavacTestingAbstractProcessor TestTypeParameter
874N/A * @compile -processor TestTypeParameter -proc:only TestTypeParameter.java
874N/A */
874N/A
874N/Aimport java.util.*;
874N/Aimport javax.annotation.processing.*;
874N/Aimport javax.lang.model.element.*;
874N/Aimport javax.lang.model.util.*;
874N/Aimport javax.tools.*;
874N/A
874N/Apublic class TestTypeParameter<T> extends JavacTestingAbstractProcessor {
874N/A int round = 0;
874N/A
874N/A public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
874N/A if (++round == 1) {
874N/A int found = (new Scanner()).scan(roundEnv.getRootElements(), null);
874N/A if (found == expect) {
874N/A note("generic elements found and verified: " + found);
874N/A } else {
874N/A error("unexpected number of results: expected " + expect
874N/A + ", found " + found);
874N/A }
874N/A
874N/A }
874N/A return true;
874N/A }
874N/A
874N/A class Scanner extends ElementScanner7<Integer,Void> {
874N/A @Override
874N/A public Integer visitExecutable(ExecutableElement e, Void p) {
874N/A super.visitExecutable(e, p);
874N/A found += check(e, e.getTypeParameters());
874N/A return found;
874N/A }
874N/A
874N/A @Override
874N/A public Integer visitType(TypeElement e, Void p) {
874N/A super.visitType(e, p);
874N/A found += check(e, e.getTypeParameters());
874N/A return found;
874N/A }
874N/A
874N/A int found;
874N/A }
874N/A
874N/A /**
874N/A * Check if type parameters, if any, have expected owner.
874N/A * Return 1 if typarams not empty and all have expected owner, else return 0.
874N/A */
874N/A int check(Element e, List<? extends TypeParameterElement> typarams) {
874N/A note("checking " + e, e);
874N/A if (typarams.isEmpty()) {
874N/A note("no type parameters found", e);
874N/A return 0;
874N/A }
874N/A for (TypeParameterElement tpe: typarams) {
874N/A note("checking type parameter " + tpe, tpe);
874N/A if (tpe.getEnclosingElement() != e) {
874N/A error("unexpected owner; expected: " + e
874N/A + ", found " + tpe.getEnclosingElement(),
874N/A tpe);
874N/A return 0;
874N/A }
874N/A if (tpe.getEnclosingElement() != tpe.getGenericElement()) {
874N/A error("unexpected generic element; expected: " + tpe.getGenericElement()
874N/A + ", found " + tpe.getEnclosingElement(),
874N/A tpe);
874N/A return 0;
874N/A }
874N/A }
874N/A note("verified " + e, e);
874N/A return 1;
874N/A }
874N/A
874N/A void note(String msg) {
874N/A messager.printMessage(Diagnostic.Kind.NOTE, msg);
874N/A }
874N/A
874N/A void note(String msg, Element e) {
874N/A messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
874N/A }
874N/A
874N/A void error(String msg, Element e) {
874N/A messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
874N/A }
874N/A
874N/A void error(String msg) {
874N/A messager.printMessage(Diagnostic.Kind.ERROR, msg);
874N/A }
874N/A
874N/A // additional generic elements to test
874N/A <X> X m(X x) { return x; }
874N/A
874N/A interface Intf<X> { X m() ; }
874N/A
874N/A class Class<X> {
874N/A <Y> Class() { }
874N/A }
874N/A
874N/A final int expect = 5; // top level class, plus preceding examples
874N/A}