1/*
2 * Copyright (c) 2014, 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
24/*
25 * @test
26 * @bug 8042345
27 * @summary getDocComment() fails for doc comments on PackageElement found in package-info.java
28 * @library /tools/javac/lib
29 * @modules jdk.compiler
30 * @build JavacTestingAbstractProcessor TestPackageInfoComments
31 * @run main TestPackageInfoComments
32 */
33import com.sun.source.util.JavacTask;
34
35import java.io.*;
36import java.util.*;
37import javax.annotation.processing.*;
38import javax.lang.model.*;
39import javax.lang.model.element.*;
40import javax.lang.model.util.*;
41import javax.tools.*;
42
43public class TestPackageInfoComments extends JavacTestingAbstractProcessor {
44
45    public static void main(String... args) throws Exception {
46        String[] opts = {
47            "-implicit:none",
48            "-processor", TestPackageInfoComments.class.getName(),
49            "-processorpath", System.getProperty("test.class.path")
50        };
51        File[] files = {
52            new File(System.getProperty("test.src"), "p/package-info.java")
53        };
54        run_test(opts, files);
55    }
56
57    static void run_test(String[] opts, File[] files) throws IOException {
58        DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
59            public void report(Diagnostic diagnostic) {
60                throw new Error(diagnostic.toString());
61            }
62        };
63        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
64        try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
65            Iterable<? extends JavaFileObject> units = fm.getJavaFileObjects(files);
66            JavacTask t = (JavacTask) c.getTask(null, fm, dl, Arrays.asList(opts), null, units);
67            t.parse();
68            t.analyze();
69        }
70    }
71
72    // -- Annotation processor: Check all PackageDecl's have a doc comment
73
74    @Override
75    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
76        for (Element e: roundEnv.getRootElements())
77            new TestElementScanner().scan(e);
78        return true;
79    }
80
81    class TestElementScanner extends ElementScanner<Void, Void> {
82        @Override
83        public Void visitPackage(PackageElement e, Void v) {
84            if (elements.getDocComment(e) == null)
85                messager.printMessage(Diagnostic.Kind.ERROR, "doc comment is null", e);
86            return v;
87        }
88    }
89}
90