1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8072480 27 * @summary Ensure all methods of PlatformProvider are called correctly, and their result is used 28 * correctly. 29 * @library /tools/lib 30 * @build ToolBox PlatformProviderTest 31 * @run main PlatformProviderTest 32 */ 33 34 import java.io.IOException; 35 import java.io.Writer; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.ArrayList; 40 import java.util.Arrays; 41 import java.util.Collection; 42 import java.util.Collections; 43 import java.util.List; 44 import java.util.Map; 45 import java.util.Set; 46 47 import javax.annotation.processing.AbstractProcessor; 48 import javax.annotation.processing.Processor; 49 import javax.annotation.processing.RoundEnvironment; 50 import javax.annotation.processing.SupportedAnnotationTypes; 51 import javax.annotation.processing.SupportedOptions; 52 import javax.lang.model.SourceVersion; 53 import javax.lang.model.element.TypeElement; 54 import javax.tools.JavaCompiler; 55 import javax.tools.StandardJavaFileManager; 56 import javax.tools.StandardLocation; 57 import javax.tools.ToolProvider; 58 59 import com.sun.source.util.JavacTask; 60 import com.sun.source.util.Plugin; 61 import com.sun.tools.javac.platform.PlatformProvider; 62 import com.sun.tools.javac.platform.PlatformProviderFactory; 63 64 public class PlatformProviderTest implements PlatformProviderFactory { 65 66 public static void main(String... args) throws IOException { 67 Path registration = Paths.get(System.getProperty("test.classes"), 68 "META-INF", 69 "services", 70 "com.sun.tools.javac.platform.PlatformProviderFactory"); 71 Files.createDirectories(registration.getParent()); 72 try (Writer metaInf = Files.newBufferedWriter(registration)) { 73 metaInf.write(PlatformProviderTest.class.getName()); 74 } 75 ToolBox tb = new ToolBox(); 76 ToolBox.Result result = 77 tb.new JavacTask(ToolBox.Mode.EXEC) 78 .options("-J-classpath", 79 "-J" + System.getProperty("test.classes"), 80 "-release", 81 "name", 82 System.getProperty("test.src") + "/PlatformProviderTestSource.java") 83 .run(); 84 85 List<String> expectedOutput = 86 Arrays.asList("getName", 87 "getSourceVersion", 88 "getTargetVersion", 89 "getPlatformPath", 90 "testPlugin: [testPluginKey=testPluginValue]", 91 "process: {testAPKey=testAPValue}", 92 "process: {testAPKey=testAPValue}", 93 "PlatformProviderTestSource.java:4:49: compiler.warn.raw.class.use: java.util.ArrayList, java.util.ArrayList<E>", 94 "1 warning", 95 "close"); 96 List<String> actualOutput = result.getOutputLines(ToolBox.OutputKind.STDERR); 97 result.writeAll(); 98 if (!expectedOutput.equals(actualOutput)) { 99 throw new AssertionError( "Expected output: " + expectedOutput + 100 "; actual output: " + actualOutput); 101 } 102 result.writeAll(); 103 } 104 105 @Override 106 public Iterable<PlatformProvider> createPlatformProviders() { 107 return Arrays.asList(new ProviderImpl()); 108 } 109 110 class ProviderImpl implements PlatformProvider { 111 112 private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 113 private final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); 114 115 @Override 116 public String getName() { 117 System.err.println("getName"); 118 return "name"; 119 } 120 121 @Override 122 public Collection<Path> getPlatformPath() { 123 System.err.println("getPlatformPath"); 124 List<Path> result = new ArrayList<>(); 125 fm.getLocationAsPaths(StandardLocation.PLATFORM_CLASS_PATH).forEach(p -> { result.add(p); }); 126 return result; 127 } 128 129 @Override 130 public String getSourceVersion() { 131 System.err.println("getSourceVersion"); 132 return "8"; 133 } 134 135 @Override 136 public String getTargetVersion() { 137 System.err.println("getTargetVersion"); 138 return "8"; 139 } 140 141 @Override 142 public List<PluginInfo<Processor>> getAnnotationProcessors() { 143 return Arrays.asList(new PluginInfo<Processor>() { 144 @Override 145 public String getName() { 146 return "test"; 147 } 148 @Override 149 public Map<String, String> getOptions() { 150 return Collections.singletonMap("testAPKey", "testAPValue"); 151 } 152 @Override 153 public Processor getPlugin() { 154 return new ProcessorImpl(); 155 } 156 }); 157 } 158 159 @Override 160 public List<PluginInfo<Plugin>> getPlugins() { 161 return Arrays.asList(new PluginInfo<Plugin>() { 162 @Override 163 public String getName() { 164 return "testPlugin"; 165 } 166 @Override 167 public Map<String, String> getOptions() { 168 return Collections.singletonMap("testPluginKey", "testPluginValue"); 169 } 170 @Override 171 public Plugin getPlugin() { 172 return new PluginImpl(); 173 } 174 }); 175 } 176 177 @Override 178 public List<String> getAdditionalOptions() { 179 return Arrays.asList("-Xlint:rawtypes", "-XDrawDiagnostics"); 180 } 181 182 @Override 183 public void close() throws IOException { 184 System.err.println("close"); 185 fm.close(); 186 } 187 188 } 189 190 @SupportedAnnotationTypes("*") 191 @SupportedOptions("testAPKey") 192 class ProcessorImpl extends AbstractProcessor { 193 194 @Override 195 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 196 System.err.println("process: " + processingEnv.getOptions()); 197 return true; 198 } 199 200 @Override 201 public SourceVersion getSupportedSourceVersion() { 202 return SourceVersion.latest(); 203 } 204 205 } 206 207 class PluginImpl implements Plugin { 208 209 @Override 210 public String getName() { 211 return "testPluginName"; 212 } 213 214 @Override 215 public void init(JavacTask task, String... args) { 216 System.err.println("testPlugin: " + Arrays.toString(args)); 217 } 218 219 } 220 }