test/tools/javac/api/TestJavacTask.java

changeset 2075
82044fe8c7f7
parent 554
9d9f26857129
child 2525
2eb010b6cb22
equal deleted inserted replaced
2074:dee28dd47e12 2075:82044fe8c7f7
1 /* 1 /*
2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2005, 2013, 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 4813736 26 * @bug 4813736 8015073
27 * @summary Provide a basic test of access to the Java Model from javac 27 * @summary Provide a basic test of access to the Java Model from javac, and error messages
28 * @author Peter von der Ah\u00e9 28 * @author Peter von der Ah\u00e9
29 * @run main TestJavacTask TestJavacTask.java 29 * @run main TestJavacTask TestJavacTask.java
30 */ 30 */
31 31
32 import com.sun.tools.javac.api.JavacTaskImpl; 32 import com.sun.tools.javac.api.JavacTaskImpl;
38 import javax.tools.JavaFileObject; 38 import javax.tools.JavaFileObject;
39 import javax.tools.StandardJavaFileManager; 39 import javax.tools.StandardJavaFileManager;
40 import javax.tools.ToolProvider; 40 import javax.tools.ToolProvider;
41 41
42 public class TestJavacTask { 42 public class TestJavacTask {
43 43 static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
44 static JavacTaskImpl getTask(JavaCompiler compiler, File... file) { 44 static JavacTaskImpl getTask(File... file) {
45 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); 45 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
46 Iterable<? extends JavaFileObject> files = 46 Iterable<? extends JavaFileObject> files =
47 fm.getJavaFileObjectsFromFiles(Arrays.asList(file)); 47 fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
48 return (JavacTaskImpl)compiler.getTask(null, fm, null, null, null, files); 48 return (JavacTaskImpl)compiler.getTask(null, fm, null, null, null, files);
49 } 49 }
50 50
51 public static void main(String... args) throws IOException { 51 static void basicTest(String... args) throws IOException {
52 JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
53 String srcdir = System.getProperty("test.src"); 52 String srcdir = System.getProperty("test.src");
54 File file = new File(srcdir, args[0]); 53 File file = new File(srcdir, args[0]);
55 JavacTaskImpl task = getTask(tool, file); 54 JavacTaskImpl task = getTask(file);
56 for (TypeElement clazz : task.enter(task.parse())) 55 for (TypeElement clazz : task.enter(task.parse()))
57 System.out.println(clazz.getSimpleName()); 56 System.out.println(clazz.getSimpleName());
58 } 57 }
59 58
59 static void checkKindError() {
60 final File testFile = new File("Test.java "); // <-note trailing space!
61 try {
62 getTask(testFile);
63 } catch (IllegalArgumentException iae) {
64 if (!iae.getMessage().contains("\"" + testFile.getName() + "\"")) {
65 System.err.println("Got message: " + iae.getMessage());
66 throw new RuntimeException("Error: expected string not found");
67 }
68 }
69 }
70
71 public static void main(String... args) throws IOException {
72 basicTest(args);
73 checkKindError();
74 }
60 } 75 }

mercurial