test/tools/javac/varargs/warning/Warn4.java

changeset 1482
954541f13717
parent 962
0ff2bbd38f10
child 1520
5c956be64b9e
     1.1 --- a/test/tools/javac/varargs/warning/Warn4.java	Tue Jan 08 10:17:29 2013 +0100
     1.2 +++ b/test/tools/javac/varargs/warning/Warn4.java	Tue Jan 08 13:47:57 2013 +0000
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2010, 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 @@ -26,10 +26,11 @@
    1.11   * @bug     6945418 6993978
    1.12   * @summary Project Coin: Simplified Varargs Method Invocation
    1.13   * @author  mcimadamore
    1.14 + * @library ../../lib
    1.15 + * @build JavacTestingAbstractThreadedTest
    1.16   * @run main Warn4
    1.17   */
    1.18 -import com.sun.source.util.JavacTask;
    1.19 -import com.sun.tools.javac.api.JavacTool;
    1.20 +
    1.21  import java.net.URI;
    1.22  import java.util.Arrays;
    1.23  import java.util.Set;
    1.24 @@ -38,16 +39,19 @@
    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 +import com.sun.source.util.JavacTask;
    1.31  
    1.32 -public class Warn4 {
    1.33 +public class Warn4
    1.34 +    extends JavacTestingAbstractThreadedTest
    1.35 +    implements Runnable {
    1.36  
    1.37      final static Warning[] error = null;
    1.38      final static Warning[] none = new Warning[] {};
    1.39      final static Warning[] vararg = new Warning[] { Warning.VARARGS };
    1.40      final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED };
    1.41 -    final static Warning[] both = new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
    1.42 +    final static Warning[] both =
    1.43 +            new Warning[] { Warning.VARARGS, Warning.UNCHECKED };
    1.44  
    1.45      enum Warning {
    1.46          UNCHECKED("generic.array.creation"),
    1.47 @@ -59,8 +63,10 @@
    1.48              this.key = key;
    1.49          }
    1.50  
    1.51 -        boolean isSuppressed(TrustMe trustMe, SourceLevel source, SuppressLevel suppressLevelClient,
    1.52 -                SuppressLevel suppressLevelDecl, ModifierKind modKind) {
    1.53 +        boolean isSuppressed(TrustMe trustMe, SourceLevel source,
    1.54 +                SuppressLevel suppressLevelClient,
    1.55 +                SuppressLevel suppressLevelDecl,
    1.56 +                ModifierKind modKind) {
    1.57              switch(this) {
    1.58                  case VARARGS:
    1.59                      return source == SourceLevel.JDK_6 ||
    1.60 @@ -68,7 +74,8 @@
    1.61                              trustMe == TrustMe.TRUST;
    1.62                  case UNCHECKED:
    1.63                      return suppressLevelClient == SuppressLevel.UNCHECKED ||
    1.64 -                        (trustMe == TrustMe.TRUST && modKind != ModifierKind.NONE && source == SourceLevel.JDK_7);
    1.65 +                        (trustMe == TrustMe.TRUST && modKind !=
    1.66 +                            ModifierKind.NONE && source == SourceLevel.JDK_7);
    1.67              }
    1.68  
    1.69              SuppressLevel supLev = this == VARARGS ?
    1.70 @@ -172,13 +179,13 @@
    1.71                              for (Signature vararg_meth : Signature.values()) {
    1.72                                  for (Signature client_meth : Signature.values()) {
    1.73                                      if (vararg_meth.isApplicableTo(client_meth)) {
    1.74 -                                        test(sourceLevel,
    1.75 +                                        pool.execute(new Warn4(sourceLevel,
    1.76                                                  trustMe,
    1.77                                                  suppressLevelClient,
    1.78                                                  suppressLevelDecl,
    1.79                                                  modKind,
    1.80                                                  vararg_meth,
    1.81 -                                                client_meth);
    1.82 +                                                client_meth));
    1.83                                      }
    1.84                                  }
    1.85                              }
    1.86 @@ -187,63 +194,82 @@
    1.87                  }
    1.88              }
    1.89          }
    1.90 +
    1.91 +        checkAfterExec();
    1.92      }
    1.93  
    1.94 -    // Create a single file manager and reuse it for each compile to save time.
    1.95 -    static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
    1.96 +    SourceLevel sourceLevel;
    1.97 +    TrustMe trustMe;
    1.98 +    SuppressLevel suppressLevelClient;
    1.99 +    SuppressLevel suppressLevelDecl;
   1.100 +    ModifierKind modKind;
   1.101 +    Signature vararg_meth;
   1.102 +    Signature client_meth;
   1.103 +    DiagnosticChecker diagChecker;
   1.104  
   1.105 -    static void test(SourceLevel sourceLevel, TrustMe trustMe, SuppressLevel suppressLevelClient,
   1.106 -            SuppressLevel suppressLevelDecl, ModifierKind modKind, Signature vararg_meth, Signature client_meth) throws Exception {
   1.107 +    public Warn4(SourceLevel sourceLevel, TrustMe trustMe,
   1.108 +            SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl,
   1.109 +            ModifierKind modKind, Signature vararg_meth, Signature client_meth) {
   1.110 +        this.sourceLevel = sourceLevel;
   1.111 +        this.trustMe = trustMe;
   1.112 +        this.suppressLevelClient = suppressLevelClient;
   1.113 +        this.suppressLevelDecl = suppressLevelDecl;
   1.114 +        this.modKind = modKind;
   1.115 +        this.vararg_meth = vararg_meth;
   1.116 +        this.client_meth = client_meth;
   1.117 +        this.diagChecker = new DiagnosticChecker();
   1.118 +    }
   1.119 +
   1.120 +    @Override
   1.121 +    public void run() {
   1.122 +        int id = checkCount.incrementAndGet();
   1.123          final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.124 -        JavaSource source = new JavaSource(trustMe, suppressLevelClient, suppressLevelDecl, modKind, vararg_meth, client_meth);
   1.125 -        DiagnosticChecker dc = new DiagnosticChecker();
   1.126 -        JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
   1.127 +        JavaSource source = new JavaSource(id);
   1.128 +        JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), diagChecker,
   1.129                  Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey),
   1.130                  null, Arrays.asList(source));
   1.131 -        ct.generate(); //to get mandatory notes
   1.132 -        check(dc.warnings, sourceLevel,
   1.133 -                new boolean[] {vararg_meth.giveUnchecked(client_meth),
   1.134 -                               vararg_meth.giveVarargs(client_meth)},
   1.135 -                source, trustMe, suppressLevelClient, suppressLevelDecl, modKind);
   1.136 +        ct.call(); //to get mandatory notes
   1.137 +        check(source, new boolean[] {vararg_meth.giveUnchecked(client_meth),
   1.138 +                               vararg_meth.giveVarargs(client_meth)});
   1.139      }
   1.140  
   1.141 -    static void check(Set<Warning> warnings, SourceLevel sourceLevel, boolean[] warnArr, JavaSource source,
   1.142 -            TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, ModifierKind modKind) {
   1.143 +    void check(JavaSource source, boolean[] warnArr) {
   1.144          boolean badOutput = false;
   1.145          for (Warning wkind : Warning.values()) {
   1.146              boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel,
   1.147                      suppressLevelClient, suppressLevelDecl, modKind);
   1.148              System.out.println("SUPPRESSED = " + isSuppressed);
   1.149 -            badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) != warnings.contains(wkind);
   1.150 +            badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) !=
   1.151 +                    diagChecker.warnings.contains(wkind);
   1.152          }
   1.153          if (badOutput) {
   1.154              throw new Error("invalid diagnostics for source:\n" +
   1.155                      source.getCharContent(true) +
   1.156                      "\nExpected unchecked warning: " + warnArr[0] +
   1.157                      "\nExpected unsafe vararg warning: " + warnArr[1] +
   1.158 -                    "\nWarnings: " + warnings +
   1.159 +                    "\nWarnings: " + diagChecker.warnings +
   1.160                      "\nSource level: " + sourceLevel);
   1.161          }
   1.162      }
   1.163  
   1.164 -    static class JavaSource extends SimpleJavaFileObject {
   1.165 +    class JavaSource extends SimpleJavaFileObject {
   1.166  
   1.167          String source;
   1.168  
   1.169 -        public JavaSource(TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl,
   1.170 -                ModifierKind modKind, Signature vararg_meth, Signature client_meth) {
   1.171 -            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.172 +        public JavaSource(int id) {
   1.173 +            super(URI.create(String.format("myfo:/Test%d.java", id)),
   1.174 +                    JavaFileObject.Kind.SOURCE);
   1.175              String meth1 = vararg_meth.template.replace("#arity", "...");
   1.176              meth1 = meth1.replace("#name", "m");
   1.177              meth1 = meth1.replace("#body", "");
   1.178 -            meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() + modKind.mod + meth1;
   1.179 +            meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() +
   1.180 +                    modKind.mod + meth1;
   1.181              String meth2 = client_meth.template.replace("#arity", "");
   1.182              meth2 = meth2.replace("#name", "test");
   1.183              meth2 = meth2.replace("#body", "m(arg);");
   1.184              meth2 = suppressLevelClient.getSuppressAnno() + meth2;
   1.185 -            source = "import java.util.List;\n" +
   1.186 -                     "class Test {\n" + meth1 +
   1.187 -                     "\n" + meth2 + "\n}\n";
   1.188 +            source = String.format("import java.util.List;\n" +
   1.189 +                     "class Test%s {\n %s \n %s \n } \n", id, meth1, meth2);
   1.190          }
   1.191  
   1.192          @Override
   1.193 @@ -252,7 +278,8 @@
   1.194          }
   1.195      }
   1.196  
   1.197 -    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.198 +    static class DiagnosticChecker
   1.199 +        implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.200  
   1.201          Set<Warning> warnings = new HashSet<>();
   1.202  
   1.203 @@ -267,4 +294,5 @@
   1.204              }
   1.205          }
   1.206      }
   1.207 +
   1.208  }

mercurial