test/tools/javac/annotations/typeAnnotations/api/ArrayCreationTree.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 in array creation trees
jjg@1521 28 */
jjg@1521 29
jjg@1521 30 import com.sun.tools.javac.api.JavacTool;
jjg@1521 31 import com.sun.tools.javac.tree.JCTree.JCNewArray;
jjg@1521 32 import java.lang.annotation.*;
jjg@1521 33 import java.io.File;
jjg@1521 34 import java.io.PrintWriter;
jjg@1521 35 import java.util.Arrays;
jjg@1521 36 import java.util.List;
jjg@1521 37 import java.util.Map;
jjg@1521 38 import java.util.HashMap;
jjg@1521 39 import javax.tools.JavaFileManager;
jjg@1521 40 import javax.tools.JavaFileObject;
jjg@1521 41 import com.sun.source.tree.*;
jjg@1521 42 import com.sun.source.util.JavacTask;
jjg@1521 43 import com.sun.source.util.TreeScanner;
jjg@1521 44 import javax.tools.StandardJavaFileManager;
jjg@1521 45
jjg@1521 46
jjg@1521 47 public class ArrayCreationTree {
jjg@1521 48 public static void main(String[] args) throws Exception {
jjg@1521 49 PrintWriter out = new PrintWriter(System.out, true);
jjg@1521 50 JavacTool tool = JavacTool.create();
jjg@1521 51 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
jjg@1521 52 File testSrc = new File(System.getProperty("test.src"));
jjg@1521 53 Iterable<? extends JavaFileObject> f =
jjg@1521 54 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayCreationTree.java")));
jjg@1521 55 JavacTask task = tool.getTask(out, fm, null, null, null, f);
jjg@1521 56 Iterable<? extends CompilationUnitTree> trees = task.parse();
jjg@1521 57 out.flush();
jjg@1521 58
jjg@1521 59 Scanner s = new Scanner();
jjg@1521 60 for (CompilationUnitTree t: trees)
jjg@1521 61 s.scan(t, null);
jjg@1521 62
jjg@1521 63 }
jjg@1521 64
jjg@1521 65 private static class Scanner extends TreeScanner<Void,Void> {
jjg@1521 66 int foundAnnotations = 0;
jjg@1521 67 public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) {
jjg@1521 68 super.visitCompilationUnit(node, ignore);
jjg@1521 69 if (foundAnnotations != expectedAnnotations) {
jjg@1521 70 throw new AssertionError("Expected " + expectedAnnotations +
jjg@1521 71 " annotations but found: " + foundAnnotations);
jjg@1521 72 }
jjg@1521 73 return null;
jjg@1521 74 }
jjg@1521 75
jjg@1521 76 private void testAnnotations(List<? extends AnnotationTree> annos, int found) {
jjg@1521 77 if (annos.isEmpty()) return;
jjg@1521 78
jjg@1521 79 String annotation = annos.get(0).toString();
jjg@1521 80 foundAnnotations++;
jjg@1521 81
jjg@1521 82 int expected = -1;
jjg@1521 83 if (annotation.equals("@A()"))
jjg@1521 84 expected = 0;
jjg@1521 85 else if (annotation.equals("@B()"))
jjg@1521 86 expected = 1;
jjg@1521 87 else if (annotation.equals("@C()"))
jjg@1521 88 expected = 2;
jjg@1521 89 else
jjg@1521 90 throw new AssertionError("found an unexpected annotation: " + annotation);
jjg@1521 91 if (found != expected) {
jjg@1521 92 throw new AssertionError("Unexpected found length" +
jjg@1521 93 ", found " + found + " but expected " + expected);
jjg@1521 94 }
jjg@1521 95 }
jjg@1521 96
jjg@1521 97 public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) {
jjg@1521 98 testAnnotations(node.getAnnotations(), arrayLength(node));
jjg@1521 99 return super.visitAnnotatedType(node, ignore);
jjg@1521 100 }
jjg@1521 101
jjg@1521 102 public Void visitNewArray(NewArrayTree node, Void ignore) {
jjg@1521 103 // the Tree API hasn't been updated to expose annotations yet
jjg@1521 104 JCNewArray newArray = (JCNewArray)node;
jjg@1521 105 int totalLength = node.getDimensions().size()
jjg@1521 106 + arrayLength(node.getType())
jjg@1521 107 + ((newArray.getInitializers() != null) ? 1 : 0);
jjg@1521 108 testAnnotations(newArray.annotations, totalLength);
jjg@1521 109 int count = 0;
jjg@1521 110 for (List<? extends AnnotationTree> annos : newArray.dimAnnotations) {
jjg@1521 111 testAnnotations(annos, totalLength - count);
jjg@1521 112 count++;
jjg@1521 113 }
jjg@1521 114 return super.visitNewArray(node, ignore);
jjg@1521 115 }
jjg@1521 116
jjg@1521 117 private int arrayLength(Tree tree) {
jjg@1521 118 // TODO: the tree is null when called with node.getType(). Why?
jjg@1521 119 if (tree==null) return -1;
jjg@1521 120 switch (tree.getKind()) {
jjg@1521 121 case ARRAY_TYPE:
jjg@1521 122 return 1 + arrayLength(((ArrayTypeTree)tree).getType());
jjg@1521 123 case ANNOTATED_TYPE:
jjg@1521 124 return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType());
jjg@1521 125 default:
jjg@1521 126 return 0;
jjg@1521 127 }
jjg@1521 128 }
jjg@1521 129 }
jjg@1521 130
jjg@1521 131 static int expectedAnnotations = 21;
jjg@1521 132
jjg@1521 133 Object a1 = new @A Object @C [2] @B [1];
jjg@1521 134 Object b1 = new @A Object @C [2] @B [ ];
jjg@1521 135 Object c1 = new @A Object @C [ ] @B [ ] { };
jjg@1521 136
jjg@1521 137 Object a2 = new @A Object @C [2] [1];
jjg@1521 138 Object b2 = new @A Object @C [2] [ ];
jjg@1521 139 Object c2 = new @A Object @C [ ] [ ] { };
jjg@1521 140
jjg@1521 141 Object a3 = new @A Object [2] @B [1];
jjg@1521 142 Object b3 = new @A Object [2] @B [ ];
jjg@1521 143 Object c3 = new @A Object [ ] @B [ ] { };
jjg@1521 144
jjg@1521 145 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 146 @interface A {}
jjg@1521 147 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 148 @interface B {}
jjg@1521 149 @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
jjg@1521 150 @interface C {}
jjg@1521 151
jjg@1521 152 }

mercurial