langtools/test/tools/javac/plugin/showtype/Test.java
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
equal deleted inserted replaced
36777:28d33fb9097f 36778:e04318f39f92
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2016, 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.
    24 /**
    24 /**
    25  *  @test
    25  *  @test
    26  *  @bug 8001098 8004961 8004082
    26  *  @bug 8001098 8004961 8004082
    27  *  @library /tools/lib
    27  *  @library /tools/lib
    28  *  @modules jdk.compiler/com.sun.tools.javac.api
    28  *  @modules jdk.compiler/com.sun.tools.javac.api
    29  *           jdk.compiler/com.sun.tools.javac.file
       
    30  *           jdk.compiler/com.sun.tools.javac.main
    29  *           jdk.compiler/com.sun.tools.javac.main
    31  *          jdk.jdeps/com.sun.tools.javap
    30  *          jdk.jdeps/com.sun.tools.javap
    32  *  @build ToolBox
    31  *  @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask
    33  *  @run main Test
    32  *  @run main Test
    34  *  @summary Provide a simple light-weight "plug-in" mechanism for javac
    33  *  @summary Provide a simple light-weight "plug-in" mechanism for javac
    35  */
    34  */
    36 
    35 
    37 import java.io.File;
    36 import java.io.File;
    42 import javax.tools.JavaFileObject;
    41 import javax.tools.JavaFileObject;
    43 import javax.tools.StandardJavaFileManager;
    42 import javax.tools.StandardJavaFileManager;
    44 import javax.tools.StandardLocation;
    43 import javax.tools.StandardLocation;
    45 import javax.tools.ToolProvider;
    44 import javax.tools.ToolProvider;
    46 
    45 
    47 import com.sun.source.util.JavacTask;
    46 import toolbox.JarTask;
       
    47 import toolbox.JavacTask;
       
    48 import toolbox.Task;
       
    49 import toolbox.ToolBox;
    48 
    50 
    49 public class Test {
    51 public class Test {
    50     public static void main(String... args) throws Exception {
    52     public static void main(String... args) throws Exception {
    51         new Test().run();
    53         new Test().run();
    52     }
    54     }
    75 
    77 
    76     void run() throws Exception {
    78     void run() throws Exception {
    77         try {
    79         try {
    78             // compile the plugin explicitly, to a non-standard directory
    80             // compile the plugin explicitly, to a non-standard directory
    79             // so that we don't find it on the wrong path by accident
    81             // so that we don't find it on the wrong path by accident
    80             tb.new JavacTask()
    82             new JavacTask(tb)
    81               .options("-d", pluginClasses.getPath())
    83               .options("-d", pluginClasses.getPath())
    82               .files(pluginSrc.getPath())
    84               .files(pluginSrc.getPath())
    83               .run();
    85               .run();
    84 
    86 
    85             File plugin = new File(pluginClasses.getPath(), "META-INF/services/com.sun.source.util.Plugin");
    87             File plugin = new File(pluginClasses.getPath(), "META-INF/services/com.sun.source.util.Plugin");
    86             tb.writeFile(plugin.getPath(), "ShowTypePlugin\n");
    88             tb.writeFile(plugin.getPath(), "ShowTypePlugin\n");
    87             tb.new JarTask()
    89             new JarTask(tb)
    88               .run("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
    90               .run("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
    89 
    91 
    90             testCommandLine("-Xplugin:showtype", ref1);
    92             testCommandLine("-Xplugin:showtype", ref1);
    91             testCommandLine("-Xplugin:showtype PI", ref2);
    93             testCommandLine("-Xplugin:showtype PI", ref2);
    92             testAPI("-Xplugin:showtype", ref1);
    94             testAPI("-Xplugin:showtype", ref1);
   105         fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
   107         fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
   106         List<String> options = Arrays.asList(opt);
   108         List<String> options = Arrays.asList(opt);
   107         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
   109         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
   108 
   110 
   109         System.err.println("test api: " + options + " " + files);
   111         System.err.println("test api: " + options + " " + files);
   110         ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.API)
   112         Task.Result result = new JavacTask(tb, Task.Mode.API)
   111                                   .fileManager(fm)
   113                                   .fileManager(fm)
   112                                   .options(opt)
   114                                   .options(opt)
   113                                   .files(identifiers.toPath())
   115                                   .files(identifiers.toPath())
   114                                   .run(ToolBox.Expect.SUCCESS)
   116                                   .run(Task.Expect.SUCCESS)
   115                                   .writeAll();
   117                                   .writeAll();
   116         String out = result.getOutput(ToolBox.OutputKind.DIRECT);
   118         String out = result.getOutput(Task.OutputKind.DIRECT);
   117         checkOutput(out, ref);
   119         checkOutput(out, ref);
   118     }
   120     }
   119 
   121 
   120     void testCommandLine(String opt, List<String> ref) {
   122     void testCommandLine(String opt, List<String> ref) {
   121         File identifiers = new File(testSrc, "Identifiers.java");
   123         File identifiers = new File(testSrc, "Identifiers.java");
   124             "-processorpath", pluginJar.getPath(),
   126             "-processorpath", pluginJar.getPath(),
   125             opt,
   127             opt,
   126             identifiers.getPath() };
   128             identifiers.getPath() };
   127 
   129 
   128         System.err.println("test command line: " + Arrays.asList(args));
   130         System.err.println("test command line: " + Arrays.asList(args));
   129         ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.CMDLINE)
   131         Task.Result result = new JavacTask(tb, Task.Mode.CMDLINE)
   130                                   .options(args)
   132                                   .options(args)
   131                                   .run(ToolBox.Expect.SUCCESS)
   133                                   .run(Task.Expect.SUCCESS)
   132                                   .writeAll();
   134                                   .writeAll();
   133         String out = result.getOutput(ToolBox.OutputKind.DIRECT);
   135         String out = result.getOutput(Task.OutputKind.DIRECT);
   134         checkOutput(out, ref);
   136         checkOutput(out, ref);
   135     }
   137     }
   136 
   138 
   137     private void checkOutput(String out, List<String> ref) {
   139     private void checkOutput(String out, List<String> ref) {
   138         List<String> lines = Arrays.asList(out
   140         List<String> lines = Arrays.asList(out