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