langtools/test/tools/javac/processing/model/element/TestTypeParameter.java
author darcy
Thu, 17 Jul 2014 09:47:25 -0700
changeset 25690 b1dac768ab79
parent 14963 974d4423c999
child 30730 d3ce7619db2c
permissions -rw-r--r--
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9 Reviewed-by: jjg

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6505047
 * @summary javax.lang.model.element.Element.getEnclosingElement() doesn't return null for type parameter
 * @library /tools/javac/lib
 * @build JavacTestingAbstractProcessor TestTypeParameter
 * @compile -processor TestTypeParameter -proc:only TestTypeParameter.java
 */

import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.util.*;
import javax.tools.*;

public class TestTypeParameter<T> extends JavacTestingAbstractProcessor {
    int round = 0;

    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if (++round == 1) {
            int found = (new Scanner()).scan(roundEnv.getRootElements(), null);
            if (found == expect) {
                note("generic elements found and verified: " + found);
            } else {
                error("unexpected number of results: expected " + expect
                        + ", found " + found);
            }

        }
        return true;
    }

    class Scanner extends ElementScanner<Integer, Void> {
        @Override
        public Integer visitExecutable(ExecutableElement e, Void p) {
            super.visitExecutable(e, p);
            found += check(e, e.getTypeParameters());
            return found;
        }

        @Override
        public Integer visitType(TypeElement e, Void p) {
            super.visitType(e, p);
            found += check(e, e.getTypeParameters());
            return found;
        }

        int found;
    }

    /**
     * Check if type parameters, if any, have expected owner.
     * Return 1 if typarams not empty and all have expected owner, else return 0.
     */
    int check(Element e, List<? extends TypeParameterElement> typarams) {
        note("checking " + e, e);
        if (typarams.isEmpty()) {
            note("no type parameters found", e);
            return 0;
        }
        for (TypeParameterElement tpe: typarams) {
            note("checking type parameter " + tpe, tpe);
            if (tpe.getEnclosingElement() != e) {
                error("unexpected owner; expected: " + e
                        + ", found " + tpe.getEnclosingElement(),
                        tpe);
                return 0;
            }
            if (tpe.getEnclosingElement() != tpe.getGenericElement()) {
                error("unexpected generic element; expected: " + tpe.getGenericElement()
                        + ", found " + tpe.getEnclosingElement(),
                        tpe);
                return 0;
            }
        }
        note("verified " + e, e);
        return 1;
    }

    void note(String msg) {
        messager.printMessage(Diagnostic.Kind.NOTE, msg);
    }

    void note(String msg, Element e) {
        messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
    }

    void error(String msg, Element e) {
        messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
    }

    void error(String msg) {
        messager.printMessage(Diagnostic.Kind.ERROR, msg);
    }

    // additional generic elements to test
    <X> X m(X x) { return x; }

    interface Intf<X> { X m() ; }

    class Class<X> {
        <Y> Class() { }
    }

    final int expect = 5;  // top level class, plus preceding examples
}