test/tools/javac/T6403466.java

Mon, 15 Feb 2010 18:20:57 -0800

author
darcy
date
Mon, 15 Feb 2010 18:20:57 -0800
changeset 494
af18e3956985
parent 1
9a66ca7c79fa
child 554
9d9f26857129
permissions
-rw-r--r--

6634138: Source generated in last round not compiled
Reviewed-by: jjg

     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 6403466
    27  * @summary javac TaskListener should be informed when annotation processing occurs
    28  */
    30 import com.sun.source.util.*;
    31 import java.io.*;
    32 import java.lang.annotation.*;
    33 import java.util.*;
    34 import javax.annotation.processing.*;
    35 import javax.lang.model.*;
    36 import javax.lang.model.element.*;
    37 import javax.lang.model.type.*;
    38 import javax.lang.model.util.*;
    39 import javax.tools.*;
    40 import com.sun.tools.javac.api.JavacTool;
    42 @Wrap
    43 @SupportedAnnotationTypes("Wrap")
    44 public class T6403466 extends AbstractProcessor {
    46     static final String testSrcDir = System.getProperty("test.src");
    47     static final String testClassDir = System.getProperty("test.classes");
    48     static final String self = T6403466.class.getName();
    49     static PrintWriter out = new PrintWriter(System.err, true);
    51     public static void main(String[] args) throws IOException {
    52         JavacTool tool = JavacTool.create();
    54         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    55         Iterable<? extends JavaFileObject> files =
    56             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
    58         Iterable<String> options = Arrays.asList("-processorpath", testClassDir,
    59                                                  "-processor", self,
    60                                                  "-s", ".",
    61                                                  "-d", ".");
    62         JavacTask task = tool.getTask(out, fm, null, options, null, files);
    64         VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
    65         task.setTaskListener(vtl);
    67         if (!task.call())
    68             throw new AssertionError("compilation failed");
    70         if (vtl.iter.hasNext() || vtl.errors)
    71             throw new AssertionError("comparison against golden file failed.");
    72     }
    74     public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
    75         if (!rEnv.processingOver()) {
    76             Filer filer = processingEnv.getFiler();
    77             for (TypeElement anno: annos) {
    78                 Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno);
    79                 System.err.println("anno: " + anno);
    80                 System.err.println("elts: " + elts);
    81                 for (TypeElement te: ElementFilter.typesIn(elts)) {
    82                     try {
    83                         Writer out = filer.createSourceFile(te.getSimpleName() + "Wrapper").openWriter();
    84                         out.write("class " + te.getSimpleName() + "Wrapper { }");
    85                         out.close();
    86                     } catch (IOException ex) {
    87                         ex.printStackTrace();
    88                     }
    89                 }
    91             }
    92         }
    93         return true;
    94     }
    96     @Override
    97     public SourceVersion getSupportedSourceVersion() {
    98         return SourceVersion.latest();
    99     }
   100 }
   102 @Retention(RetentionPolicy.SOURCE)
   103 @Target(ElementType.TYPE)
   104 @interface Wrap {
   105 }
   108 class VerifyingTaskListener implements TaskListener {
   109     VerifyingTaskListener(File ref) throws IOException {
   110         BufferedReader in = new BufferedReader(new FileReader(ref));
   111         String line;
   112         List<String> lines = new ArrayList<String>();
   113         while ((line = in.readLine()) != null)
   114             lines.add(line);
   115         in.close();
   116         iter = lines.iterator();
   117     }
   119     public void started(TaskEvent e) {
   120         check("Started " + toString(e));
   121     }
   122     public void finished(TaskEvent e) {
   123         check("Finished " + toString(e));
   124     }
   126     // similar to TaskEvent.toString(), but just prints basename of the file
   127     private String toString(TaskEvent e) {
   128         JavaFileObject file = e.getSourceFile();
   129         return "TaskEvent["
   130             + e.getKind() + ","
   131             + (file == null ? null : new File(file.toUri().getPath()).getName()) + ","
   132             // the compilation unit is identified by the file
   133             + e.getTypeElement() + "]";
   134     }
   136     private void check(String s) {
   137         System.out.println(s); // write a reference copy of expected output to stdout
   139         String ref = iter.hasNext() ? iter.next() : null;
   140         line++;
   141         if (!s.equals(ref)) {
   142             if (ref != null)
   143                 System.err.println(line + ": expected: " + ref);
   144             System.err.println(line + ":    found: " + s);
   145             errors = true;
   146         }
   147     }
   149     Iterator<String> iter;
   150     int line;
   151     boolean errors;
   152 }

mercurial