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