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