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

mercurial