|
1 /* |
|
2 * Copyright (c) 2010, 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 6981185 |
|
27 * @summary com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType |
|
28 * @run main TestContainTypes |
|
29 */ |
|
30 |
|
31 import java.net.URI; |
|
32 import java.util.Arrays; |
|
33 import java.util.HashSet; |
|
34 import java.util.Set; |
|
35 import javax.annotation.processing.AbstractProcessor; |
|
36 import javax.annotation.processing.RoundEnvironment; |
|
37 import javax.lang.model.element.Element; |
|
38 import javax.lang.model.element.TypeElement; |
|
39 import javax.lang.model.element.ExecutableElement; |
|
40 import javax.lang.model.type.TypeMirror; |
|
41 import javax.lang.model.type.DeclaredType; |
|
42 import javax.tools.JavaCompiler; |
|
43 import javax.tools.JavaFileObject; |
|
44 import javax.tools.SimpleJavaFileObject; |
|
45 import javax.tools.ToolProvider; |
|
46 |
|
47 import com.sun.source.util.JavacTask; |
|
48 import javax.annotation.processing.SupportedSourceVersion; |
|
49 import javax.lang.model.SourceVersion; |
|
50 |
|
51 public class TestContainTypes { |
|
52 |
|
53 enum ClassType { |
|
54 OBJECT("Object"), |
|
55 NUMBER("Number"), |
|
56 INTEGER("Integer"), |
|
57 STRING("String"); |
|
58 |
|
59 String classStub; |
|
60 |
|
61 ClassType(String classStub) { |
|
62 this.classStub = classStub; |
|
63 } |
|
64 |
|
65 boolean subtypeOf(ClassType that) { |
|
66 switch (that) { |
|
67 case OBJECT: return true; |
|
68 case NUMBER: return this == NUMBER || this == INTEGER; |
|
69 case INTEGER: return this == INTEGER; |
|
70 case STRING: return this == STRING; |
|
71 default: throw new AssertionError("Bad type kind in subtyping test"); |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 enum ParameterType { |
|
77 INVARIANT("List<#1>"), |
|
78 COVARIANT("List<? extends #1>"), |
|
79 CONTRAVARIANT("List<? super #1>"), |
|
80 BIVARIANT("List<?>"); |
|
81 |
|
82 String paramTypeStub; |
|
83 |
|
84 ParameterType(String paramTypeStub) { |
|
85 this.paramTypeStub = paramTypeStub; |
|
86 } |
|
87 |
|
88 String instantiate(ClassType ct) { |
|
89 return paramTypeStub.replace("#1", ct.classStub); |
|
90 } |
|
91 |
|
92 static boolean contains(ParameterType pt1, ClassType ct1, |
|
93 ParameterType pt2, ClassType ct2) { |
|
94 switch (pt1) { |
|
95 case INVARIANT: return (pt2 == INVARIANT && ct1 == ct2) || |
|
96 (pt2 == CONTRAVARIANT && ct1 == ct2 && ct1 == ClassType.OBJECT); |
|
97 case COVARIANT: return ((pt2 == INVARIANT || pt2 == COVARIANT) && |
|
98 ct2.subtypeOf(ct1)) || |
|
99 (ct1 == ClassType.OBJECT); |
|
100 case CONTRAVARIANT: return (pt2 == INVARIANT || pt2 == CONTRAVARIANT) && |
|
101 ct1.subtypeOf(ct2); |
|
102 case BIVARIANT: return true; |
|
103 default: throw new AssertionError("Bad type kind in containment test"); |
|
104 } |
|
105 } |
|
106 } |
|
107 |
|
108 static class JavaSource extends SimpleJavaFileObject { |
|
109 |
|
110 final static String sourceStub = |
|
111 "import java.util.List;\n" + |
|
112 "@interface ToCheck {}\n" + |
|
113 "class Test {\n" + |
|
114 " @ToCheck void test(#A a, #B b) {}\n" + |
|
115 "}\n"; |
|
116 |
|
117 String source; |
|
118 |
|
119 public JavaSource(String typeA, String typeB) { |
|
120 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
121 source = sourceStub.replace("#A", typeA).replace("#B", typeB); |
|
122 } |
|
123 |
|
124 @Override |
|
125 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
126 return source; |
|
127 } |
|
128 } |
|
129 |
|
130 public static void main(String... args) throws Exception { |
|
131 for (ClassType ctA : ClassType.values()) { |
|
132 for (ParameterType ptA : ParameterType.values()) { |
|
133 for (ClassType ctB : ClassType.values()) { |
|
134 for (ParameterType ptB : ParameterType.values()) { |
|
135 compileAndCheck(ptA, ctA, ptB, ctB); |
|
136 } |
|
137 } |
|
138 } |
|
139 } |
|
140 } |
|
141 |
|
142 static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception { |
|
143 JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB)); |
|
144 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
|
145 JavacTask ct = (JavacTask)tool.getTask(null, null, null, |
|
146 null, null, Arrays.asList(source)); |
|
147 ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source))); |
|
148 System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA)); |
|
149 System.err.println("B = " + ptB +" / " + ptB.instantiate(ctB)); |
|
150 System.err.println("Source = " + source.source); |
|
151 ct.analyze(); |
|
152 } |
|
153 |
|
154 @SupportedSourceVersion(SourceVersion.RELEASE_7) |
|
155 static class ContainTypesTester extends AbstractProcessor { |
|
156 |
|
157 boolean expected; |
|
158 JavaSource source; |
|
159 |
|
160 ContainTypesTester(boolean expected, JavaSource source) { |
|
161 this.expected = expected; |
|
162 this.source = source; |
|
163 } |
|
164 |
|
165 @Override |
|
166 public Set<String> getSupportedAnnotationTypes() { |
|
167 Set<String> supportedAnnos = new HashSet(); |
|
168 supportedAnnos.add("*"); |
|
169 return supportedAnnos; |
|
170 } |
|
171 |
|
172 private void error(String msg) { |
|
173 System.err.println(source.source); |
|
174 throw new AssertionError(msg); |
|
175 } |
|
176 |
|
177 @Override |
|
178 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
179 if (roundEnv.getRootElements().size() == 0) { |
|
180 return true; |
|
181 } |
|
182 if (annotations.isEmpty() || annotations.size() > 1) { |
|
183 error("no anno found/wrong number of annotations found: " + annotations.size()); |
|
184 } |
|
185 TypeElement anno = (TypeElement)annotations.toArray()[0]; |
|
186 Set<? extends Element> annoElems = roundEnv.getElementsAnnotatedWith(anno); |
|
187 if (annoElems.isEmpty() || annoElems.size() > 1) { |
|
188 error("no annotated element found/wrong number of annotated elements found: " + annoElems.size()); |
|
189 } |
|
190 Element annoElement = (Element)annoElems.toArray()[0]; |
|
191 if (!(annoElement instanceof ExecutableElement)) { |
|
192 error("annotated element must be a method"); |
|
193 } |
|
194 ExecutableElement method = (ExecutableElement)annoElement; |
|
195 if (method.getParameters().size() != 2) { |
|
196 error("annotated method must have 2 arguments"); |
|
197 } |
|
198 DeclaredType d1 = (DeclaredType)method.getParameters().get(0).asType(); |
|
199 DeclaredType d2 = (DeclaredType)method.getParameters().get(1).asType(); |
|
200 if (d1.getTypeArguments().size() != 1 || |
|
201 d1.getTypeArguments().size() != 1) { |
|
202 error("parameter type must be generic in one type-variable"); |
|
203 } |
|
204 TypeMirror t1 = d1.getTypeArguments().get(0); |
|
205 TypeMirror t2 = d2.getTypeArguments().get(0); |
|
206 |
|
207 if (processingEnv.getTypeUtils().contains(t1, t2) != expected) { |
|
208 error("bad type containment result\n" + |
|
209 "t1 : " + t1 +"\n" + |
|
210 "t2 : " + t2 +"\n" + |
|
211 "expected answer : " + expected +"\n"); |
|
212 } |
|
213 return true; |
|
214 } |
|
215 } |
|
216 } |