test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java

changeset 1709
bae8387d16aa
child 2427
a3ad6e2ede44
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java	Mon Apr 22 10:24:19 2013 +0200
     1.3 @@ -0,0 +1,190 @@
     1.4 +/*
     1.5 + * Copyright (c) 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 + * @bug 8011027
    1.30 + * @library /tools/javac/lib
    1.31 + * @build JavacTestingAbstractProcessor TestTypeParameterAnnotations
    1.32 + * @compile -processor TestTypeParameterAnnotations -proc:only TestTypeParameterAnnotations.java
    1.33 + */
    1.34 +
    1.35 +import java.util.*;
    1.36 +import java.lang.annotation.*;
    1.37 +import javax.annotation.processing.*;
    1.38 +import javax.lang.model.element.*;
    1.39 +import javax.lang.model.util.*;
    1.40 +import javax.tools.*;
    1.41 +
    1.42 +public class TestTypeParameterAnnotations<@Foo @Bar @Baz T> extends JavacTestingAbstractProcessor {
    1.43 +    int round = 0;
    1.44 +
    1.45 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.46 +        if (++round == 1) {
    1.47 +            int found = (new Scanner()).scan(roundEnv.getRootElements(), null);
    1.48 +            if (found == expect) {
    1.49 +                ; //nop
    1.50 +            } else {
    1.51 +                error("unexpected number of results: expected " + expect
    1.52 +                        + ", found " + found);
    1.53 +            }
    1.54 +
    1.55 +        }
    1.56 +        return true;
    1.57 +    }
    1.58 +
    1.59 +    class Scanner extends JavacTestingAbstractProcessor.ElementScanner<Integer,Void> {
    1.60 +        @Override
    1.61 +        public Integer visitExecutable(ExecutableElement e, Void p) {
    1.62 +            super.visitExecutable(e, p);
    1.63 +            found += check(e, e.getTypeParameters());
    1.64 +            return found;
    1.65 +        }
    1.66 +
    1.67 +        @Override
    1.68 +        public Integer visitType(TypeElement e, Void p) {
    1.69 +            super.visitType(e, p);
    1.70 +            found += check(e, e.getTypeParameters());
    1.71 +            return found;
    1.72 +        }
    1.73 +
    1.74 +        int found;
    1.75 +    }
    1.76 +
    1.77 +    int check(Element e, List<? extends TypeParameterElement> typarams) {
    1.78 +        if (typarams.isEmpty())
    1.79 +            return 0;
    1.80 +        if (typarams.size() != 1)
    1.81 +            return 0;
    1.82 +
    1.83 +        for (TypeParameterElement tpe: typarams) {
    1.84 +            boolean b1 = checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors());
    1.85 +            boolean b2 = checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe));
    1.86 +            boolean b3 = checkGetAnnotation(tpe);
    1.87 +            boolean b4 = checkGetAnnotations(tpe);
    1.88 +            return b1 && b2 && b3 && b4 ? 1 : 0;
    1.89 +        }
    1.90 +        return 0;
    1.91 +    }
    1.92 +
    1.93 +    boolean checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l) {
    1.94 +        if (l.size() != 3) {
    1.95 +            error("To few annotations, got " + l.size() +
    1.96 +                    ", should be 3", tpe);
    1.97 +            return false;
    1.98 +        }
    1.99 +
   1.100 +        AnnotationMirror m = l.get(0);
   1.101 +        if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Foo"))) {
   1.102 +            error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement());
   1.103 +            return false;
   1.104 +        }
   1.105 +        m = l.get(1);
   1.106 +        if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Bar"))) {
   1.107 +            error("Wrong type of annotation, was expecting @Bar", m.getAnnotationType().asElement());
   1.108 +            return false;
   1.109 +        }
   1.110 +        m = l.get(2);
   1.111 +        if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Baz"))) {
   1.112 +            error("Wrong type of annotation, was expecting @Baz", m.getAnnotationType().asElement());
   1.113 +            return false;
   1.114 +        }
   1.115 +        return true;
   1.116 +    }
   1.117 +
   1.118 +    boolean checkGetAnnotation(TypeParameterElement tpe) {
   1.119 +        Foo f = tpe.getAnnotation(Foo.class);
   1.120 +        if (f == null)
   1.121 +            error("Expecting @Foo to be present in getAnnotation()", tpe);
   1.122 +
   1.123 +        Bar b = tpe.getAnnotation(Bar.class);
   1.124 +        if (b == null)
   1.125 +            error("Expecting @Bar to be present in getAnnotation()", tpe);
   1.126 +
   1.127 +        Baz z = tpe.getAnnotation(Baz.class);
   1.128 +        if (z == null)
   1.129 +            error("Expecting @Baz to be present in getAnnotation()", tpe);
   1.130 +
   1.131 +        return f != null &&
   1.132 +            b != null &&
   1.133 +            z != null;
   1.134 +    }
   1.135 +
   1.136 +    boolean checkGetAnnotations(TypeParameterElement tpe) {
   1.137 +        Foo[] f = tpe.getAnnotationsByType(Foo.class);
   1.138 +        if (f.length != 1) {
   1.139 +            error("Expecting 1 @Foo to be present in getAnnotationsByType()", tpe);
   1.140 +            return false;
   1.141 +        }
   1.142 +
   1.143 +        Bar[] b = tpe.getAnnotationsByType(Bar.class);
   1.144 +        if (b.length != 1) {
   1.145 +            error("Expecting 1 @Bar to be present in getAnnotationsByType()", tpe);
   1.146 +            return false;
   1.147 +        }
   1.148 +
   1.149 +        Baz[] z = tpe.getAnnotationsByType(Baz.class);
   1.150 +        if (z.length != 1) {
   1.151 +            error("Expecting 1 @Baz to be present in getAnnotationsByType()", tpe);
   1.152 +            return false;
   1.153 +        }
   1.154 +
   1.155 +        return true;
   1.156 +    }
   1.157 +
   1.158 +    void note(String msg) {
   1.159 +        messager.printMessage(Diagnostic.Kind.NOTE, msg);
   1.160 +    }
   1.161 +
   1.162 +    void note(String msg, Element e) {
   1.163 +        messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
   1.164 +    }
   1.165 +
   1.166 +    void error(String msg, Element e) {
   1.167 +        messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
   1.168 +    }
   1.169 +
   1.170 +    void error(String msg) {
   1.171 +        messager.printMessage(Diagnostic.Kind.ERROR, msg);
   1.172 +    }
   1.173 +
   1.174 +    // additional generic elements to test
   1.175 +    <@Foo @Bar @Baz X> X m(X x) { return x; }
   1.176 +
   1.177 +    interface Intf<@Foo @Bar @Baz X> { X m() ; }
   1.178 +
   1.179 +    class Class<@Foo @Bar @Baz X> {
   1.180 +        <@Foo @Bar @Baz Y> Class() { }
   1.181 +    }
   1.182 +
   1.183 +    final int expect = 5;  // top level class, plus preceding examples
   1.184 +}
   1.185 +
   1.186 +@Target(ElementType.TYPE_PARAMETER)
   1.187 +@interface Foo {}
   1.188 +
   1.189 +@Target(ElementType.TYPE_PARAMETER)
   1.190 +@interface Bar {}
   1.191 +
   1.192 +@Target(ElementType.TYPE_PARAMETER)
   1.193 +@interface Baz {}

mercurial