1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/neg/8022765/VerifyAnnotationsAttributed.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,78 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 +import com.sun.source.tree.CompilationUnitTree; 1.27 +import com.sun.source.tree.IdentifierTree; 1.28 +import com.sun.source.tree.MemberSelectTree; 1.29 +import com.sun.source.util.JavacTask; 1.30 +import com.sun.source.util.TreePathScanner; 1.31 +import com.sun.source.util.Trees; 1.32 +import com.sun.tools.javac.api.JavacTool; 1.33 +import com.sun.tools.javac.file.JavacFileManager; 1.34 +import java.io.File; 1.35 +import java.io.IOException; 1.36 +import java.net.URISyntaxException; 1.37 +import java.util.Collections; 1.38 +import javax.lang.model.element.Element; 1.39 +import javax.lang.model.element.ElementKind; 1.40 + 1.41 +public class VerifyAnnotationsAttributed { 1.42 + public static void main(String... args) throws IOException, URISyntaxException { 1.43 + if (args.length != 1) throw new IllegalStateException("Must provide class name!"); 1.44 + File testSrc = new File(System.getProperty("test.src")); 1.45 + File testFile = new File(testSrc, args[0]); 1.46 + if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source"); 1.47 + JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); 1.48 + JavacTask task = JavacTool.create().getTask(null, 1.49 + fm, 1.50 + null, 1.51 + Collections.<String>emptyList(), 1.52 + null, 1.53 + fm.getJavaFileObjects(testFile)); 1.54 + final Trees trees = Trees.instance(task); 1.55 + final CompilationUnitTree cut = task.parse().iterator().next(); 1.56 + task.analyze(); 1.57 + 1.58 + //ensure all the annotation attributes are annotated meaningfully 1.59 + //all the attributes in the test file should contain either an identifier 1.60 + //or a select, so only checking those for a reasonable Element/Symbol. 1.61 + new TreePathScanner<Void, Void>() { 1.62 + @Override 1.63 + public Void visitIdentifier(IdentifierTree node, Void p) { 1.64 + verifyAttributedMeaningfully(); 1.65 + return super.visitIdentifier(node, p); 1.66 + } 1.67 + @Override 1.68 + public Void visitMemberSelect(MemberSelectTree node, Void p) { 1.69 + verifyAttributedMeaningfully(); 1.70 + return super.visitMemberSelect(node, p); 1.71 + } 1.72 + private void verifyAttributedMeaningfully() { 1.73 + Element el = trees.getElement(getCurrentPath()); 1.74 + 1.75 + if (el == null || el.getKind() == ElementKind.OTHER) { 1.76 + throw new IllegalStateException("Not attributed properly: " + getCurrentPath().getParentPath().getLeaf()); 1.77 + } 1.78 + } 1.79 + }.scan(cut, null); 1.80 + } 1.81 +}