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