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