test/tools/javac/annotations/typeAnnotations/api/ArrayPositionConsistency.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/api/ArrayPositionConsistency.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @summary Checks that the interaction between annotated and unannotated
    1.30 +  *         array levels
    1.31 + */
    1.32 +
    1.33 +import com.sun.tools.javac.api.JavacTool;
    1.34 +import java.lang.annotation.*;
    1.35 +import java.io.File;
    1.36 +import java.io.PrintWriter;
    1.37 +import java.util.Arrays;
    1.38 +import java.util.List;
    1.39 +import java.util.Map;
    1.40 +import java.util.HashMap;
    1.41 +import javax.tools.JavaFileManager;
    1.42 +import javax.tools.JavaFileObject;
    1.43 +import com.sun.source.tree.*;
    1.44 +import com.sun.source.util.JavacTask;
    1.45 +import com.sun.source.util.TreeScanner;
    1.46 +import javax.tools.StandardJavaFileManager;
    1.47 +
    1.48 +
    1.49 +public class ArrayPositionConsistency {
    1.50 +    public static void main(String[] args) throws Exception {
    1.51 +        PrintWriter out = new PrintWriter(System.out, true);
    1.52 +        JavacTool tool = JavacTool.create();
    1.53 +        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    1.54 +        File testSrc = new File(System.getProperty("test.src"));
    1.55 +        Iterable<? extends JavaFileObject> f =
    1.56 +            fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayPositionConsistency.java")));
    1.57 +        JavacTask task = tool.getTask(out, fm, null, null, null, f);
    1.58 +        Iterable<? extends CompilationUnitTree> trees = task.parse();
    1.59 +        out.flush();
    1.60 +
    1.61 +        Scanner s = new Scanner();
    1.62 +        for (CompilationUnitTree t: trees)
    1.63 +            s.scan(t, null);
    1.64 +
    1.65 +    }
    1.66 +
    1.67 +    private static class Scanner extends TreeScanner<Void,Void> {
    1.68 +        int foundAnnotations = 0;
    1.69 +        public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) {
    1.70 +            super.visitCompilationUnit(node, ignore);
    1.71 +            if (foundAnnotations != expectedAnnotations) {
    1.72 +                throw new AssertionError("Expected " + expectedAnnotations +
    1.73 +                        " annotations but found: " + foundAnnotations);
    1.74 +            }
    1.75 +            return null;
    1.76 +        }
    1.77 +
    1.78 +        private void testAnnotations(List<? extends AnnotationTree> annos, int found) {
    1.79 +            String annotation = annos.get(0).toString();
    1.80 +            foundAnnotations++;
    1.81 +
    1.82 +            int expected = -1;
    1.83 +            if (annotation.equals("@A()"))
    1.84 +                expected = 0;
    1.85 +            else if (annotation.equals("@B()"))
    1.86 +                expected = 1;
    1.87 +            else if (annotation.equals("@C()"))
    1.88 +                expected = 2;
    1.89 +            else
    1.90 +                throw new AssertionError("found an unexpected annotation: " + annotation);
    1.91 +            if (found != expected) {
    1.92 +                throw new AssertionError("Unexpected found length" +
    1.93 +                    ", found " + found + " but expected " + expected);
    1.94 +            }
    1.95 +        }
    1.96 +
    1.97 +        public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) {
    1.98 +            testAnnotations(node.getAnnotations(), arrayLength(node));
    1.99 +            return super.visitAnnotatedType(node, ignore);
   1.100 +        }
   1.101 +
   1.102 +        private int arrayLength(Tree tree) {
   1.103 +            switch (tree.getKind()) {
   1.104 +            case ARRAY_TYPE:
   1.105 +                return 1 + arrayLength(((ArrayTypeTree)tree).getType());
   1.106 +            case ANNOTATED_TYPE:
   1.107 +                return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType());
   1.108 +            default:
   1.109 +                return 0;
   1.110 +            }
   1.111 +        }
   1.112 +    }
   1.113 +
   1.114 +    static int expectedAnnotations = 23;
   1.115 +
   1.116 +    // visited code
   1.117 +    @A String @C [] @B [] field1;
   1.118 +    @A String @C []    [] field2;
   1.119 +    @A String    [] @B [] field3;
   1.120 +       String    [] @B [] field4;
   1.121 +
   1.122 +    @A List<String> @C [] @B [] genfield1;
   1.123 +    @A List<String> @C []    [] genfield2;
   1.124 +    @A List<String>    [] @B [] genfield3;
   1.125 +       List<String>    [] @B [] genfield4;
   1.126 +
   1.127 +    List<@A String @C [] @B []> typearg1;
   1.128 +    List<@A String @C []    []> typearg2;
   1.129 +    List<@A String    [] @B []> typearg3;
   1.130 +    List<   String    [] @B []> typearg4;
   1.131 +
   1.132 +    void vararg1(@A String @C [] @B ... arg) {}
   1.133 +    void vararg2(@A String @C []    ... arg) {}
   1.134 +    void vararg3(@A String    [] @B ... arg) {}
   1.135 +    void vararg4(   String    [] @B ... arg) {}
   1.136 +
   1.137 +    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
   1.138 +    @interface A {}
   1.139 +    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
   1.140 +    @interface B {}
   1.141 +    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
   1.142 +    @interface C {}
   1.143 +
   1.144 +}

mercurial