test/tools/javac/T6358168.java

Mon, 16 Jun 2008 13:28:00 -0700

author
jjg
date
Mon, 16 Jun 2008 13:28:00 -0700
changeset 50
b9bcea8bbe24
parent 1
9a66ca7c79fa
child 54
eaf608c64fec
child 58
8bc2ca2a3b0a
permissions
-rw-r--r--

6714364: refactor javac File handling code into new javac.file package
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 6358168
    27  * @summary JavaCompiler.hasBeenUsed is not set in delegateCompiler
    28  */
    30 import java.io.*;
    31 import java.net.*;
    32 import java.util.*;
    33 import javax.annotation.processing.*;
    34 import javax.lang.model.element.*;
    35 import javax.tools.*;
    36 import com.sun.tools.javac.file.*;
    37 import com.sun.tools.javac.main.JavaCompiler;
    38 import com.sun.tools.javac.main.*;
    39 import com.sun.tools.javac.util.*;
    40 import com.sun.tools.javac.util.List; // disambiguate
    43 @SupportedAnnotationTypes("*")
    44 public class T6358168 extends AbstractProcessor {
    45     private static String testClasses = System.getProperty("test.classes");
    46     private static String testSrc = System.getProperty("test.src");
    47     private static String self = T6358168.class.getName();
    49     public static void main(String... args) throws Throwable {
    51         JavacFileManager fm = new JavacFileManager(new Context(), false, null);
    52         JavaFileObject f = fm.getFileForInput(testSrc + File.separatorChar + T6358168.class.getName() + ".java");
    54         try {
    55             // first, test case with no annotation processing
    56             testNoAnnotationProcessing(fm, f);
    58             // now, test case with annotation processing
    59             testAnnotationProcessing(fm, f);
    60         }
    61         catch (Throwable t) {
    62             AssertionError e = new AssertionError();
    63             e.initCause(t);
    64             throw e;
    65         }
    66     }
    68     static void testNoAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
    69         Context context = new Context();
    70         fm.setContext(context);
    72         Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
    73         compilerMain.setOptions(Options.instance(context));
    74         compilerMain.filenames = new ListBuffer<File>();
    75         compilerMain.processArgs(new String[] { "-d", "." });
    77         JavaCompiler compiler = JavaCompiler.instance(context);
    78         compiler.compile(List.of(f));
    79         try {
    80             compiler.compile(List.of(f));
    81             throw new Error("Error: AssertionError not thrown after second call of compile");
    82         } catch (AssertionError e) {
    83             System.err.println("Exception from compiler (expected): " + e);
    84         }
    85     }
    87     static void testAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
    88         Context context = new Context();
    89         fm.setContext(context);
    91         Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
    92         compilerMain.setOptions(Options.instance(context));
    93         compilerMain.filenames = new ListBuffer<File>();
    94         compilerMain.processArgs(new String[] {
    95                                      "-XprintRounds",
    96                                      "-processorpath", testClasses,
    97                                      "-processor", self,
    98                                      "-d", "."});
   100         JavaCompiler compiler = JavaCompiler.instance(context);
   101         compiler.initProcessAnnotations(null);
   102         JavaCompiler compiler2 = compiler.processAnnotations(compiler.enterTrees(compiler.parseFiles(List.of(f))));
   103         try {
   104             compiler2.compile(List.of(f));
   105             throw new Error("Error: AssertionError not thrown after second call of compile");
   106         } catch (AssertionError e) {
   107             System.err.println("Exception from compiler (expected): " + e);
   108         }
   109     }
   111     public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
   112         return true;
   113     }
   114 }

mercurial