jjg@483: /* ohair@554: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@483: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@483: * jjg@483: * This code is free software; you can redistribute it and/or modify it jjg@483: * under the terms of the GNU General Public License version 2 only, as jjg@483: * published by the Free Software Foundation. jjg@483: * jjg@483: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@483: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@483: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@483: * version 2 for more details (a copy is included in the LICENSE file that jjg@483: * accompanied this code). jjg@483: * jjg@483: * You should have received a copy of the GNU General Public License version jjg@483: * 2 along with this work; if not, write to the Free Software Foundation, jjg@483: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@483: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@483: */ jjg@483: jjg@483: import java.io.*; jjg@483: import java.util.*; jjg@483: import javax.annotation.processing.*; jjg@483: import javax.lang.model.element.*; jjg@483: import javax.lang.model.SourceVersion; jjg@483: import javax.tools.Diagnostic.Kind; jjg@483: jjg@483: /* jjg@483: * @test jjg@483: * @bug 6499119 jjg@483: * @summary Created package-info class file modeled improperly darcy@1466: * @library /tools/javac/lib darcy@699: * @build JavacTestingAbstractProcessor jjg@483: * @compile ClassProcessor.java package-info.java jjg@483: * @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object jjg@483: * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object jjg@483: */ jjg@483: jjg@483: @SupportedOptions({ "gen", "expect" }) darcy@699: public class ClassProcessor extends JavacTestingAbstractProcessor { jjg@483: int round = 1; jjg@483: jjg@483: public boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@483: if (round == 1) { jjg@483: System.out.println("-- Round 1 --"); jjg@483: createPackageFile(); jjg@483: } else if (round == 2) { jjg@483: boolean found_foo_A = false; jjg@483: System.out.println("-- Round 2 --"); jjg@483: for(Element e: roundEnv.getRootElements()) { jjg@483: System.out.println("ElementKind: " + e.getKind()); jjg@483: System.out.println("Modifiers: " + e.getModifiers()); jjg@483: System.out.println("Annotations: " + e.getAnnotationMirrors()); jjg@483: if (e.getAnnotationMirrors().toString().equals("@foo.A")) { jjg@483: found_foo_A = true; jjg@483: checkEqual("ElementKind", e.getKind().toString(), "PACKAGE"); jjg@483: checkEqual("Modifiers", e.getModifiers().toString(), "[]"); jjg@483: } jjg@483: } jjg@483: if (!found_foo_A) jjg@483: error("did not find @foo.A"); jjg@483: } jjg@483: round++; jjg@483: return true; jjg@483: } jjg@483: jjg@483: private void createPackageFile() { jjg@483: String kind = processingEnv.getOptions().get("kind"); jjg@483: jjg@483: File pkgInfo; jjg@483: if (kind.equals("java")) jjg@483: pkgInfo = new File(System.getProperty("test.src"), "package-info.java"); jjg@483: else jjg@483: pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class"); jjg@483: jjg@483: byte[] bytes = new byte[(int) pkgInfo.length()]; jjg@483: DataInputStream in = null; jjg@483: try { jjg@483: in = new DataInputStream(new FileInputStream(pkgInfo)); jjg@483: in.readFully(bytes); jjg@483: } catch (IOException ioe) { jjg@483: error("Couldn't read package info file: " + ioe); jjg@483: } finally { jjg@483: if(in != null) { jjg@483: try { jjg@483: in.close(); jjg@483: } catch (IOException e) { jjg@483: error("InputStream closing failed: " + e); jjg@483: } jjg@483: } jjg@483: } jjg@483: jjg@483: OutputStream out = null; jjg@483: try { jjg@483: if (kind.equals("java")) jjg@483: out = filer.createSourceFile("foo.package-info").openOutputStream(); jjg@483: else jjg@483: out = filer.createClassFile("foo.package-info").openOutputStream(); jjg@483: out.write(bytes, 0, bytes.length); jjg@483: } catch (IOException ioe) { jjg@483: error("Couldn't create package info file: " + ioe); jjg@483: } finally { jjg@483: if(out != null) { jjg@483: try { jjg@483: out.close(); jjg@483: } catch (IOException e) { jjg@483: error("OutputStream closing failed: " + e); jjg@483: } jjg@483: } jjg@483: } jjg@483: } jjg@483: jjg@483: private void checkEqual(String label, String actual, String expect) { jjg@483: if (!actual.equals(expect)) { jjg@483: error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect); jjg@483: } jjg@483: } jjg@483: jjg@483: private void error(String msg) { jjg@483: messager.printMessage(Kind.ERROR, msg); jjg@483: } jjg@483: } jjg@483: