1/* 2 * Copyright (c) 2005, 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 4813736 8015073 27 * @summary Provide a basic test of access to the Java Model from javac, and error messages 28 * @author Peter von der Ah\u00e9 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * @run main TestJavacTask TestJavacTask.java 31 */ 32 33import com.sun.tools.javac.api.JavacTaskImpl; 34 35import java.io.File; 36import java.io.IOException; 37import java.util.Arrays; 38import javax.lang.model.element.Element; 39import javax.tools.JavaCompiler; 40import javax.tools.JavaFileObject; 41import javax.tools.StandardJavaFileManager; 42import javax.tools.ToolProvider; 43 44public class TestJavacTask { 45 static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 46 static final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); 47 static JavacTaskImpl getTask(File... file) { 48 Iterable<? extends JavaFileObject> files = 49 fm.getJavaFileObjectsFromFiles(Arrays.asList(file)); 50 return (JavacTaskImpl)compiler.getTask(null, fm, null, null, null, files); 51 } 52 53 static void basicTest(String... args) throws IOException { 54 String srcdir = System.getProperty("test.src"); 55 File file = new File(srcdir, args[0]); 56 JavacTaskImpl task = getTask(file); 57 for (Element clazz : task.enter(task.parse())) 58 System.out.println(clazz.getSimpleName()); 59 } 60 61 static void checkKindError() { 62 final File testFile = new File("Test.java "); // <-note trailing space! 63 try { 64 getTask(testFile); 65 } catch (IllegalArgumentException iae) { 66 // The following check is somewhat fragile, since the content of the ILA is not 67 // formally specified. If we want to fix this, we should catch/rewrap ILA coming 68 // from use of java.nio.file.Path inside javac's impl of JavaFileManager. 69 if (!iae.getMessage().contains(testFile.getName())) { 70 System.err.println("Got message: " + iae.getMessage()); 71 throw new RuntimeException("Error: expected string not found"); 72 } 73 } 74 } 75 76 public static void main(String... args) throws IOException { 77 try { 78 basicTest(args); 79 checkKindError(); 80 } finally { 81 fm.close(); 82 } 83 } 84} 85