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 24/* 25 * @test 26 * @bug 8031744 27 * @summary Checks the annotation types targeting array types 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.file 30 */ 31 32import com.sun.tools.javac.api.JavacTool; 33import java.io.File; 34import java.io.PrintWriter; 35import java.util.Arrays; 36import java.util.List; 37import java.util.Map; 38import java.util.HashMap; 39import java.lang.annotation.*; 40import javax.tools.JavaFileObject; 41import com.sun.source.tree.*; 42import com.sun.source.util.JavacTask; 43import com.sun.source.util.TreeScanner; 44import javax.tools.StandardJavaFileManager; 45 46 47public class AnnotatedArrayOrder { 48 public static void main(String[] args) throws Exception { 49 PrintWriter out = new PrintWriter(System.out, true); 50 JavacTool tool = JavacTool.create(); 51 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 52 File testSrc = new File(System.getProperty("test.src")); 53 Iterable<? extends JavaFileObject> f = 54 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "AnnotatedArrayOrder.java"))); 55 JavacTask task = tool.getTask(out, fm, null, null, null, f); 56 Iterable<? extends CompilationUnitTree> trees = task.parse(); 57 out.flush(); 58 59 Scanner s = new Scanner(); 60 for (CompilationUnitTree t: trees) 61 s.scan(t, null); 62 } 63 } 64 65 private static class Scanner extends TreeScanner<Void,Void> { 66 public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) { 67 super.visitCompilationUnit(node, ignore); 68 if (!expectedLocations.isEmpty()) { 69 throw new AssertionError("Didn't found all annotations: " + expectedLocations); 70 } 71 return null; 72 } 73 74 private void testAnnotations(List<? extends AnnotationTree> annos, int found) { 75 String annotation = annos.get(0).toString(); 76 77 if (!expectedLocations.containsKey(annotation)) 78 throw new AssertionError("Found unexpected annotation: " + annotation + expectedLocations); 79 80 int expected = expectedLocations.get(annotation); 81 if (found != expected) 82 throw new AssertionError("The expected array length for this error doesn't match"); 83 84 expectedLocations.remove(annotation); 85 } 86 87 public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) { 88 testAnnotations(node.getAnnotations(), arrayLength(node)); 89 return super.visitAnnotatedType(node, ignore); 90 } 91 92 private int arrayLength(Tree tree) { 93 switch (tree.getKind()) { 94 case ARRAY_TYPE: 95 return 1 + arrayLength(((ArrayTypeTree)tree).getType()); 96 case ANNOTATED_TYPE: 97 return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType()); 98 default: 99 return 0; 100 } 101 } 102 } 103 104 // expectedLocations values: 105 static Map<String, Integer> expectedLocations = new HashMap<String, Integer>(); 106 107 // visited code 108 @A String @C [] @B [] field; 109 static { 110 // Shouldn't find @A(), as it is field annotation 111 expectedLocations.put("@B()", 1); 112 expectedLocations.put("@C()", 2); 113 } 114 115 List<@D String @F [] @E []> typearg; 116 static { 117 expectedLocations.put("@D()", 0); 118 expectedLocations.put("@E()", 1); 119 expectedLocations.put("@F()", 2); 120 } 121 122 void varargSimple(@G String @H ... vararg1) { } 123 static { 124 // Shouldn't find @G(), as it is a parameter annotation 125 expectedLocations.put("@H()", 1); 126 } 127 128 void varargLong(@I String @L [] @K [] @J ... vararg2) { } 129 static { 130 // Shouldn't find @I(), as it is a parameter annotation 131 expectedLocations.put("@J()", 1); 132 expectedLocations.put("@K()", 2); 133 expectedLocations.put("@L()", 3); 134 } 135 136 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 137 @interface A {} 138 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 139 @interface B {} 140 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 141 @interface C {} 142 143 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 144 @interface D {} 145 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 146 @interface E {} 147 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 148 @interface F {} 149 150 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 151 @interface G {} 152 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 153 @interface H {} 154 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 155 @interface I {} 156 157 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 158 @interface J {} 159 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 160 @interface K {} 161 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 162 @interface L {} 163} 164