test/tools/javac/api/TestContainTypes.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6981185
aoqi@0 27 * @summary com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType
aoqi@0 28 * @run main TestContainTypes
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 import java.net.URI;
aoqi@0 32 import java.util.Arrays;
aoqi@0 33 import java.util.HashSet;
aoqi@0 34 import java.util.Set;
aoqi@0 35 import javax.annotation.processing.AbstractProcessor;
aoqi@0 36 import javax.annotation.processing.RoundEnvironment;
aoqi@0 37 import javax.lang.model.element.Element;
aoqi@0 38 import javax.lang.model.element.TypeElement;
aoqi@0 39 import javax.lang.model.element.ExecutableElement;
aoqi@0 40 import javax.lang.model.type.TypeMirror;
aoqi@0 41 import javax.lang.model.type.DeclaredType;
aoqi@0 42 import javax.tools.JavaCompiler;
aoqi@0 43 import javax.tools.JavaFileObject;
aoqi@0 44 import javax.tools.SimpleJavaFileObject;
aoqi@0 45 import javax.tools.ToolProvider;
aoqi@0 46
aoqi@0 47 import com.sun.source.util.JavacTask;
aoqi@0 48 import javax.annotation.processing.SupportedSourceVersion;
aoqi@0 49 import javax.lang.model.SourceVersion;
aoqi@0 50
aoqi@0 51 public class TestContainTypes {
aoqi@0 52
aoqi@0 53 enum ClassType {
aoqi@0 54 OBJECT("Object"),
aoqi@0 55 NUMBER("Number"),
aoqi@0 56 INTEGER("Integer"),
aoqi@0 57 STRING("String");
aoqi@0 58
aoqi@0 59 String classStub;
aoqi@0 60
aoqi@0 61 ClassType(String classStub) {
aoqi@0 62 this.classStub = classStub;
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 boolean subtypeOf(ClassType that) {
aoqi@0 66 switch (that) {
aoqi@0 67 case OBJECT: return true;
aoqi@0 68 case NUMBER: return this == NUMBER || this == INTEGER;
aoqi@0 69 case INTEGER: return this == INTEGER;
aoqi@0 70 case STRING: return this == STRING;
aoqi@0 71 default: throw new AssertionError("Bad type kind in subtyping test");
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 enum ParameterType {
aoqi@0 77 INVARIANT("List<#1>"),
aoqi@0 78 COVARIANT("List<? extends #1>"),
aoqi@0 79 CONTRAVARIANT("List<? super #1>"),
aoqi@0 80 BIVARIANT("List<?>");
aoqi@0 81
aoqi@0 82 String paramTypeStub;
aoqi@0 83
aoqi@0 84 ParameterType(String paramTypeStub) {
aoqi@0 85 this.paramTypeStub = paramTypeStub;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 String instantiate(ClassType ct) {
aoqi@0 89 return paramTypeStub.replace("#1", ct.classStub);
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 static boolean contains(ParameterType pt1, ClassType ct1,
aoqi@0 93 ParameterType pt2, ClassType ct2) {
aoqi@0 94 switch (pt1) {
aoqi@0 95 case INVARIANT: return (pt2 == INVARIANT && ct1 == ct2) ||
aoqi@0 96 (pt2 == CONTRAVARIANT && ct1 == ct2 && ct1 == ClassType.OBJECT);
aoqi@0 97 case COVARIANT: return ((pt2 == INVARIANT || pt2 == COVARIANT) &&
aoqi@0 98 ct2.subtypeOf(ct1)) ||
aoqi@0 99 (ct1 == ClassType.OBJECT);
aoqi@0 100 case CONTRAVARIANT: return (pt2 == INVARIANT || pt2 == CONTRAVARIANT) &&
aoqi@0 101 ct1.subtypeOf(ct2);
aoqi@0 102 case BIVARIANT: return true;
aoqi@0 103 default: throw new AssertionError("Bad type kind in containment test");
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 static class JavaSource extends SimpleJavaFileObject {
aoqi@0 109
aoqi@0 110 final static String sourceStub =
aoqi@0 111 "import java.util.List;\n" +
aoqi@0 112 "@interface ToCheck {}\n" +
aoqi@0 113 "class Test {\n" +
aoqi@0 114 " @ToCheck void test(#A a, #B b) {}\n" +
aoqi@0 115 "}\n";
aoqi@0 116
aoqi@0 117 String source;
aoqi@0 118
aoqi@0 119 public JavaSource(String typeA, String typeB) {
aoqi@0 120 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 121 source = sourceStub.replace("#A", typeA).replace("#B", typeB);
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 @Override
aoqi@0 125 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 126 return source;
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public static void main(String... args) throws Exception {
aoqi@0 131 for (ClassType ctA : ClassType.values()) {
aoqi@0 132 for (ParameterType ptA : ParameterType.values()) {
aoqi@0 133 for (ClassType ctB : ClassType.values()) {
aoqi@0 134 for (ParameterType ptB : ParameterType.values()) {
aoqi@0 135 compileAndCheck(ptA, ctA, ptB, ctB);
aoqi@0 136 }
aoqi@0 137 }
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
aoqi@0 143 JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
aoqi@0 144 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
aoqi@0 145 JavacTask ct = (JavacTask)tool.getTask(null, null, null,
aoqi@0 146 null, null, Arrays.asList(source));
aoqi@0 147 ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
aoqi@0 148 System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));
aoqi@0 149 System.err.println("B = " + ptB +" / " + ptB.instantiate(ctB));
aoqi@0 150 System.err.println("Source = " + source.source);
aoqi@0 151 ct.analyze();
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 @SupportedSourceVersion(SourceVersion.RELEASE_7)
aoqi@0 155 static class ContainTypesTester extends AbstractProcessor {
aoqi@0 156
aoqi@0 157 boolean expected;
aoqi@0 158 JavaSource source;
aoqi@0 159
aoqi@0 160 ContainTypesTester(boolean expected, JavaSource source) {
aoqi@0 161 this.expected = expected;
aoqi@0 162 this.source = source;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 @Override
aoqi@0 166 public Set<String> getSupportedAnnotationTypes() {
aoqi@0 167 Set<String> supportedAnnos = new HashSet();
aoqi@0 168 supportedAnnos.add("*");
aoqi@0 169 return supportedAnnos;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 private void error(String msg) {
aoqi@0 173 System.err.println(source.source);
aoqi@0 174 throw new AssertionError(msg);
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 @Override
aoqi@0 178 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
aoqi@0 179 if (roundEnv.getRootElements().size() == 0) {
aoqi@0 180 return true;
aoqi@0 181 }
aoqi@0 182 if (annotations.isEmpty() || annotations.size() > 1) {
aoqi@0 183 error("no anno found/wrong number of annotations found: " + annotations.size());
aoqi@0 184 }
aoqi@0 185 TypeElement anno = (TypeElement)annotations.toArray()[0];
aoqi@0 186 Set<? extends Element> annoElems = roundEnv.getElementsAnnotatedWith(anno);
aoqi@0 187 if (annoElems.isEmpty() || annoElems.size() > 1) {
aoqi@0 188 error("no annotated element found/wrong number of annotated elements found: " + annoElems.size());
aoqi@0 189 }
aoqi@0 190 Element annoElement = (Element)annoElems.toArray()[0];
aoqi@0 191 if (!(annoElement instanceof ExecutableElement)) {
aoqi@0 192 error("annotated element must be a method");
aoqi@0 193 }
aoqi@0 194 ExecutableElement method = (ExecutableElement)annoElement;
aoqi@0 195 if (method.getParameters().size() != 2) {
aoqi@0 196 error("annotated method must have 2 arguments");
aoqi@0 197 }
aoqi@0 198 DeclaredType d1 = (DeclaredType)method.getParameters().get(0).asType();
aoqi@0 199 DeclaredType d2 = (DeclaredType)method.getParameters().get(1).asType();
aoqi@0 200 if (d1.getTypeArguments().size() != 1 ||
aoqi@0 201 d1.getTypeArguments().size() != 1) {
aoqi@0 202 error("parameter type must be generic in one type-variable");
aoqi@0 203 }
aoqi@0 204 TypeMirror t1 = d1.getTypeArguments().get(0);
aoqi@0 205 TypeMirror t2 = d2.getTypeArguments().get(0);
aoqi@0 206
aoqi@0 207 if (processingEnv.getTypeUtils().contains(t1, t2) != expected) {
aoqi@0 208 error("bad type containment result\n" +
aoqi@0 209 "t1 : " + t1 +"\n" +
aoqi@0 210 "t2 : " + t2 +"\n" +
aoqi@0 211 "expected answer : " + expected +"\n");
aoqi@0 212 }
aoqi@0 213 return true;
aoqi@0 214 }
aoqi@0 215 }
aoqi@0 216 }

mercurial