6981185: com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType

Tue, 07 Sep 2010 17:32:52 +0100

author
mcimadamore
date
Tue, 07 Sep 2010 17:32:52 +0100
changeset 675
12d8f7e417fd
parent 674
584365f256a7
child 676
bfdfc13fe641

6981185: com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType
Summary: wrong implementation is causing trivial containment tests to fail unexpectedly (when such tests are executed using compiler API)
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/model/JavacTypes.java file | annotate | diff | comparison | revisions
test/tools/javac/api/TestContainTypes.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/model/JavacTypes.java	Tue Sep 07 17:32:27 2010 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/model/JavacTypes.java	Tue Sep 07 17:32:52 2010 +0100
     1.3 @@ -103,7 +103,7 @@
     1.4      public boolean contains(TypeMirror t1, TypeMirror t2) {
     1.5          validateTypeNotIn(t1, EXEC_OR_PKG);
     1.6          validateTypeNotIn(t2, EXEC_OR_PKG);
     1.7 -        return ((Type) t1).contains((Type) t2);
     1.8 +        return types.containsType((Type) t1, (Type) t2);
     1.9      }
    1.10  
    1.11      public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/api/TestContainTypes.java	Tue Sep 07 17:32:52 2010 +0100
     2.3 @@ -0,0 +1,216 @@
     2.4 +/*
     2.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 6981185
    2.30 + * @summary  com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType
    2.31 + * @run main TestContainTypes
    2.32 + */
    2.33 +
    2.34 +import java.net.URI;
    2.35 +import java.util.Arrays;
    2.36 +import java.util.HashSet;
    2.37 +import java.util.Set;
    2.38 +import javax.annotation.processing.AbstractProcessor;
    2.39 +import javax.annotation.processing.RoundEnvironment;
    2.40 +import javax.lang.model.element.Element;
    2.41 +import javax.lang.model.element.TypeElement;
    2.42 +import javax.lang.model.element.ExecutableElement;
    2.43 +import javax.lang.model.type.TypeMirror;
    2.44 +import javax.lang.model.type.DeclaredType;
    2.45 +import javax.tools.JavaCompiler;
    2.46 +import javax.tools.JavaFileObject;
    2.47 +import javax.tools.SimpleJavaFileObject;
    2.48 +import javax.tools.ToolProvider;
    2.49 +
    2.50 +import com.sun.source.util.JavacTask;
    2.51 +import javax.annotation.processing.SupportedSourceVersion;
    2.52 +import javax.lang.model.SourceVersion;
    2.53 +
    2.54 +public class TestContainTypes {
    2.55 +
    2.56 +    enum ClassType {
    2.57 +        OBJECT("Object"),
    2.58 +        NUMBER("Number"),
    2.59 +        INTEGER("Integer"),
    2.60 +        STRING("String");
    2.61 +
    2.62 +        String classStub;
    2.63 +
    2.64 +        ClassType(String classStub) {
    2.65 +            this.classStub = classStub;
    2.66 +        }
    2.67 +
    2.68 +        boolean subtypeOf(ClassType that) {
    2.69 +            switch (that) {
    2.70 +                case OBJECT: return true;
    2.71 +                case NUMBER: return this == NUMBER || this == INTEGER;
    2.72 +                case INTEGER: return this == INTEGER;
    2.73 +                case STRING: return this == STRING;
    2.74 +                default: throw new AssertionError("Bad type kind in subtyping test");
    2.75 +            }
    2.76 +        }
    2.77 +    }
    2.78 +
    2.79 +    enum ParameterType {
    2.80 +        INVARIANT("List<#1>"),
    2.81 +        COVARIANT("List<? extends #1>"),
    2.82 +        CONTRAVARIANT("List<? super #1>"),
    2.83 +        BIVARIANT("List<?>");
    2.84 +
    2.85 +        String paramTypeStub;
    2.86 +
    2.87 +        ParameterType(String paramTypeStub) {
    2.88 +            this.paramTypeStub = paramTypeStub;
    2.89 +        }
    2.90 +
    2.91 +        String instantiate(ClassType ct) {
    2.92 +            return paramTypeStub.replace("#1", ct.classStub);
    2.93 +        }
    2.94 +
    2.95 +        static boolean contains(ParameterType pt1, ClassType ct1,
    2.96 +                ParameterType pt2, ClassType ct2) {
    2.97 +            switch (pt1) {
    2.98 +                case INVARIANT: return (pt2 == INVARIANT && ct1 == ct2) ||
    2.99 +                                           (pt2 == CONTRAVARIANT && ct1 == ct2 && ct1 == ClassType.OBJECT);
   2.100 +                case COVARIANT: return ((pt2 == INVARIANT || pt2 == COVARIANT) &&
   2.101 +                                            ct2.subtypeOf(ct1)) ||
   2.102 +                                            (ct1 == ClassType.OBJECT);
   2.103 +                case CONTRAVARIANT: return (pt2 == INVARIANT || pt2 == CONTRAVARIANT) &&
   2.104 +                                            ct1.subtypeOf(ct2);
   2.105 +                case BIVARIANT: return true;
   2.106 +                default: throw new AssertionError("Bad type kind in containment test");
   2.107 +            }
   2.108 +        }
   2.109 +    }
   2.110 +
   2.111 +    static class JavaSource extends SimpleJavaFileObject {
   2.112 +
   2.113 +        final static String sourceStub =
   2.114 +                        "import java.util.List;\n" +
   2.115 +                        "@interface ToCheck {}\n" +
   2.116 +                        "class Test {\n" +
   2.117 +                        "   @ToCheck void test(#A a, #B b) {}\n" +
   2.118 +                        "}\n";
   2.119 +
   2.120 +        String source;
   2.121 +
   2.122 +        public JavaSource(String typeA, String typeB) {
   2.123 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   2.124 +            source = sourceStub.replace("#A", typeA).replace("#B", typeB);
   2.125 +        }
   2.126 +
   2.127 +        @Override
   2.128 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   2.129 +            return source;
   2.130 +        }
   2.131 +    }
   2.132 +
   2.133 +    public static void main(String... args) throws Exception {
   2.134 +        for (ClassType ctA : ClassType.values()) {
   2.135 +            for (ParameterType ptA : ParameterType.values()) {
   2.136 +                for (ClassType ctB : ClassType.values()) {
   2.137 +                    for (ParameterType ptB : ParameterType.values()) {
   2.138 +                        compileAndCheck(ptA, ctA, ptB, ctB);
   2.139 +                    }
   2.140 +                }
   2.141 +            }
   2.142 +        }
   2.143 +    }
   2.144 +
   2.145 +    static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
   2.146 +        JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
   2.147 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   2.148 +        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
   2.149 +                null, null, Arrays.asList(source));
   2.150 +        ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
   2.151 +        System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));
   2.152 +        System.err.println("B = " + ptB +" / " + ptB.instantiate(ctB));
   2.153 +        System.err.println("Source = " + source.source);
   2.154 +        ct.analyze();
   2.155 +    }
   2.156 +
   2.157 +    @SupportedSourceVersion(SourceVersion.RELEASE_7)
   2.158 +    static class ContainTypesTester extends AbstractProcessor {
   2.159 +
   2.160 +        boolean expected;
   2.161 +        JavaSource source;
   2.162 +
   2.163 +        ContainTypesTester(boolean expected, JavaSource source) {
   2.164 +            this.expected = expected;
   2.165 +            this.source = source;
   2.166 +        }
   2.167 +
   2.168 +        @Override
   2.169 +        public Set<String> getSupportedAnnotationTypes() {
   2.170 +            Set<String> supportedAnnos = new HashSet();
   2.171 +            supportedAnnos.add("*");
   2.172 +            return supportedAnnos;
   2.173 +        }
   2.174 +
   2.175 +        private void error(String msg) {
   2.176 +            System.err.println(source.source);
   2.177 +            throw new AssertionError(msg);
   2.178 +        }
   2.179 +
   2.180 +        @Override
   2.181 +        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   2.182 +            if (roundEnv.getRootElements().size() == 0) {
   2.183 +                return true;
   2.184 +            }
   2.185 +            if (annotations.isEmpty() || annotations.size() > 1) {
   2.186 +                error("no anno found/wrong number of annotations found: " + annotations.size());
   2.187 +            }
   2.188 +            TypeElement anno = (TypeElement)annotations.toArray()[0];
   2.189 +            Set<? extends Element> annoElems = roundEnv.getElementsAnnotatedWith(anno);
   2.190 +            if (annoElems.isEmpty() || annoElems.size() > 1) {
   2.191 +                error("no annotated element found/wrong number of annotated elements found: " + annoElems.size());
   2.192 +            }
   2.193 +            Element annoElement = (Element)annoElems.toArray()[0];
   2.194 +            if (!(annoElement instanceof ExecutableElement)) {
   2.195 +                error("annotated element must be a method");
   2.196 +            }
   2.197 +            ExecutableElement method = (ExecutableElement)annoElement;
   2.198 +            if (method.getParameters().size() != 2) {
   2.199 +                error("annotated method must have 2 arguments");
   2.200 +            }
   2.201 +            DeclaredType d1 = (DeclaredType)method.getParameters().get(0).asType();
   2.202 +            DeclaredType d2 = (DeclaredType)method.getParameters().get(1).asType();
   2.203 +            if (d1.getTypeArguments().size() != 1 ||
   2.204 +                    d1.getTypeArguments().size() != 1) {
   2.205 +                error("parameter type must be generic in one type-variable");
   2.206 +            }
   2.207 +            TypeMirror t1 = d1.getTypeArguments().get(0);
   2.208 +            TypeMirror t2 = d2.getTypeArguments().get(0);
   2.209 +
   2.210 +            if (processingEnv.getTypeUtils().contains(t1, t2) != expected) {
   2.211 +                error("bad type containment result\n" +
   2.212 +                        "t1 : " + t1 +"\n" +
   2.213 +                        "t2 : " + t2 +"\n" +
   2.214 +                        "expected answer : " + expected +"\n");
   2.215 +            }
   2.216 +            return true;
   2.217 +        }
   2.218 +    }
   2.219 +}

mercurial