langtools/test/tools/javac/modules/ConvenientAccessErrorsTest.java
changeset 43272 421ae1e38d2d
parent 43141 7ea6578a9110
child 43866 3195ea126044
equal deleted inserted replaced
43271:ce89609dde7c 43272:421ae1e38d2d
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8169197 8172668
    26  * @bug 8169197 8172668 8173117
    27  * @summary Check convenient errors are produced for inaccessible classes.
    27  * @summary Check convenient errors are produced for inaccessible classes.
    28  * @library /tools/lib
    28  * @library /tools/lib
    29  * @modules jdk.compiler/com.sun.tools.javac.api
    29  * @modules jdk.compiler/com.sun.tools.javac.api
    30  *          jdk.compiler/com.sun.tools.javac.main
    30  *          jdk.compiler/com.sun.tools.javac.main
       
    31  *          jdk.compiler/com.sun.tools.javac.util
    31  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase
    32  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase
    32  * @run main ConvenientAccessErrorsTest
    33  * @run main ConvenientAccessErrorsTest
    33  */
    34  */
    34 
    35 
    35 import java.nio.file.Files;
    36 import java.nio.file.Files;
    36 import java.nio.file.Path;
    37 import java.nio.file.Path;
    37 import java.util.Arrays;
    38 import java.util.Arrays;
    38 import java.util.List;
    39 import java.util.List;
    39 
    40 
       
    41 import com.sun.tools.javac.util.Context;
       
    42 import com.sun.tools.javac.util.Convert;
       
    43 import com.sun.tools.javac.util.Name;
       
    44 import com.sun.tools.javac.util.Names;
    40 import toolbox.JarTask;
    45 import toolbox.JarTask;
    41 import toolbox.JavacTask;
    46 import toolbox.JavacTask;
    42 import toolbox.Task;
    47 import toolbox.Task;
    43 
    48 
    44 public class ConvenientAccessErrorsTest extends ModuleTestBase {
    49 public class ConvenientAccessErrorsTest extends ModuleTestBase {
    56                           "package api; public class Api { public void call() { } }");
    61                           "package api; public class Api { public void call() { } }");
    57         Path src_m2 = src.resolve("m2x");
    62         Path src_m2 = src.resolve("m2x");
    58         tb.writeJavaFiles(src_m2,
    63         tb.writeJavaFiles(src_m2,
    59                           "module m2x { }",
    64                           "module m2x { }",
    60                           "package test; public class Test { api.Api api; }");
    65                           "package test; public class Test { api.Api api; }");
       
    66         Path classes = base.resolve("classes");
       
    67         tb.createDirectories(classes);
       
    68 
       
    69         List<String> log = new JavacTask(tb)
       
    70                 .options("-XDrawDiagnostics",
       
    71                          "--module-source-path", src.toString())
       
    72                 .outdir(classes)
       
    73                 .files(findJavaFiles(src))
       
    74                 .run(Task.Expect.FAIL)
       
    75                 .writeAll()
       
    76                 .getOutputLines(Task.OutputKind.DIRECT);
       
    77 
       
    78         List<String> expected = Arrays.asList(
       
    79                 "Test.java:1:35: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read: m2x, api, m1x)",
       
    80                 "1 error");
       
    81 
       
    82         if (!expected.equals(log))
       
    83             throw new Exception("expected output not found; actual: " + log);
       
    84     }
       
    85 
       
    86     @Test
       
    87     public void testNoDepNested(Path base) throws Exception {
       
    88         Path src = base.resolve("src");
       
    89         Path src_m1 = src.resolve("m1x");
       
    90         tb.writeJavaFiles(src_m1,
       
    91                           "module m1x { exports api; }",
       
    92                           "package api; public class Api { public static class Nested {} }");
       
    93         Path src_m2 = src.resolve("m2x");
       
    94         tb.writeJavaFiles(src_m2,
       
    95                           "module m2x { }",
       
    96                           "package test; public class Test { api.Api.Nested nested; }");
    61         Path classes = base.resolve("classes");
    97         Path classes = base.resolve("classes");
    62         tb.createDirectories(classes);
    98         tb.createDirectories(classes);
    63 
    99 
    64         List<String> log = new JavacTask(tb)
   100         List<String> log = new JavacTask(tb)
    65                 .options("-XDrawDiagnostics",
   101                 .options("-XDrawDiagnostics",
   388                 .writeAll()
   424                 .writeAll()
   389                 .getOutputLines(Task.OutputKind.DIRECT);
   425                 .getOutputLines(Task.OutputKind.DIRECT);
   390 
   426 
   391         List<String> expected = Arrays.asList(
   427         List<String> expected = Arrays.asList(
   392                 "Test.java:1:22: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.not.exported: api, m1x)",
   428                 "Test.java:1:22: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.not.exported: api, m1x)",
   393                 "Test.java:1:49: compiler.err.not.def.access.package.cant.access: api.Api, api, (compiler.misc.not.def.access.not.exported: api, m1x)",
   429                 "1 error");
   394                 "2 errors");
       
   395 
   430 
   396         if (!expected.equals(log))
   431         if (!expected.equals(log))
   397             throw new Exception("expected output not found; actual: " + log);
   432             throw new Exception("expected output not found; actual: " + log);
   398     }
   433     }
   399 
   434 
   591 
   626 
   592         if (!expected.equals(log))
   627         if (!expected.equals(log))
   593             throw new Exception("expected output not found; actual: " + log);
   628             throw new Exception("expected output not found; actual: " + log);
   594     }
   629     }
   595 
   630 
       
   631     @Test
       
   632     public void testInaccessibleInSourceModuleViaBinaryModule(Path base) throws Exception {
       
   633         Path src = base.resolve("src");
       
   634         Path src_m1 = src.resolve("m1x");
       
   635         tb.writeJavaFiles(src_m1,
       
   636                           "@Deprecated module m1x { }");
       
   637         Path src_m2 = src.resolve("m2x");
       
   638         tb.writeJavaFiles(src_m2,
       
   639                           "module m2x { requires transitive m1x; }");
       
   640         Path src_m3 = src.resolve("m3x");
       
   641         tb.writeJavaFiles(src_m3,
       
   642                           "module m3x { requires transitive m2x; exports api; }",
       
   643                           "package api; class Api { }");
       
   644         Path classes = base.resolve("classes");
       
   645         tb.createDirectories(classes);
       
   646 
       
   647         new JavacTask(tb)
       
   648             .options("-XDrawDiagnostics",
       
   649                      "--module-source-path", src.toString())
       
   650             .outdir(classes)
       
   651             .files(findJavaFiles(src))
       
   652             .run()
       
   653             .writeAll();
       
   654 
       
   655         tb.cleanDirectory(classes.resolve("m2x")); //force completion from source if needed
       
   656         Files.delete(classes.resolve("m2x"));
       
   657 
       
   658         tb.cleanDirectory(src_m3); //binary only module
       
   659         Files.delete(src_m3);
       
   660 
       
   661         //m4x does not depend on m1x/m2x/m3x, so cannot access api.Api
       
   662         //but the recovery search should not complete m2x, as that would cause a deprecation warning:
       
   663         Path src_m4 = src.resolve("m4x");
       
   664         tb.writeJavaFiles(src_m4,
       
   665                           "module m4x { }",
       
   666                           "package m4x; public class Test extends api.Api { }");
       
   667 
       
   668         List<String> log = new JavacTask(tb)
       
   669                 .options("-XDrawDiagnostics",
       
   670                          "--module-source-path", src.toString(),
       
   671                          "--module-path", classes.toString(),
       
   672                          "-Xlint:deprecation")
       
   673                 .outdir(classes)
       
   674                 .files(findJavaFiles(src_m4))
       
   675                 .run(Task.Expect.FAIL)
       
   676                 .writeAll()
       
   677                 .getOutputLines(Task.OutputKind.DIRECT);
       
   678 
       
   679         List<String> expected = Arrays.asList(
       
   680                 "Test.java:1:40: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read: m4x, api, m3x)",
       
   681                 "1 error");
       
   682 
       
   683         if (!expected.equals(log))
       
   684             throw new Exception("expected output not found; actual: " + log);
       
   685     }
       
   686 
       
   687     @Test
       
   688     public void testConvertNameCandidates(Path base) throws Exception {
       
   689         Context ctx = new Context();
       
   690         Names names = Names.instance(ctx);
       
   691         Name name = names.fromString("com.sun.tools.javac.Attr.BreakAttr");
       
   692 
       
   693         com.sun.tools.javac.util.List<String> actual =
       
   694                 Convert.classCandidates(name).map(n -> n.toString());
       
   695         List<String> expected = Arrays.asList(
       
   696                 "com.sun$tools$javac$Attr$BreakAttr",
       
   697                 "com.sun.tools$javac$Attr$BreakAttr",
       
   698                 "com.sun.tools.javac$Attr$BreakAttr",
       
   699                 "com.sun.tools.javac.Attr$BreakAttr",
       
   700                 "com.sun.tools.javac.Attr.BreakAttr"
       
   701         );
       
   702 
       
   703         if (!expected.equals(actual)) {
       
   704             throw new Exception("Expected names not generated: " + actual);
       
   705         }
       
   706     }
   596 }
   707 }