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 the annotation types targeting array types
  27  */
  28 
  29 import com.sun.tools.javac.api.JavacTool;
  30 import java.io.File;
  31 import java.io.PrintWriter;
  32 import java.util.Arrays;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.HashMap;
  36 import java.lang.annotation.*;
  37 import javax.tools.JavaFileManager;
  38 import javax.tools.JavaFileObject;
  39 import com.sun.source.tree.*;
  40 import com.sun.source.util.JavacTask;
  41 import com.sun.source.util.TreeScanner;
  42 import javax.tools.StandardJavaFileManager;
  43 
  44 
  45 public class AnnotatedArrayOrder {
  46     public static void main(String[] args) throws Exception {
  47         PrintWriter out = new PrintWriter(System.out, true);
  48         JavacTool tool = JavacTool.create();
  49         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
  50         File testSrc = new File(System.getProperty("test.src"));
  51         Iterable<? extends JavaFileObject> f =
  52             fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "AnnotatedArrayOrder.java")));
  53         JavacTask task = tool.getTask(out, fm, null, null, null, f);
  54         Iterable<? extends CompilationUnitTree> trees = task.parse();
  55         out.flush();
  56 
  57         Scanner s = new Scanner();
  58         for (CompilationUnitTree t: trees)
  59             s.scan(t, null);
  60 
  61     }
  62 
  63     private static class Scanner extends TreeScanner<Void,Void> {
  64         public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) {
  65             super.visitCompilationUnit(node, ignore);
  66             if (!expectedLocations.isEmpty()) {
  67                 throw new AssertionError("Didn't found all annotations: " + expectedLocations);
  68             }
  69             return null;
  70         }
  71 
  72         private void testAnnotations(List<? extends AnnotationTree> annos, int found) {
  73             String annotation = annos.get(0).toString();
  74 
  75             if (!expectedLocations.containsKey(annotation))
  76                 throw new AssertionError("Found unexpected annotation: " + annotation + expectedLocations);
  77 
  78             int expected = expectedLocations.get(annotation);
  79             if (found != expected)
  80                 throw new AssertionError("The expected array length for this error doesn't match");
  81 
  82             expectedLocations.remove(annotation);
  83         }
  84 
  85         public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) {
  86             testAnnotations(node.getAnnotations(), arrayLength(node));
  87             return super.visitAnnotatedType(node, ignore);
  88         }
  89 
  90         private int arrayLength(Tree tree) {
  91             switch (tree.getKind()) {
  92             case ARRAY_TYPE:
  93                 return 1 + arrayLength(((ArrayTypeTree)tree).getType());
  94             case ANNOTATED_TYPE:
  95                 return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType());
  96             default:
  97                 return 0;
  98             }
  99         }
 100     }
 101 
 102     // expectedLocations values:
 103     static Map<String, Integer> expectedLocations = new HashMap<String, Integer>();
 104 
 105     // visited code
 106     @A String @C [] @B [] field;
 107     static {
 108         // Shouldn't find @A(), as it is field annotation
 109         expectedLocations.put("@B()", 1);
 110         expectedLocations.put("@C()", 2);
 111     }
 112 
 113     List<@D String @F [] @E []> typearg;
 114     static {
 115         expectedLocations.put("@D()", 0);
 116         expectedLocations.put("@E()", 1);
 117         expectedLocations.put("@F()", 2);
 118     }
 119 
 120     void varargSimple(@G String @H ... vararg1) { }
 121     static {
 122         // Shouldn't find @G(), as it is a parameter annotation
 123         expectedLocations.put("@H()", 1);
 124     }
 125 
 126     void varargLong(@I String @L [] @K [] @J ... vararg2) { }
 127     static {
 128         // Shouldn't find @I(), as it is a parameter annotation
 129         expectedLocations.put("@J()", 1);
 130         expectedLocations.put("@K()", 2);
 131         expectedLocations.put("@L()", 3);
 132     }
 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     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 142     @interface D {}
 143     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 144     @interface E {}
 145     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 146     @interface F {}
 147 
 148     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 149     @interface G {}
 150     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 151     @interface H {}
 152     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 153     @interface I {}
 154 
 155     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 156     @interface J {}
 157     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 158     @interface K {}
 159     @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
 160     @interface L {}
 161 }