1/*
2 * Copyright (c) 2006, 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     6423972
27 * @summary Tests TypeParameter.getBounds.
28 * @author  Scott Seligman
29 * @library /tools/javac/lib
30 * @modules java.compiler
31 *          jdk.compiler
32 * @build   JavacTestingAbstractProcessor TypeParamBounds
33 * @compile -processor TypeParamBounds -proc:only TypeParamBounds.java
34 */
35
36import java.util.HashMap;
37import java.util.List;
38import java.util.Map;
39import java.util.Set;
40import javax.annotation.processing.*;
41import javax.lang.model.SourceVersion;
42import javax.lang.model.element.*;
43import javax.lang.model.type.*;
44import javax.lang.model.util.*;
45
46public class TypeParamBounds extends JavacTestingAbstractProcessor {
47    public boolean process(Set<? extends TypeElement> annoTypes,
48                           RoundEnvironment round) {
49        if (!round.processingOver())
50            doit(annoTypes, round);
51        return true;
52    }
53
54    private void doit(Set<? extends TypeElement> annoTypes,
55                      RoundEnvironment round) {
56        TypeElement gen = elements.getTypeElement("TypeParamBounds.Gen");
57
58        // For each type parameter of Gen, compare its bounds with the
59        // bounds that are expected.
60        //
61        for (TypeParameterElement tparam : gen.getTypeParameters()) {
62            System.out.println(tparam);
63            List<? extends TypeMirror> bounds = tparam.getBounds();
64            String[] expected = Gen.boundNames.get(tparam + "");
65
66            if (bounds.size() != expected.length)
67                throw new AssertionError();
68            int i = 0;
69            for (TypeMirror bound : bounds) {
70                Name got = types.asElement(bound).getSimpleName();
71                String shoulda = expected[i++];
72                System.out.println("  " + got);
73                if (!got.contentEquals(shoulda))
74                    throw new AssertionError(shoulda);
75            }
76        }
77    }
78
79
80    // Fodder for the processor
81    static class Gen<T, U extends Object, V extends Number, W extends U,
82                     X extends Runnable, Y extends CharSequence & Runnable,
83                     Z extends Object & Runnable> {
84
85        // The names of the bounds of each type parameter of Gen.
86        static Map<String, String[]> boundNames =
87                new HashMap<String, String[]>();
88
89        static {
90            boundNames.put("T", new String[] {"Object"});
91            boundNames.put("U", new String[] {"Object"});
92            boundNames.put("V", new String[] {"Number"});
93            boundNames.put("W", new String[] {"U"});
94            boundNames.put("X", new String[] {"Runnable"});
95            boundNames.put("Y", new String[] {"CharSequence", "Runnable"});
96            boundNames.put("Z", new String[] {"Object", "Runnable"});
97        }
98    }
99}
100