Tue, 02 Feb 2010 10:56:10 -0800
6918625: handle annotations on array class literals
Reviewed-by: jjg, darcy
Contributed-by: mali@csail.mit.edu, mernst@cs.washington.edu
jjg@439 | 1 | /* |
jjg@439 | 2 | * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
jjg@439 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@439 | 4 | * |
jjg@439 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@439 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@439 | 7 | * published by the Free Software Foundation. |
jjg@439 | 8 | * |
jjg@439 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@439 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@439 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@439 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@439 | 13 | * accompanied this code). |
jjg@439 | 14 | * |
jjg@439 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@439 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@439 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@439 | 18 | * |
jjg@439 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@439 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@439 | 21 | * have any questions. |
jjg@439 | 22 | */ |
jjg@439 | 23 | |
jjg@439 | 24 | import java.io.*; |
jjg@439 | 25 | import java.net.*; |
jjg@439 | 26 | import javax.tools.*; |
jjg@439 | 27 | import java.util.*; |
jjg@439 | 28 | |
jjg@439 | 29 | import com.sun.source.tree.CompilationUnitTree; |
jjg@439 | 30 | import com.sun.source.util.JavacTask; |
jjg@439 | 31 | import com.sun.tools.javac.api.JavacTool; |
jjg@439 | 32 | import com.sun.tools.javac.tree.JCTree; |
jjg@439 | 33 | import com.sun.tools.javac.tree.Pretty; |
jjg@439 | 34 | |
jjg@439 | 35 | /** |
jjg@439 | 36 | * @test |
jjg@439 | 37 | * @bug 6902720 |
jjg@439 | 38 | * @summary javac pretty printer does not handle enums correctly |
jjg@439 | 39 | */ |
jjg@439 | 40 | |
jjg@439 | 41 | public class Test { |
jjg@439 | 42 | |
jjg@439 | 43 | public static void main(String[] args) throws Exception { |
jjg@439 | 44 | Test t = new Test(); |
jjg@439 | 45 | t.run("E1.java", "E2.java"); |
jjg@439 | 46 | } |
jjg@439 | 47 | |
jjg@439 | 48 | void run(String... args) throws Exception { |
jjg@439 | 49 | File testSrcDir = new File(System.getProperty("test.src")); |
jjg@439 | 50 | for (String arg: args) { |
jjg@439 | 51 | test(new File(testSrcDir, arg)); |
jjg@439 | 52 | } |
jjg@439 | 53 | } |
jjg@439 | 54 | |
jjg@439 | 55 | void test(File test) throws Exception { |
jjg@439 | 56 | JavacTool tool1 = JavacTool.create(); |
jjg@439 | 57 | StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null); |
jjg@439 | 58 | Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test); |
jjg@439 | 59 | |
jjg@439 | 60 | // parse test file into a tree, and write it out to a stringbuffer using Pretty |
jjg@439 | 61 | JavacTask t1 = tool1.getTask(null, fm, null, null, null, files); |
jjg@439 | 62 | StringWriter sw = new StringWriter(); |
jjg@439 | 63 | PrintWriter pw = new PrintWriter(sw); |
jjg@439 | 64 | Iterable<? extends CompilationUnitTree> trees = t1.parse(); |
jjg@439 | 65 | for (CompilationUnitTree tree: trees) { |
jjg@439 | 66 | new Pretty(pw, true).printExpr((JCTree) tree); |
jjg@439 | 67 | } |
jjg@439 | 68 | pw.close(); |
jjg@439 | 69 | |
jjg@439 | 70 | final String out = sw.toString(); |
jjg@439 | 71 | System.err.println("generated code:\n" + out + "\n"); |
jjg@439 | 72 | |
jjg@439 | 73 | // verify the generated code is valid Java by compiling it |
jjg@439 | 74 | JavacTool tool2 = JavacTool.create(); |
jjg@439 | 75 | JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) { |
jjg@439 | 76 | @Override |
jjg@439 | 77 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
jjg@439 | 78 | return out; |
jjg@439 | 79 | } |
jjg@439 | 80 | }; |
jjg@439 | 81 | JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo)); |
jjg@439 | 82 | boolean ok = t2.call(); |
jjg@439 | 83 | if (!ok) |
jjg@439 | 84 | throw new Exception("compilation of generated code failed"); |
jjg@439 | 85 | |
jjg@439 | 86 | File expectedClass = new File(test.getName().replace(".java", ".class")); |
jjg@439 | 87 | if (!expectedClass.exists()) |
jjg@439 | 88 | throw new Exception(expectedClass + " not found"); |
jjg@439 | 89 | } |
jjg@439 | 90 | } |
jjg@439 | 91 |