changeset 39914:cebd92646695

8161277: javax.lang.model.util.Types.isSameType(...) returns true on wildcards Reviewed-by: mcimadamore
author vromero
date Wed, 27 Jul 2016 07:07:10 -0700
parents e8373543a3f0
children 5d1e0740e709
files langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java langtools/test/tools/javac/T8161277/IsSameTypeWildcardTest.java langtools/test/tools/lib/types/TypeHarness.java
diffstat 3 files changed, 90 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java	Wed Jul 05 22:00:06 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java	Wed Jul 27 07:07:10 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -85,6 +85,9 @@
 
     @DefinedBy(Api.LANGUAGE_MODEL)
     public boolean isSameType(TypeMirror t1, TypeMirror t2) {
+        if (t1.getKind() == TypeKind.WILDCARD || t2.getKind() == TypeKind.WILDCARD) {
+            return false;
+        }
         return types.isSameType((Type) t1, (Type) t2);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8161277/IsSameTypeWildcardTest.java	Wed Jul 27 07:07:10 2016 -0700
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8161277
+ * @summary javax.lang.model.util.Types.isSameType(...) returns true on wildcards
+ * @library /tools/lib/types
+ * @modules jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.main
+ *          jdk.compiler/com.sun.tools.javac.code
+ *          jdk.compiler/com.sun.tools.javac.comp
+ *          jdk.compiler/com.sun.tools.javac.tree
+ *          jdk.compiler/com.sun.tools.javac.util
+ *          jdk.compiler/com.sun.tools.javac.file
+ *          jdk.compiler/com.sun.tools.javac.model
+ * @build TypeHarness
+ * @run main IsSameTypeWildcardTest
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.sun.tools.javac.code.Type;
+import com.sun.tools.javac.util.Assert;
+import com.sun.tools.javac.model.JavacTypes;
+
+public class IsSameTypeWildcardTest extends TypeHarness {
+    StrToTypeFactory strToTypeFactory;
+    JavacTypes javacTypes;
+
+    public static void main(String... args) throws Exception {
+        new IsSameTypeWildcardTest().runTest();
+    }
+
+    public IsSameTypeWildcardTest() {
+        javacTypes = JavacTypes.instance(context);
+    }
+
+    void runTest() {
+        List<String> imports = new ArrayList<>();
+        imports.add("java.util.*");
+        strToTypeFactory = new StrToTypeFactory(null, imports, null);
+
+        Type listOfWildcard = strToTypeFactory.getType("List<?>");
+        com.sun.tools.javac.util.List<Type> arguments = listOfWildcard.getTypeArguments();
+        Assert.check(!javacTypes.isSameType(arguments.head, arguments.head),
+                "if any argument is a wildcard then result must be false");
+    }
+}
--- a/langtools/test/tools/lib/types/TypeHarness.java	Wed Jul 05 22:00:06 2017 +0200
+++ b/langtools/test/tools/lib/types/TypeHarness.java	Wed Jul 27 07:07:10 2016 -0700
@@ -22,6 +22,7 @@
  */
 
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
@@ -82,6 +83,7 @@
  * The code then verifies that {@code [X:=Object,Y:=Object]A<X,Y> == A<Object,Object>}.
  *
  * @author mcimadamore
+ * @author vromero
  */
 public class TypeHarness {
 
@@ -91,19 +93,20 @@
     protected Names names;
     protected ReusableJavaCompiler tool;
     protected Infer infer;
+    protected Context context;
 
     protected Factory fac;
 
     protected TypeHarness() {
-        Context ctx = new Context();
-        JavacFileManager.preRegister(ctx);
-        MyAttr.preRegister(ctx);
-        tool = new ReusableJavaCompiler(ctx);
-        types = Types.instance(ctx);
-        infer = Infer.instance(ctx);
-        chk = Check.instance(ctx);
-        predef = Symtab.instance(ctx);
-        names = Names.instance(ctx);
+        context = new Context();
+        JavacFileManager.preRegister(context);
+        MyAttr.preRegister(context);
+        tool = new ReusableJavaCompiler(context);
+        types = Types.instance(context);
+        infer = Infer.instance(context);
+        chk = Check.instance(context);
+        predef = Symtab.instance(context);
+        names = Names.instance(context);
         fac = new Factory();
     }
 
@@ -411,8 +414,8 @@
         public StrToTypeFactory(String pkg, java.util.List<String> imports, java.util.List<String> typeVarDecls) {
             this.pkg = pkg;
             this.imports = imports;
-            this.typeVarDecls = typeVarDecls;
-            this.typeVariables = from(typeVarDecls.stream()
+            this.typeVarDecls = typeVarDecls == null ? new ArrayList<>() : typeVarDecls;
+            this.typeVariables = from(this.typeVarDecls.stream()
                     .map(this::typeVarName)
                     .map(this::getType)
                     .collect(Collectors.toList())
@@ -420,7 +423,7 @@
         }
 
         TypeVar getTypeVarFromStr(String name) {
-            if (typeVarDecls == null) {
+            if (typeVarDecls.isEmpty()) {
                 return null;
             }
             int index = typeVarDecls.indexOf(name);
@@ -476,7 +479,7 @@
             public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                 String impStmts = imports.size() > 0 ?
                         imports.stream().map(i -> "import " + i + ";").collect(Collectors.joining("\n")) : "";
-                String tvars = typeVarDecls.size() > 0 ?
+                String tvars = !typeVarDecls.isEmpty() ?
                         typeVarDecls.stream().collect(Collectors.joining(",", "<", ">")) : "";
                 return template
                         .replace("#Package", (pkg == null) ? "" : "package " + pkg + ";")