1 /* 2 * Copyright (c) 2010, 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 * @summary Checks that the interaction between annotated and unannotated 27 * array levels 28 */ 29 30 import com.sun.tools.javac.api.JavacTool; 31 import java.lang.annotation.*; 32 import java.io.File; 33 import java.io.PrintWriter; 34 import java.util.Arrays; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.HashMap; 38 import javax.tools.JavaFileManager; 39 import javax.tools.JavaFileObject; 40 import com.sun.source.tree.*; 41 import com.sun.source.util.JavacTask; 42 import com.sun.source.util.TreeScanner; 43 import javax.tools.StandardJavaFileManager; 44 45 46 public class ArrayPositionConsistency { 47 public static void main(String[] args) throws Exception { 48 PrintWriter out = new PrintWriter(System.out, true); 49 JavacTool tool = JavacTool.create(); 50 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 51 File testSrc = new File(System.getProperty("test.src")); 52 Iterable<? extends JavaFileObject> f = 53 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayPositionConsistency.java"))); 54 JavacTask task = tool.getTask(out, fm, null, null, null, f); 55 Iterable<? extends CompilationUnitTree> trees = task.parse(); 56 out.flush(); 57 58 Scanner s = new Scanner(); 59 for (CompilationUnitTree t: trees) 60 s.scan(t, null); 61 62 } 63 64 private static class Scanner extends TreeScanner<Void,Void> { 65 int foundAnnotations = 0; 66 public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) { 67 super.visitCompilationUnit(node, ignore); 68 if (foundAnnotations != expectedAnnotations) { 69 throw new AssertionError("Expected " + expectedAnnotations + 70 " annotations but found: " + foundAnnotations); 71 } 72 return null; 73 } 74 75 private void testAnnotations(List<? extends AnnotationTree> annos, int found) { 76 String annotation = annos.get(0).toString(); 77 foundAnnotations++; 78 79 int expected = -1; 80 if (annotation.equals("@A()")) 81 expected = 0; 82 else if (annotation.equals("@B()")) 83 expected = 1; 84 else if (annotation.equals("@C()")) 85 expected = 2; 86 else 87 throw new AssertionError("found an unexpected annotation: " + annotation); 88 if (found != expected) { 89 throw new AssertionError("Unexpected found length" + 90 ", found " + found + " but expected " + expected); 91 } 92 } 93 94 public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) { 95 testAnnotations(node.getAnnotations(), arrayLength(node)); 96 return super.visitAnnotatedType(node, ignore); 97 } 98 99 private int arrayLength(Tree tree) { 100 switch (tree.getKind()) { 101 case ARRAY_TYPE: 102 return 1 + arrayLength(((ArrayTypeTree)tree).getType()); 103 case ANNOTATED_TYPE: 104 return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType()); 105 default: 106 return 0; 107 } 108 } 109 } 110 111 static int expectedAnnotations = 23; 112 113 // visited code 114 @A String @C [] @B [] field1; 115 @A String @C [] [] field2; 116 @A String [] @B [] field3; 117 String [] @B [] field4; 118 119 @A List<String> @C [] @B [] genfield1; 120 @A List<String> @C [] [] genfield2; 121 @A List<String> [] @B [] genfield3; 122 List<String> [] @B [] genfield4; 123 124 List<@A String @C [] @B []> typearg1; 125 List<@A String @C [] []> typearg2; 126 List<@A String [] @B []> typearg3; 127 List< String [] @B []> typearg4; 128 129 void vararg1(@A String @C [] @B ... arg) {} 130 void vararg2(@A String @C [] ... arg) {} 131 void vararg3(@A String [] @B ... arg) {} 132 void vararg4( String [] @B ... arg) {} 133 134 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 135 @interface A {} 136 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 137 @interface B {} 138 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) 139 @interface C {} 140 141 }