1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/TestContainTypes.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,216 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6981185 1.30 + * @summary com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType 1.31 + * @run main TestContainTypes 1.32 + */ 1.33 + 1.34 +import java.net.URI; 1.35 +import java.util.Arrays; 1.36 +import java.util.HashSet; 1.37 +import java.util.Set; 1.38 +import javax.annotation.processing.AbstractProcessor; 1.39 +import javax.annotation.processing.RoundEnvironment; 1.40 +import javax.lang.model.element.Element; 1.41 +import javax.lang.model.element.TypeElement; 1.42 +import javax.lang.model.element.ExecutableElement; 1.43 +import javax.lang.model.type.TypeMirror; 1.44 +import javax.lang.model.type.DeclaredType; 1.45 +import javax.tools.JavaCompiler; 1.46 +import javax.tools.JavaFileObject; 1.47 +import javax.tools.SimpleJavaFileObject; 1.48 +import javax.tools.ToolProvider; 1.49 + 1.50 +import com.sun.source.util.JavacTask; 1.51 +import javax.annotation.processing.SupportedSourceVersion; 1.52 +import javax.lang.model.SourceVersion; 1.53 + 1.54 +public class TestContainTypes { 1.55 + 1.56 + enum ClassType { 1.57 + OBJECT("Object"), 1.58 + NUMBER("Number"), 1.59 + INTEGER("Integer"), 1.60 + STRING("String"); 1.61 + 1.62 + String classStub; 1.63 + 1.64 + ClassType(String classStub) { 1.65 + this.classStub = classStub; 1.66 + } 1.67 + 1.68 + boolean subtypeOf(ClassType that) { 1.69 + switch (that) { 1.70 + case OBJECT: return true; 1.71 + case NUMBER: return this == NUMBER || this == INTEGER; 1.72 + case INTEGER: return this == INTEGER; 1.73 + case STRING: return this == STRING; 1.74 + default: throw new AssertionError("Bad type kind in subtyping test"); 1.75 + } 1.76 + } 1.77 + } 1.78 + 1.79 + enum ParameterType { 1.80 + INVARIANT("List<#1>"), 1.81 + COVARIANT("List<? extends #1>"), 1.82 + CONTRAVARIANT("List<? super #1>"), 1.83 + BIVARIANT("List<?>"); 1.84 + 1.85 + String paramTypeStub; 1.86 + 1.87 + ParameterType(String paramTypeStub) { 1.88 + this.paramTypeStub = paramTypeStub; 1.89 + } 1.90 + 1.91 + String instantiate(ClassType ct) { 1.92 + return paramTypeStub.replace("#1", ct.classStub); 1.93 + } 1.94 + 1.95 + static boolean contains(ParameterType pt1, ClassType ct1, 1.96 + ParameterType pt2, ClassType ct2) { 1.97 + switch (pt1) { 1.98 + case INVARIANT: return (pt2 == INVARIANT && ct1 == ct2) || 1.99 + (pt2 == CONTRAVARIANT && ct1 == ct2 && ct1 == ClassType.OBJECT); 1.100 + case COVARIANT: return ((pt2 == INVARIANT || pt2 == COVARIANT) && 1.101 + ct2.subtypeOf(ct1)) || 1.102 + (ct1 == ClassType.OBJECT); 1.103 + case CONTRAVARIANT: return (pt2 == INVARIANT || pt2 == CONTRAVARIANT) && 1.104 + ct1.subtypeOf(ct2); 1.105 + case BIVARIANT: return true; 1.106 + default: throw new AssertionError("Bad type kind in containment test"); 1.107 + } 1.108 + } 1.109 + } 1.110 + 1.111 + static class JavaSource extends SimpleJavaFileObject { 1.112 + 1.113 + final static String sourceStub = 1.114 + "import java.util.List;\n" + 1.115 + "@interface ToCheck {}\n" + 1.116 + "class Test {\n" + 1.117 + " @ToCheck void test(#A a, #B b) {}\n" + 1.118 + "}\n"; 1.119 + 1.120 + String source; 1.121 + 1.122 + public JavaSource(String typeA, String typeB) { 1.123 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.124 + source = sourceStub.replace("#A", typeA).replace("#B", typeB); 1.125 + } 1.126 + 1.127 + @Override 1.128 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.129 + return source; 1.130 + } 1.131 + } 1.132 + 1.133 + public static void main(String... args) throws Exception { 1.134 + for (ClassType ctA : ClassType.values()) { 1.135 + for (ParameterType ptA : ParameterType.values()) { 1.136 + for (ClassType ctB : ClassType.values()) { 1.137 + for (ParameterType ptB : ParameterType.values()) { 1.138 + compileAndCheck(ptA, ctA, ptB, ctB); 1.139 + } 1.140 + } 1.141 + } 1.142 + } 1.143 + } 1.144 + 1.145 + static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception { 1.146 + JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB)); 1.147 + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 1.148 + JavacTask ct = (JavacTask)tool.getTask(null, null, null, 1.149 + null, null, Arrays.asList(source)); 1.150 + ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source))); 1.151 + System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA)); 1.152 + System.err.println("B = " + ptB +" / " + ptB.instantiate(ctB)); 1.153 + System.err.println("Source = " + source.source); 1.154 + ct.analyze(); 1.155 + } 1.156 + 1.157 + @SupportedSourceVersion(SourceVersion.RELEASE_7) 1.158 + static class ContainTypesTester extends AbstractProcessor { 1.159 + 1.160 + boolean expected; 1.161 + JavaSource source; 1.162 + 1.163 + ContainTypesTester(boolean expected, JavaSource source) { 1.164 + this.expected = expected; 1.165 + this.source = source; 1.166 + } 1.167 + 1.168 + @Override 1.169 + public Set<String> getSupportedAnnotationTypes() { 1.170 + Set<String> supportedAnnos = new HashSet(); 1.171 + supportedAnnos.add("*"); 1.172 + return supportedAnnos; 1.173 + } 1.174 + 1.175 + private void error(String msg) { 1.176 + System.err.println(source.source); 1.177 + throw new AssertionError(msg); 1.178 + } 1.179 + 1.180 + @Override 1.181 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 1.182 + if (roundEnv.getRootElements().size() == 0) { 1.183 + return true; 1.184 + } 1.185 + if (annotations.isEmpty() || annotations.size() > 1) { 1.186 + error("no anno found/wrong number of annotations found: " + annotations.size()); 1.187 + } 1.188 + TypeElement anno = (TypeElement)annotations.toArray()[0]; 1.189 + Set<? extends Element> annoElems = roundEnv.getElementsAnnotatedWith(anno); 1.190 + if (annoElems.isEmpty() || annoElems.size() > 1) { 1.191 + error("no annotated element found/wrong number of annotated elements found: " + annoElems.size()); 1.192 + } 1.193 + Element annoElement = (Element)annoElems.toArray()[0]; 1.194 + if (!(annoElement instanceof ExecutableElement)) { 1.195 + error("annotated element must be a method"); 1.196 + } 1.197 + ExecutableElement method = (ExecutableElement)annoElement; 1.198 + if (method.getParameters().size() != 2) { 1.199 + error("annotated method must have 2 arguments"); 1.200 + } 1.201 + DeclaredType d1 = (DeclaredType)method.getParameters().get(0).asType(); 1.202 + DeclaredType d2 = (DeclaredType)method.getParameters().get(1).asType(); 1.203 + if (d1.getTypeArguments().size() != 1 || 1.204 + d1.getTypeArguments().size() != 1) { 1.205 + error("parameter type must be generic in one type-variable"); 1.206 + } 1.207 + TypeMirror t1 = d1.getTypeArguments().get(0); 1.208 + TypeMirror t2 = d2.getTypeArguments().get(0); 1.209 + 1.210 + if (processingEnv.getTypeUtils().contains(t1, t2) != expected) { 1.211 + error("bad type containment result\n" + 1.212 + "t1 : " + t1 +"\n" + 1.213 + "t2 : " + t2 +"\n" + 1.214 + "expected answer : " + expected +"\n"); 1.215 + } 1.216 + return true; 1.217 + } 1.218 + } 1.219 +}