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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 554
9d9f26857129
permissions
-rw-r--r--

Initial load

     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 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  */
    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;
    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;
    59     private static String CONTENTS = "Hello World.";
    60     private static String PKG = "";
    61     private static String RESOURCE_NAME = "Resource1";
    63     public boolean process(Set<? extends TypeElement> annotations,
    64                            RoundEnvironment roundEnv) {
    65         try {
    66             if (!roundEnv.processingOver()) {
    67                 String phase = options.get("phase");
    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     }
    96     public SourceVersion getSupportedSourceVersion() {
    97         return SourceVersion.latest();
    98     }
   100     public void init(ProcessingEnvironment processingEnv) {
   101         super.init(processingEnv);
   102         messager = processingEnv.getMessager();
   103         filer    = processingEnv.getFiler();
   104         options  = processingEnv.getOptions();
   105     }
   106 }

mercurial