test/tools/javac/processing/filer/TestGetResource.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 6449798
  27  * @summary Test Filer.getResource
  28  * @author  Joseph D. Darcy


  29  * @build TestGetResource
  30  * @compile -processor TestGetResource -proc:only -Aphase=write TestGetResource.java
  31  * @compile -processor TestGetResource -proc:only -Aphase=read  TestGetResource.java
  32  */
  33 
  34 import java.util.Set;
  35 import java.util.Map;
  36 import javax.annotation.processing.*;
  37 import javax.lang.model.SourceVersion;
  38 import static javax.lang.model.SourceVersion.*;
  39 import javax.lang.model.element.*;
  40 import javax.lang.model.util.*;
  41 import static javax.lang.model.util.ElementFilter.*;
  42 import static javax.tools.Diagnostic.Kind.*;
  43 import static javax.tools.StandardLocation.*;
  44 import java.io.IOException;
  45 import java.io.PrintWriter;
  46 
  47 /**
  48  * Test basic functionality of the Filer.getResource method.  On the
  49  * first run of the annotation processor, write out a resource file
  50  * and on the second run read it in.
  51  */
  52 @SupportedAnnotationTypes("*")
  53 @SupportedOptions("phase")
  54 public class TestGetResource extends AbstractProcessor {
  55     private Messager messager;
  56     private Filer filer;
  57     private Map<String,String> options;
  58 
  59     private static String CONTENTS = "Hello World.";
  60     private static String PKG = "";
  61     private static String RESOURCE_NAME = "Resource1";
  62 
  63     public boolean process(Set<? extends TypeElement> annotations,
  64                            RoundEnvironment roundEnv) {
  65         try {
  66             if (!roundEnv.processingOver()) {
  67                 String phase = options.get("phase");
  68 
  69                 if (phase.equals("write")) {
  70                     PrintWriter pw =
  71                         new PrintWriter(filer.createResource(CLASS_OUTPUT, PKG, RESOURCE_NAME).openWriter());
  72                     pw.print(CONTENTS);
  73                     pw.close();
  74                 } else if (phase.equals("read")) {
  75                     String contents = filer.getResource(CLASS_OUTPUT,
  76                                                        PKG,
  77                                                        RESOURCE_NAME).getCharContent(false).toString();
  78                     if (!contents.equals(CONTENTS))
  79                         throw new RuntimeException("Expected \n\t" + CONTENTS +
  80                                                    "\nbut instead got \n\t" +
  81                                                    contents);
  82                     // Now try to open the file for writing
  83                     filer.createResource(CLASS_OUTPUT,
  84                                          PKG,
  85                                          RESOURCE_NAME);
  86                 } else {
  87                     throw new RuntimeException("Unexpected phase: " + phase);
  88                 }
  89             }
  90         } catch(IOException ioe) {
  91             throw new RuntimeException(ioe);
  92         }
  93         return false;
  94     }
  95 
  96     public SourceVersion getSupportedSourceVersion() {
  97         return SourceVersion.latest();
  98     }
  99 
 100     public void init(ProcessingEnvironment processingEnv) {
 101         super.init(processingEnv);
 102         messager = processingEnv.getMessager();
 103         filer    = processingEnv.getFiler();
 104         options  = processingEnv.getOptions();
 105     }
 106 }
   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 6449798
  27  * @summary Test Filer.getResource
  28  * @author  Joseph D. Darcy
  29  * @library ../../lib
  30  * @build  JavacTestingAbstractProcessor
  31  * @build TestGetResource
  32  * @compile -processor TestGetResource -proc:only -Aphase=write TestGetResource.java
  33  * @compile -processor TestGetResource -proc:only -Aphase=read  TestGetResource.java
  34  */
  35 
  36 import java.util.Set;
  37 import java.util.Map;
  38 import javax.annotation.processing.*;
  39 import javax.lang.model.SourceVersion;
  40 import static javax.lang.model.SourceVersion.*;
  41 import javax.lang.model.element.*;
  42 import javax.lang.model.util.*;
  43 import static javax.lang.model.util.ElementFilter.*;
  44 import static javax.tools.Diagnostic.Kind.*;
  45 import static javax.tools.StandardLocation.*;
  46 import java.io.IOException;
  47 import java.io.PrintWriter;
  48 
  49 /**
  50  * Test basic functionality of the Filer.getResource method.  On the
  51  * first run of the annotation processor, write out a resource file
  52  * and on the second run read it in.
  53  */

  54 @SupportedOptions("phase")
  55 public class TestGetResource extends JavacTestingAbstractProcessor {




  56     private static String CONTENTS = "Hello World.";
  57     private static String PKG = "";
  58     private static String RESOURCE_NAME = "Resource1";
  59 
  60     public boolean process(Set<? extends TypeElement> annotations,
  61                            RoundEnvironment roundEnv) {
  62         try {
  63             if (!roundEnv.processingOver()) {
  64                 String phase = options.get("phase");
  65 
  66                 if (phase.equals("write")) {
  67                     PrintWriter pw =
  68                         new PrintWriter(filer.createResource(CLASS_OUTPUT, PKG, RESOURCE_NAME).openWriter());
  69                     pw.print(CONTENTS);
  70                     pw.close();
  71                 } else if (phase.equals("read")) {
  72                     String contents = filer.getResource(CLASS_OUTPUT,
  73                                                        PKG,
  74                                                        RESOURCE_NAME).getCharContent(false).toString();
  75                     if (!contents.equals(CONTENTS))
  76                         throw new RuntimeException("Expected \n\t" + CONTENTS +
  77                                                    "\nbut instead got \n\t" +
  78                                                    contents);
  79                     // Now try to open the file for writing
  80                     filer.createResource(CLASS_OUTPUT,
  81                                          PKG,
  82                                          RESOURCE_NAME);
  83                 } else {
  84                     throw new RuntimeException("Unexpected phase: " + phase);
  85                 }
  86             }
  87         } catch(IOException ioe) {
  88             throw new RuntimeException(ioe);
  89         }
  90         return false;
  91     }











  92 }