duke@1: /* duke@1: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: /* duke@1: * @test duke@1: * @bug 6380018 6449798 duke@1: * @summary Test Filer.getResource duke@1: * @author Joseph D. Darcy duke@1: * @build TestGetResource duke@1: * @compile -processor TestGetResource -proc:only -Aphase=write TestGetResource.java duke@1: * @compile -processor TestGetResource -proc:only -Aphase=read TestGetResource.java duke@1: */ duke@1: duke@1: import java.util.Set; duke@1: import java.util.Map; duke@1: import javax.annotation.processing.*; duke@1: import javax.lang.model.SourceVersion; duke@1: import static javax.lang.model.SourceVersion.*; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.util.*; duke@1: import static javax.lang.model.util.ElementFilter.*; duke@1: import static javax.tools.Diagnostic.Kind.*; duke@1: import static javax.tools.StandardLocation.*; duke@1: import java.io.IOException; duke@1: import java.io.PrintWriter; duke@1: duke@1: /** duke@1: * Test basic functionality of the Filer.getResource method. On the duke@1: * first run of the annotation processor, write out a resource file duke@1: * and on the second run read it in. duke@1: */ duke@1: @SupportedAnnotationTypes("*") duke@1: @SupportedOptions("phase") duke@1: public class TestGetResource extends AbstractProcessor { duke@1: private Messager messager; duke@1: private Filer filer; duke@1: private Map options; duke@1: duke@1: private static String CONTENTS = "Hello World."; duke@1: private static String PKG = ""; duke@1: private static String RESOURCE_NAME = "Resource1"; duke@1: duke@1: public boolean process(Set annotations, duke@1: RoundEnvironment roundEnv) { duke@1: try { duke@1: if (!roundEnv.processingOver()) { duke@1: String phase = options.get("phase"); duke@1: duke@1: if (phase.equals("write")) { duke@1: PrintWriter pw = duke@1: new PrintWriter(filer.createResource(CLASS_OUTPUT, PKG, RESOURCE_NAME).openWriter()); duke@1: pw.print(CONTENTS); duke@1: pw.close(); duke@1: } else if (phase.equals("read")) { duke@1: String contents = filer.getResource(CLASS_OUTPUT, duke@1: PKG, duke@1: RESOURCE_NAME).getCharContent(false).toString(); duke@1: if (!contents.equals(CONTENTS)) duke@1: throw new RuntimeException("Expected \n\t" + CONTENTS + duke@1: "\nbut instead got \n\t" + duke@1: contents); duke@1: // Now try to open the file for writing duke@1: filer.createResource(CLASS_OUTPUT, duke@1: PKG, duke@1: RESOURCE_NAME); duke@1: } else { duke@1: throw new RuntimeException("Unexpected phase: " + phase); duke@1: } duke@1: } duke@1: } catch(IOException ioe) { duke@1: throw new RuntimeException(ioe); duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: public SourceVersion getSupportedSourceVersion() { duke@1: return SourceVersion.latest(); duke@1: } duke@1: duke@1: public void init(ProcessingEnvironment processingEnv) { duke@1: super.init(processingEnv); duke@1: messager = processingEnv.getMessager(); duke@1: filer = processingEnv.getFiler(); duke@1: options = processingEnv.getOptions(); duke@1: } duke@1: }