test/tools/javac/processing/filer/TestFilerConstraints.java

Print this page


   1 /*
   2  * Copyright (c) 2006, 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 6380018 6453386 6457283
  27  * @summary Test that the constraints guaranteed by the Filer and maintained
  28  * @author  Joseph D. Darcy

  29  * @build TestFilerConstraints
  30  * @compile -encoding iso-8859-1 -processor TestFilerConstraints -proc:only TestFilerConstraints.java
  31  */
  32 
  33 import java.util.Set;
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.SourceVersion;
  36 import static javax.lang.model.SourceVersion.*;
  37 import javax.lang.model.element.*;
  38 import javax.lang.model.util.*;
  39 import static javax.lang.model.util.ElementFilter.*;
  40 import static javax.tools.Diagnostic.Kind.*;
  41 import static javax.tools.StandardLocation.*;
  42 
  43 import java.io.*;
  44 import java.nio.charset.Charset;
  45 
  46 /**
  47  * A processor that verifies the explicit and implicit constraints in
  48  * the Filer contract are maintained:


  52  *  During each run of an annotation processing tool, a file with a
  53  *  given pathname may be created only once. If that file already
  54  *  exists before the first attempt to create it, the old contents
  55  *  will be deleted. Any subsequent attempt to create the same file
  56  *  during a run will throw a FilerException, as will attempting to
  57  *  open both a class file and source file for the same type name.
  58  *
  59  * </blockquote>
  60  *
  61  * Specific checks will include:
  62  *
  63  * <ul>
  64  *
  65  * <li> Source and class files can be written to from either a Writer or an OutputStream.
  66  *
  67  * <li> Calling close multiple times does not re-register the file for
  68  * processing.
  69  *
  70  * </ul>
  71  */
  72 @SupportedAnnotationTypes("*")
  73 public class TestFilerConstraints extends AbstractProcessor {
  74     private int round = 0;
  75     private Messager messager;
  76     private Filer filer;
  77 
  78     private PrintWriter  pw_src1 = null;
  79     private PrintWriter  pw_src2 = null;
  80     private OutputStream os_classFile1 = null;
  81     private      Writer  pw_classFile2 = null;
  82 
  83     public boolean process(Set<? extends TypeElement> annotations,
  84                            RoundEnvironment roundEnv) {
  85         round++;
  86 
  87         try {
  88             switch(round) {
  89                 // Open two source files
  90             case 1:
  91                 pw_src1 = new PrintWriter(filer.createSourceFile("Src1").openWriter());
  92                 pw_src1.println("class Src1 {}");
  93                 pw_src1.close();
  94 
  95                 // Hold open across rounds
  96                 pw_src2 = new PrintWriter(new OutputStreamWriter(filer.createSourceFile("Src2").openOutputStream()));


 150                 pw_classFile2.close();
 151 
 152 
 153                 break;
 154 
 155             case 6:
 156                 if (!roundEnv.processingOver() && !roundEnv.errorRaised())
 157                     throw new RuntimeException("Bad round state: " + roundEnv);
 158                 break;
 159 
 160             default:
 161                 throw new RuntimeException("Unexpected round number!");
 162             }
 163         } catch (IOException ioe) {
 164             throw new RuntimeException(ioe);
 165         }
 166 
 167         return true;
 168     }
 169 
 170     public SourceVersion getSupportedSourceVersion() {
 171         return SourceVersion.latest();
 172     }
 173 
 174     public void init(ProcessingEnvironment processingEnv) {
 175         super.init(processingEnv);
 176         messager = processingEnv.getMessager();
 177         filer    = processingEnv.getFiler();
 178 
 179     }
 180 
 181     /**
 182      * Test that the single expected expected type, name, is the root
 183      * element.
 184      */
 185     private void testExpectedType(RoundEnvironment roundEnv, String name) {
 186         if (!roundEnv.getRootElements().isEmpty()) {
 187             for(TypeElement type : typesIn(roundEnv.getRootElements())) {
 188                 if (!name.contentEquals(type.getSimpleName()))
 189                     throw new RuntimeException("Unexpected type " +  type.getSimpleName());
 190             }
 191         } else
 192             throw new RuntimeException("Unexpected empty root elements.");
 193     }
 194 
 195     private void testReopening() throws IOException {
 196         String[] names = {"Src1", "Src2", "ClassFile1"};
 197         for (String name : names) {
 198             try {
 199                 filer.createSourceFile(name);
 200                 throw new RuntimeException("Opened a source file for type " + name);


   1 /*
   2  * Copyright (c) 2006, 2010, 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 6380018 6453386 6457283
  27  * @summary Test that the constraints guaranteed by the Filer and maintained
  28  * @author  Joseph D. Darcy
  29  * @library ../../lib
  30  * @build TestFilerConstraints
  31  * @compile -encoding iso-8859-1 -processor TestFilerConstraints -proc:only TestFilerConstraints.java
  32  */
  33 
  34 import java.util.Set;
  35 import javax.annotation.processing.*;
  36 import javax.lang.model.SourceVersion;
  37 import static javax.lang.model.SourceVersion.*;
  38 import javax.lang.model.element.*;
  39 import javax.lang.model.util.*;
  40 import static javax.lang.model.util.ElementFilter.*;
  41 import static javax.tools.Diagnostic.Kind.*;
  42 import static javax.tools.StandardLocation.*;
  43 
  44 import java.io.*;
  45 import java.nio.charset.Charset;
  46 
  47 /**
  48  * A processor that verifies the explicit and implicit constraints in
  49  * the Filer contract are maintained:


  53  *  During each run of an annotation processing tool, a file with a
  54  *  given pathname may be created only once. If that file already
  55  *  exists before the first attempt to create it, the old contents
  56  *  will be deleted. Any subsequent attempt to create the same file
  57  *  during a run will throw a FilerException, as will attempting to
  58  *  open both a class file and source file for the same type name.
  59  *
  60  * </blockquote>
  61  *
  62  * Specific checks will include:
  63  *
  64  * <ul>
  65  *
  66  * <li> Source and class files can be written to from either a Writer or an OutputStream.
  67  *
  68  * <li> Calling close multiple times does not re-register the file for
  69  * processing.
  70  *
  71  * </ul>
  72  */
  73 public class TestFilerConstraints extends JavacTestingAbstractProcessor {

  74     private int round = 0;


  75 
  76     private PrintWriter  pw_src1 = null;
  77     private PrintWriter  pw_src2 = null;
  78     private OutputStream os_classFile1 = null;
  79     private      Writer  pw_classFile2 = null;
  80 
  81     public boolean process(Set<? extends TypeElement> annotations,
  82                            RoundEnvironment roundEnv) {
  83         round++;
  84 
  85         try {
  86             switch(round) {
  87                 // Open two source files
  88             case 1:
  89                 pw_src1 = new PrintWriter(filer.createSourceFile("Src1").openWriter());
  90                 pw_src1.println("class Src1 {}");
  91                 pw_src1.close();
  92 
  93                 // Hold open across rounds
  94                 pw_src2 = new PrintWriter(new OutputStreamWriter(filer.createSourceFile("Src2").openOutputStream()));


 148                 pw_classFile2.close();
 149 
 150 
 151                 break;
 152 
 153             case 6:
 154                 if (!roundEnv.processingOver() && !roundEnv.errorRaised())
 155                     throw new RuntimeException("Bad round state: " + roundEnv);
 156                 break;
 157 
 158             default:
 159                 throw new RuntimeException("Unexpected round number!");
 160             }
 161         } catch (IOException ioe) {
 162             throw new RuntimeException(ioe);
 163         }
 164 
 165         return true;
 166     }
 167 











 168     /**
 169      * Test that the single expected expected type, name, is the root
 170      * element.
 171      */
 172     private void testExpectedType(RoundEnvironment roundEnv, String name) {
 173         if (!roundEnv.getRootElements().isEmpty()) {
 174             for(TypeElement type : typesIn(roundEnv.getRootElements())) {
 175                 if (!name.contentEquals(type.getSimpleName()))
 176                     throw new RuntimeException("Unexpected type " +  type.getSimpleName());
 177             }
 178         } else
 179             throw new RuntimeException("Unexpected empty root elements.");
 180     }
 181 
 182     private void testReopening() throws IOException {
 183         String[] names = {"Src1", "Src2", "ClassFile1"};
 184         for (String name : names) {
 185             try {
 186                 filer.createSourceFile(name);
 187                 throw new RuntimeException("Opened a source file for type " + name);