test/tools/javac/annotations/typeAnnotations/api/AnnotatedArrayOrder.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1534
bec996065c45
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

jjg@1521 1 /*
ksrini@2227 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1521 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1521 4 *
jjg@1521 5 * This code is free software; you can redistribute it and/or modify it
jjg@1521 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1521 7 * published by the Free Software Foundation.
jjg@1521 8 *
jjg@1521 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1521 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1521 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1521 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1521 13 * accompanied this code).
jjg@1521 14 *
jjg@1521 15 * You should have received a copy of the GNU General Public License version
jjg@1521 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1521 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1521 18 *
jjg@1521 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1521 20 * or visit www.oracle.com if you need additional information or have any
jjg@1521 21 * questions.
jjg@1521 22 */
jjg@1521 23
jjg@1521 24 /*
jjg@1521 25 * @test
jjg@1521 26 * @summary Checks the annotation types targeting array types
jjg@1521 27 */
jjg@1521 28
jjg@1521 29 import com.sun.tools.javac.api.JavacTool;
jjg@1521 30 import java.io.File;
jjg@1521 31 import java.io.PrintWriter;
jjg@1521 32 import java.util.Arrays;
jjg@1521 33 import java.util.List;
jjg@1521 34 import java.util.Map;
jjg@1521 35 import java.util.HashMap;
jjg@1521 36 import java.lang.annotation.*;
jjg@1521 37 import javax.tools.JavaFileManager;
jjg@1521 38 import javax.tools.JavaFileObject;
jjg@1521 39 import com.sun.source.tree.*;
jjg@1521 40 import com.sun.source.util.JavacTask;
jjg@1521 41 import com.sun.source.util.TreeScanner;
jjg@1521 42 import javax.tools.StandardJavaFileManager;
jjg@1521 43
jjg@1521 44
jjg@1521 45 public class AnnotatedArrayOrder {
jjg@1521 46 public static void main(String[] args) throws Exception {
jjg@1521 47 PrintWriter out = new PrintWriter(System.out, true);
jjg@1521 48 JavacTool tool = JavacTool.create();
jjg@1521 49 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
jjg@1521 50 File testSrc = new File(System.getProperty("test.src"));
jjg@1521 51 Iterable<? extends JavaFileObject> f =
jjg@1521 52 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "AnnotatedArrayOrder.java")));
jjg@1521 53 JavacTask task = tool.getTask(out, fm, null, null, null, f);
jjg@1521 54 Iterable<? extends CompilationUnitTree> trees = task.parse();
jjg@1521 55 out.flush();
jjg@1521 56
jjg@1521 57 Scanner s = new Scanner();
jjg@1521 58 for (CompilationUnitTree t: trees)
jjg@1521 59 s.scan(t, null);
jjg@1521 60
jjg@1521 61 }
jjg@1521 62
jjg@1521 63 private static class Scanner extends TreeScanner<Void,Void> {
jjg@1521 64 public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) {
jjg@1521 65 super.visitCompilationUnit(node, ignore);
jjg@1521 66 if (!expectedLocations.isEmpty()) {
jjg@1521 67 throw new AssertionError("Didn't found all annotations: " + expectedLocations);
jjg@1521 68 }
jjg@1521 69 return null;
jjg@1521 70 }
jjg@1521 71
jjg@1521 72 private void testAnnotations(List<? extends AnnotationTree> annos, int found) {
jjg@1521 73 String annotation = annos.get(0).toString();
jjg@1521 74
jjg@1521 75 if (!expectedLocations.containsKey(annotation))
jjg@1521 76 throw new AssertionError("Found unexpected annotation: " + annotation + expectedLocations);
jjg@1521 77
jjg@1521 78 int expected = expectedLocations.get(annotation);
jjg@1521 79 if (found != expected)
jjg@1521 80 throw new AssertionError("The expected array length for this error doesn't match");
jjg@1521 81
jjg@1521 82 expectedLocations.remove(annotation);
jjg@1521 83 }
jjg@1521 84
jjg@1521 85 public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) {
jjg@1521 86 testAnnotations(node.getAnnotations(), arrayLength(node));
jjg@1521 87 return super.visitAnnotatedType(node, ignore);
jjg@1521 88 }
jjg@1521 89
jjg@1521 90 private int arrayLength(Tree tree) {
jjg@1521 91 switch (tree.getKind()) {
jjg@1521 92 case ARRAY_TYPE:
jjg@1521 93 return 1 + arrayLength(((ArrayTypeTree)tree).getType());
jjg@1521 94 case ANNOTATED_TYPE:
jjg@1521 95 return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType());
jjg@1521 96 default:
jjg@1521 97 return 0;
jjg@1521 98 }
jjg@1521 99 }
jjg@1521 100 }
jjg@1521 101
jjg@1521 102 // expectedLocations values:
jjg@1521 103 static Map<String, Integer> expectedLocations = new HashMap<String, Integer>();
jjg@1521 104
jjg@1521 105 // visited code
jjg@1521 106 @A String @C [] @B [] field;
jjg@1521 107 static {
jjg@1521 108 // Shouldn't find @A(), as it is field annotation
jjg@1521 109 expectedLocations.put("@B()", 1);
jjg@1521 110 expectedLocations.put("@C()", 2);
jjg@1521 111 }
jjg@1521 112
jjg@1521 113 List<@D String @F [] @E []> typearg;
jjg@1521 114 static {
jjg@1521 115 expectedLocations.put("@D()", 0);
jjg@1521 116 expectedLocations.put("@E()", 1);
jjg@1521 117 expectedLocations.put("@F()", 2);
jjg@1521 118 }
jjg@1521 119
jjg@1521 120 void varargSimple(@G String @H ... vararg1) { }
jjg@1521 121 static {
jjg@1521 122 // Shouldn't find @G(), as it is a parameter annotation
jjg@1521 123 expectedLocations.put("@H()", 1);
jjg@1521 124 }
jjg@1521 125
jjg@1521 126 void varargLong(@I String @L [] @K [] @J ... vararg2) { }
jjg@1521 127 static {
jjg@1521 128 // Shouldn't find @I(), as it is a parameter annotation
jjg@1521 129 expectedLocations.put("@J()", 1);
jjg@1521 130 expectedLocations.put("@K()", 2);
jjg@1521 131 expectedLocations.put("@L()", 3);
jjg@1521 132 }
jjg@1521 133
jjg@1521 134 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 135 @interface A {}
jjg@1521 136 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 137 @interface B {}
jjg@1521 138 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 139 @interface C {}
jjg@1521 140
jjg@1521 141 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 142 @interface D {}
jjg@1521 143 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 144 @interface E {}
jjg@1521 145 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 146 @interface F {}
jjg@1521 147
jjg@1521 148 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 149 @interface G {}
jjg@1521 150 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 151 @interface H {}
jjg@1521 152 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 153 @interface I {}
jjg@1521 154
jjg@1521 155 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 156 @interface J {}
jjg@1521 157 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 158 @interface K {}
jjg@1521 159 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 160 @interface L {}
jjg@1521 161 }

mercurial