VerifyAnnotationsAttributed.java revision 2687:56f8be952a5c
1/*
2 * Copyright (c) 2013, 2014, 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 */
23import com.sun.source.tree.CompilationUnitTree;
24import com.sun.source.tree.IdentifierTree;
25import com.sun.source.tree.MemberSelectTree;
26import com.sun.source.util.JavacTask;
27import com.sun.source.util.TreePathScanner;
28import com.sun.source.util.Trees;
29import com.sun.tools.javac.api.JavacTool;
30import com.sun.tools.javac.file.JavacFileManager;
31import java.io.File;
32import java.io.IOException;
33import java.net.URISyntaxException;
34import java.util.Collections;
35import javax.lang.model.element.Element;
36import javax.lang.model.element.ElementKind;
37
38public class VerifyAnnotationsAttributed {
39    public static void main(String... args) throws IOException, URISyntaxException {
40        if (args.length != 1) throw new IllegalStateException("Must provide class name!");
41        File testSrc = new File(System.getProperty("test.src"));
42        File testFile = new File(testSrc, args[0]);
43        if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source");
44        try (JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null)) {
45            JavacTask task = JavacTool.create().getTask(null,
46                                                        fm,
47                                                        null,
48                                                        Collections.<String>emptyList(),
49                                                        null,
50                                                        fm.getJavaFileObjects(testFile));
51            final Trees trees = Trees.instance(task);
52            final CompilationUnitTree cut = task.parse().iterator().next();
53            task.analyze();
54
55            //ensure all the annotation attributes are annotated meaningfully
56            //all the attributes in the test file should contain either an identifier
57            //or a select, so only checking those for a reasonable Element/Symbol.
58            new TreePathScanner<Void, Void>() {
59                @Override
60                public Void visitIdentifier(IdentifierTree node, Void p) {
61                    verifyAttributedMeaningfully();
62                    return super.visitIdentifier(node, p);
63                }
64                @Override
65                public Void visitMemberSelect(MemberSelectTree node, Void p) {
66                    verifyAttributedMeaningfully();
67                    return super.visitMemberSelect(node, p);
68                }
69                private void verifyAttributedMeaningfully() {
70                    Element el = trees.getElement(getCurrentPath());
71
72                    if (el == null || el.getKind() == ElementKind.OTHER) {
73                        throw new IllegalStateException("Not attributed properly: " + getCurrentPath().getParentPath().getLeaf());
74                    }
75                }
76            }.scan(cut, null);
77        }
78    }
79}
80