1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/6499119/ClassProcessor.java Fri Jan 29 16:54:52 2010 -0800 1.3 @@ -0,0 +1,132 @@ 1.4 +/* 1.5 + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +import java.io.*; 1.28 +import java.util.*; 1.29 +import javax.annotation.processing.*; 1.30 +import javax.lang.model.element.*; 1.31 +import javax.lang.model.SourceVersion; 1.32 +import javax.tools.Diagnostic.Kind; 1.33 + 1.34 +/* 1.35 + * @test 1.36 + * @bug 6499119 1.37 + * @summary Created package-info class file modeled improperly 1.38 + * @compile ClassProcessor.java package-info.java 1.39 + * @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object 1.40 + * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object 1.41 + */ 1.42 + 1.43 +@SupportedOptions({ "gen", "expect" }) 1.44 +@SupportedAnnotationTypes({"*"}) 1.45 +public class ClassProcessor extends AbstractProcessor { 1.46 + int round = 1; 1.47 + 1.48 + public SourceVersion getSupportedSourceVersion() { 1.49 + return SourceVersion.latest(); 1.50 + } 1.51 + 1.52 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 1.53 + if (round == 1) { 1.54 + System.out.println("-- Round 1 --"); 1.55 + createPackageFile(); 1.56 + } else if (round == 2) { 1.57 + boolean found_foo_A = false; 1.58 + System.out.println("-- Round 2 --"); 1.59 + for(Element e: roundEnv.getRootElements()) { 1.60 + System.out.println("ElementKind: " + e.getKind()); 1.61 + System.out.println("Modifiers: " + e.getModifiers()); 1.62 + System.out.println("Annotations: " + e.getAnnotationMirrors()); 1.63 + if (e.getAnnotationMirrors().toString().equals("@foo.A")) { 1.64 + found_foo_A = true; 1.65 + checkEqual("ElementKind", e.getKind().toString(), "PACKAGE"); 1.66 + checkEqual("Modifiers", e.getModifiers().toString(), "[]"); 1.67 + } 1.68 + } 1.69 + if (!found_foo_A) 1.70 + error("did not find @foo.A"); 1.71 + } 1.72 + round++; 1.73 + return true; 1.74 + } 1.75 + 1.76 + private void createPackageFile() { 1.77 + Filer filer = processingEnv.getFiler(); 1.78 + 1.79 + String kind = processingEnv.getOptions().get("kind"); 1.80 + 1.81 + File pkgInfo; 1.82 + if (kind.equals("java")) 1.83 + pkgInfo = new File(System.getProperty("test.src"), "package-info.java"); 1.84 + else 1.85 + pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class"); 1.86 + 1.87 + byte[] bytes = new byte[(int) pkgInfo.length()]; 1.88 + DataInputStream in = null; 1.89 + try { 1.90 + in = new DataInputStream(new FileInputStream(pkgInfo)); 1.91 + in.readFully(bytes); 1.92 + } catch (IOException ioe) { 1.93 + error("Couldn't read package info file: " + ioe); 1.94 + } finally { 1.95 + if(in != null) { 1.96 + try { 1.97 + in.close(); 1.98 + } catch (IOException e) { 1.99 + error("InputStream closing failed: " + e); 1.100 + } 1.101 + } 1.102 + } 1.103 + 1.104 + OutputStream out = null; 1.105 + try { 1.106 + if (kind.equals("java")) 1.107 + out = filer.createSourceFile("foo.package-info").openOutputStream(); 1.108 + else 1.109 + out = filer.createClassFile("foo.package-info").openOutputStream(); 1.110 + out.write(bytes, 0, bytes.length); 1.111 + } catch (IOException ioe) { 1.112 + error("Couldn't create package info file: " + ioe); 1.113 + } finally { 1.114 + if(out != null) { 1.115 + try { 1.116 + out.close(); 1.117 + } catch (IOException e) { 1.118 + error("OutputStream closing failed: " + e); 1.119 + } 1.120 + } 1.121 + } 1.122 + } 1.123 + 1.124 + private void checkEqual(String label, String actual, String expect) { 1.125 + if (!actual.equals(expect)) { 1.126 + error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect); 1.127 + } 1.128 + } 1.129 + 1.130 + private void error(String msg) { 1.131 + Messager messager = processingEnv.getMessager(); 1.132 + messager.printMessage(Kind.ERROR, msg); 1.133 + } 1.134 +} 1.135 +