test/tools/javac/processing/model/inheritedByType/EnsureOrder.java

Thu, 25 Jul 2013 11:02:27 +0200

author
jfranck
date
Thu, 25 Jul 2013 11:02:27 +0200
changeset 1918
a218f7befd55
parent 0
959103a6100f
permissions
-rw-r--r--

8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 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 test that order is respected when inheriting both legacy container and single anno
    27  * @bug 8007961
    28  * @library /tools/javac/lib
    29  * @build   JavacTestingAbstractProcessor EnsureOrder
    30  * @compile -processor EnsureOrder -proc:only EnsureOrder.java
    31  */
    33 import java.util.Set;
    34 import java.lang.annotation.*;
    35 import javax.annotation.processing.*;
    36 import javax.lang.model.SourceVersion;
    37 import static javax.lang.model.SourceVersion.*;
    38 import javax.lang.model.element.*;
    39 import javax.lang.model.util.*;
    40 import static javax.lang.model.util.ElementFilter.*;
    41 import static javax.tools.Diagnostic.Kind.*;
    42 import static javax.tools.StandardLocation.*;
    43 import com.sun.tools.javac.util.Assert;
    45 @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE})
    46 @Inherited
    47 @Retention(RetentionPolicy.RUNTIME)
    48 @Repeatable(Foos.class)
    49 @interface Foo {
    50     int value();
    51 }
    53 @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE})
    54 @Inherited
    55 @Retention(RetentionPolicy.RUNTIME)
    56 @interface Foos {
    57     Foo[] value();
    58 }
    60 @Foos({@Foo(0), @Foo(1)}) @Foo(2)
    61 class Base {}
    63 class Sub extends Base {}
    65 public class EnsureOrder<@Foos({@Foo(0), @Foo(1)}) @Foo(2)T> extends JavacTestingAbstractProcessor {
    66     public boolean process(Set<? extends TypeElement> annotations,
    67                            RoundEnvironment roundEnv) {
    68         if (!roundEnv.processingOver()) {
    69             int hasRun = 0;
    70             for (Element element : roundEnv.getRootElements()) {
    71                 Name elemName = element.getSimpleName();
    72                 if (elemName.contentEquals("Base")) {
    73                     hasRun++;
    74                     Foo[] foos = element.getAnnotationsByType(Foo.class);
    75                     Assert.check(foos.length == 3);
    76                     Assert.check(foos[0].value() == 0);
    77                     Assert.check(foos[1].value() == 1);
    78                     Assert.check(foos[2].value() == 2);
    79                 }
    80                 if (elemName.contentEquals("Sub")) {
    81                     hasRun++;
    82                     Foo[] foos = element.getAnnotationsByType(Foo.class);
    83                     Assert.check(foos.length == 3);
    84                     Assert.check(foos[0].value() == 0);
    85                     Assert.check(foos[1].value() == 1);
    86                     Assert.check(foos[2].value() == 2);
    87                 }
    88                 if (elemName.contentEquals("EnsureOrder")) {
    89                     for (TypeParameterElement t : ((TypeElement)element).getTypeParameters()) {
    90                         if (t.getSimpleName().contentEquals("T")) {
    91                             hasRun++;
    92                             Foo[] foos = t.getAnnotationsByType(Foo.class);
    93                             Assert.check(foos.length == 3);
    94                             Assert.check(foos[0].value() == 0);
    95                             Assert.check(foos[1].value() == 1);
    96                             Assert.check(foos[2].value() == 2);
    97                         }
    98                     }
    99                 }
   100             }
   101             if (hasRun != 3)
   102                 throw new RuntimeException("Couldn't find elements");
   103         }
   104         return true;
   105     }
   106 }

mercurial