test/tools/javac/cast/intersection/IntersectionTypeCastTest.java

changeset 1482
954541f13717
parent 1436
f6f1fd261f57
child 1511
c7c41a044e7c
     1.1 --- a/test/tools/javac/cast/intersection/IntersectionTypeCastTest.java	Tue Jan 08 10:17:29 2013 +0100
     1.2 +++ b/test/tools/javac/cast/intersection/IntersectionTypeCastTest.java	Tue Jan 08 13:47:57 2013 +0000
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -25,23 +25,26 @@
    1.11   * @test
    1.12   * @bug 8002099
    1.13   * @summary Add support for intersection types in cast expression
    1.14 + * @library ../../lib
    1.15 + * @build JavacTestingAbstractThreadedTest
    1.16 + * @run main/timeout=360 IntersectionTypeCastTest
    1.17   */
    1.18  
    1.19 -import com.sun.source.util.JavacTask;
    1.20 -import com.sun.tools.javac.util.List;
    1.21 -import com.sun.tools.javac.util.ListBuffer;
    1.22  import java.net.URI;
    1.23  import java.util.Arrays;
    1.24  import javax.tools.Diagnostic;
    1.25  import javax.tools.JavaCompiler;
    1.26  import javax.tools.JavaFileObject;
    1.27  import javax.tools.SimpleJavaFileObject;
    1.28 -import javax.tools.StandardJavaFileManager;
    1.29  import javax.tools.ToolProvider;
    1.30  
    1.31 -public class IntersectionTypeCastTest {
    1.32 +import com.sun.source.util.JavacTask;
    1.33 +import com.sun.tools.javac.util.List;
    1.34 +import com.sun.tools.javac.util.ListBuffer;
    1.35  
    1.36 -    static int checkCount = 0;
    1.37 +public class IntersectionTypeCastTest
    1.38 +    extends JavacTestingAbstractThreadedTest
    1.39 +    implements Runnable {
    1.40  
    1.41      interface Type {
    1.42          boolean subtypeOf(Type that);
    1.43 @@ -59,7 +62,8 @@
    1.44          String typeStr;
    1.45          InterfaceKind superInterface;
    1.46  
    1.47 -        InterfaceKind(String declStr, String typeStr, InterfaceKind superInterface) {
    1.48 +        InterfaceKind(String declStr, String typeStr,
    1.49 +                InterfaceKind superInterface) {
    1.50              this.declStr = declStr;
    1.51              this.typeStr = typeStr;
    1.52              this.superInterface = superInterface;
    1.53 @@ -67,7 +71,8 @@
    1.54  
    1.55          @Override
    1.56          public boolean subtypeOf(Type that) {
    1.57 -            return this == that || superInterface == that || that == ClassKind.OBJECT;
    1.58 +            return this == that || superInterface == that ||
    1.59 +                   that == ClassKind.OBJECT;
    1.60          }
    1.61  
    1.62          @Override
    1.63 @@ -88,19 +93,27 @@
    1.64  
    1.65      enum ClassKind implements Type {
    1.66          OBJECT(null, "Object"),
    1.67 -        CA("#M class CA implements A { }\n", "CA", InterfaceKind.A),
    1.68 -        CB("#M class CB implements B { }\n", "CB", InterfaceKind.B),
    1.69 -        CAB("#M class CAB implements A, B { }\n", "CAB", InterfaceKind.A, InterfaceKind.B),
    1.70 -        CC("#M class CC implements C { }\n", "CC", InterfaceKind.C, InterfaceKind.A),
    1.71 -        CCA("#M class CCA implements C, A { }\n", "CCA", InterfaceKind.C, InterfaceKind.A),
    1.72 -        CCB("#M class CCB implements C, B { }\n", "CCB", InterfaceKind.C, InterfaceKind.A, InterfaceKind.B),
    1.73 -        CCAB("#M class CCAB implements C, A, B { }\n", "CCAB", InterfaceKind.C, InterfaceKind.A, InterfaceKind.B);
    1.74 +        CA("#M class CA implements A { }\n", "CA",
    1.75 +           InterfaceKind.A),
    1.76 +        CB("#M class CB implements B { }\n", "CB",
    1.77 +           InterfaceKind.B),
    1.78 +        CAB("#M class CAB implements A, B { }\n", "CAB",
    1.79 +            InterfaceKind.A, InterfaceKind.B),
    1.80 +        CC("#M class CC implements C { }\n", "CC",
    1.81 +           InterfaceKind.C, InterfaceKind.A),
    1.82 +        CCA("#M class CCA implements C, A { }\n", "CCA",
    1.83 +            InterfaceKind.C, InterfaceKind.A),
    1.84 +        CCB("#M class CCB implements C, B { }\n", "CCB",
    1.85 +            InterfaceKind.C, InterfaceKind.A, InterfaceKind.B),
    1.86 +        CCAB("#M class CCAB implements C, A, B { }\n", "CCAB",
    1.87 +             InterfaceKind.C, InterfaceKind.A, InterfaceKind.B);
    1.88  
    1.89          String declTemplate;
    1.90          String typeStr;
    1.91          List<InterfaceKind> superInterfaces;
    1.92  
    1.93 -        ClassKind(String declTemplate, String typeStr, InterfaceKind... superInterfaces) {
    1.94 +        ClassKind(String declTemplate, String typeStr,
    1.95 +                InterfaceKind... superInterfaces) {
    1.96              this.declTemplate = declTemplate;
    1.97              this.typeStr = typeStr;
    1.98              this.superInterfaces = List.from(superInterfaces);
    1.99 @@ -114,7 +127,8 @@
   1.100  
   1.101          @Override
   1.102          public boolean subtypeOf(Type that) {
   1.103 -            return this == that || superInterfaces.contains(that) || that == OBJECT;
   1.104 +            return this == that || superInterfaces.contains(that) ||
   1.105 +                    that == OBJECT;
   1.106          }
   1.107  
   1.108          @Override
   1.109 @@ -170,9 +184,11 @@
   1.110          }
   1.111  
   1.112          String getCast() {
   1.113 -            String temp = kind.castTemplate.replaceAll("#C", types[0].asString());
   1.114 +            String temp = kind.castTemplate.replaceAll("#C",
   1.115 +                    types[0].asString());
   1.116              for (int i = 0; i < kind.interfaceBounds ; i++) {
   1.117 -                temp = temp.replace(String.format("#I%d", i), types[i + 1].asString());
   1.118 +                temp = temp.replace(String.format("#I%d", i),
   1.119 +                                    types[i + 1].asString());
   1.120              }
   1.121              return temp;
   1.122          }
   1.123 @@ -195,7 +211,8 @@
   1.124                              t1.subtypeOf(t2) ||
   1.125                              t2.subtypeOf(t1) ||
   1.126                              (t1.isInterface() && t2.isInterface()) || //side-cast (1)
   1.127 -                            (mod == ModifierKind.NONE && (t1.isInterface() != t2.isInterface())); //side-cast (2)
   1.128 +                            (mod == ModifierKind.NONE &&
   1.129 +                            (t1.isInterface() != t2.isInterface())); //side-cast (2)
   1.130                      if (!compat) return false;
   1.131                  }
   1.132              }
   1.133 @@ -204,18 +221,15 @@
   1.134      }
   1.135  
   1.136      public static void main(String... args) throws Exception {
   1.137 -        //create default shared JavaCompiler - reused across multiple compilations
   1.138 -        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.139 -        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.140 -
   1.141          for (ModifierKind mod : ModifierKind.values()) {
   1.142              for (CastInfo cast1 : allCastInfo()) {
   1.143                  for (CastInfo cast2 : allCastInfo()) {
   1.144 -                    new IntersectionTypeCastTest(mod, cast1, cast2).run(comp, fm);
   1.145 +                    pool.execute(
   1.146 +                        new IntersectionTypeCastTest(mod, cast1, cast2));
   1.147                  }
   1.148              }
   1.149          }
   1.150 -        System.out.println("Total check executed: " + checkCount);
   1.151 +        checkAfterExec();
   1.152      }
   1.153  
   1.154      static List<CastInfo> allCastInfo() {
   1.155 @@ -235,11 +249,14 @@
   1.156                          } else {
   1.157                              for (InterfaceKind intf2 : InterfaceKind.values()) {
   1.158                                  if (kind.interfaceBounds == 2) {
   1.159 -                                    buf.append(new CastInfo(kind, clazz, intf1, intf2));
   1.160 +                                    buf.append(
   1.161 +                                            new CastInfo(kind, clazz, intf1, intf2));
   1.162                                      continue;
   1.163                                  } else {
   1.164                                      for (InterfaceKind intf3 : InterfaceKind.values()) {
   1.165 -                                        buf.append(new CastInfo(kind, clazz, intf1, intf2, intf3));
   1.166 +                                        buf.append(
   1.167 +                                                new CastInfo(kind, clazz, intf1,
   1.168 +                                                             intf2, intf3));
   1.169                                          continue;
   1.170                                      }
   1.171                                  }
   1.172 @@ -265,6 +282,22 @@
   1.173          this.diagChecker = new DiagnosticChecker();
   1.174      }
   1.175  
   1.176 +    @Override
   1.177 +    public void run() {
   1.178 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.179 +
   1.180 +        JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), diagChecker,
   1.181 +                Arrays.asList("-XDallowIntersectionTypes"),
   1.182 +                null, Arrays.asList(source));
   1.183 +        try {
   1.184 +            ct.analyze();
   1.185 +        } catch (Throwable ex) {
   1.186 +            throw new AssertionError("Error thrown when compiling the following code:\n" +
   1.187 +                    source.getCharContent(true));
   1.188 +        }
   1.189 +        check();
   1.190 +    }
   1.191 +
   1.192      class JavaSource extends SimpleJavaFileObject {
   1.193  
   1.194          String bodyTemplate = "class Test {\n" +
   1.195 @@ -282,7 +315,8 @@
   1.196              for (InterfaceKind ik : InterfaceKind.values()) {
   1.197                  source += ik.declStr;
   1.198              }
   1.199 -            source += bodyTemplate.replaceAll("#C1", cast1.getCast()).replaceAll("#C2", cast2.getCast());
   1.200 +            source += bodyTemplate.replaceAll("#C1", cast1.getCast()).
   1.201 +                    replaceAll("#C2", cast2.getCast());
   1.202          }
   1.203  
   1.204          @Override
   1.205 @@ -291,21 +325,11 @@
   1.206          }
   1.207      }
   1.208  
   1.209 -    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.210 -        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.211 -                Arrays.asList("-XDallowIntersectionTypes"), null, Arrays.asList(source));
   1.212 -        try {
   1.213 -            ct.analyze();
   1.214 -        } catch (Throwable ex) {
   1.215 -            throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true));
   1.216 -        }
   1.217 -        check();
   1.218 -    }
   1.219 +    void check() {
   1.220 +        checkCount.incrementAndGet();
   1.221  
   1.222 -    void check() {
   1.223 -        checkCount++;
   1.224 -
   1.225 -        boolean errorExpected = cast1.hasDuplicateTypes() || cast2.hasDuplicateTypes();
   1.226 +        boolean errorExpected = cast1.hasDuplicateTypes() ||
   1.227 +                cast2.hasDuplicateTypes();
   1.228  
   1.229          errorExpected |= !cast2.compatibleWith(mod, cast1);
   1.230  
   1.231 @@ -317,7 +341,8 @@
   1.232          }
   1.233      }
   1.234  
   1.235 -    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.236 +    static class DiagnosticChecker
   1.237 +        implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.238  
   1.239          boolean errorFound;
   1.240  
   1.241 @@ -327,4 +352,5 @@
   1.242              }
   1.243          }
   1.244      }
   1.245 +
   1.246  }

mercurial