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

     1 /*
     2  * Copyright (c) 2010, 2013, 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  */
    24 /*
    25  * @test
    26  * @summary Checks the annotation types targeting array types
    27  */
    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;
    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();
    57         Scanner s = new Scanner();
    58         for (CompilationUnitTree t: trees)
    59             s.scan(t, null);
    61     }
    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         }
    72         private void testAnnotations(List<? extends AnnotationTree> annos, int found) {
    73             String annotation = annos.get(0).toString();
    75             if (!expectedLocations.containsKey(annotation))
    76                 throw new AssertionError("Found unexpected annotation: " + annotation + expectedLocations);
    78             int expected = expectedLocations.get(annotation);
    79             if (found != expected)
    80                 throw new AssertionError("The expected array length for this error doesn't match");
    82             expectedLocations.remove(annotation);
    83         }
    85         public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) {
    86             testAnnotations(node.getAnnotations(), arrayLength(node));
    87             return super.visitAnnotatedType(node, ignore);
    88         }
    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     }
   102     // expectedLocations values:
   103     static Map<String, Integer> expectedLocations = new HashMap<String, Integer>();
   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     }
   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     }
   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     }
   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     }
   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 {}
   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 {}
   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 {}
   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 }

mercurial