1/*
2 * Copyright (c) 2010, 2015, 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
24import java.io.*;
25import java.util.*;
26import javax.annotation.processing.*;
27import javax.lang.model.element.*;
28import javax.lang.model.SourceVersion;
29import javax.tools.Diagnostic.Kind;
30
31/*
32 * @test
33 * @bug 6499119
34 * @summary Created package-info class file modeled improperly
35 * @library /tools/javac/lib
36 * @modules java.compiler
37 *          jdk.compiler
38 * @build   JavacTestingAbstractProcessor
39 * @compile ClassProcessor.java package-info.java
40 * @compile/process -cp . -processor ClassProcessor -Akind=java  java.lang.Object
41 * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
42 */
43
44@SupportedOptions({ "gen", "expect" })
45public class ClassProcessor extends JavacTestingAbstractProcessor {
46    int round = 1;
47
48    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
49        if (round == 1) {
50            System.out.println("-- Round 1 --");
51            createPackageFile();
52        } else if (round == 2) {
53            boolean found_foo_A = false;
54            System.out.println("-- Round 2 --");
55            for(Element e: roundEnv.getRootElements()) {
56                System.out.println("ElementKind: " + e.getKind());
57                System.out.println("Modifiers:   " + e.getModifiers());
58                System.out.println("Annotations: " + e.getAnnotationMirrors());
59                if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
60                    found_foo_A = true;
61                    checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
62                    checkEqual("Modifiers",   e.getModifiers().toString(), "[]");
63                }
64            }
65            if (!found_foo_A)
66                error("did not find @foo.A");
67        }
68        round++;
69        return true;
70    }
71
72    private void createPackageFile() {
73        String kind = processingEnv.getOptions().get("kind");
74
75        File pkgInfo;
76        if (kind.equals("java"))
77            pkgInfo = new File(System.getProperty("test.src"),     "package-info.java");
78        else
79            pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
80
81        byte[] bytes = new byte[(int) pkgInfo.length()];
82        DataInputStream in = null;
83        try {
84            in = new DataInputStream(new FileInputStream(pkgInfo));
85            in.readFully(bytes);
86        } catch (IOException ioe) {
87            error("Couldn't read package info file: " + ioe);
88        } finally {
89            if(in != null) {
90                try {
91                    in.close();
92                } catch (IOException e) {
93                    error("InputStream closing failed: " + e);
94                }
95            }
96        }
97
98        OutputStream out = null;
99        try {
100            if (kind.equals("java"))
101                out = filer.createSourceFile("foo.package-info").openOutputStream();
102            else
103                out = filer.createClassFile("foo.package-info").openOutputStream();
104            out.write(bytes, 0, bytes.length);
105        } catch (IOException ioe) {
106            error("Couldn't create package info file: " + ioe);
107        } finally {
108            if(out != null) {
109                try {
110                    out.close();
111                } catch (IOException e) {
112                    error("OutputStream closing failed: " + e);
113                }
114            }
115        }
116    }
117
118    private void checkEqual(String label, String actual, String expect) {
119        if (!actual.equals(expect)) {
120            error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
121        }
122    }
123
124    private void error(String msg) {
125        messager.printMessage(Kind.ERROR, msg);
126    }
127}
128
129