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     6345812
27  * @summary Validate argument kinds in Types utilities
28  * @author  Scott Seligman
29  * @library /tools/javac/lib
30  * @modules java.compiler
31  *          jdk.compiler
32  * @build   JavacTestingAbstractProcessor TypesBadArg
33  * @compile -processor TypesBadArg -proc:only TypesBadArg.java
34  */
35 
36 import java.util.Set;
37 import javax.annotation.processing.*;
38 import javax.lang.model.element.*;
39 import javax.lang.model.type.*;
40 import javax.lang.model.util.*;
41 
42 public class TypesBadArg extends JavacTestingAbstractProcessor {
43     boolean success = true;
44 
process(Set<? extends TypeElement> tes, RoundEnvironment round)45     public boolean process(Set<? extends TypeElement> tes,
46                            RoundEnvironment round) {
47         if (round.processingOver()) return true;
48 
49         final Elements elements = processingEnv.getElementUtils();
50         final Types types = processingEnv.getTypeUtils();
51 
52         final TypeMirror javaLang =
53             elements.getPackageElement("java.lang").asType();
54 
55         makeBadCall(new Runnable() {
56             public void run() {
57                 types.isSubtype(javaLang, javaLang);
58             }
59         });
60         makeBadCall(new Runnable() {
61             public void run() {
62                 types.isAssignable(javaLang, javaLang);
63             }
64         });
65         makeBadCall(new Runnable() {
66             public void run() {
67                 types.contains(javaLang, javaLang);
68             }
69         });
70         makeBadCall(new Runnable() {
71             public void run() {
72                 types.directSupertypes(javaLang);
73             }
74         });
75         makeBadCall(new Runnable() {
76             public void run() {
77                 types.erasure(javaLang);
78             }
79         });
80         makeBadCall(new Runnable() {
81             public void run() {
82                 types.capture(javaLang);
83             }
84         });
85         makeBadCall(new Runnable() {
86             public void run() {
87                 types.unboxedType(javaLang);
88             }
89         });
90         makeBadCall(new Runnable() {
91             public void run() {
92                 types.unboxedType(types.getNoType(TypeKind.VOID));
93             }
94         });
95         if (! success)
96             throw new AssertionError("Some test(s) failed.");
97         return true;
98     }
99 
makeBadCall(Runnable runnable)100     private void makeBadCall(Runnable runnable) {
101         try {
102             runnable.run();
103             System.out.println("Failure: IllegalArgumentException expected");
104             success = false;
105         } catch (IllegalArgumentException e) {
106             System.out.println("IllegalArgumentException as expected");
107         }
108     }
109 }
110