src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java
author darcy
Wed, 25 Sep 2019 13:37:42 -0700
changeset 58344 d29f0181ba42
parent 52404 32408804acd4
permissions -rw-r--r--
8231443: Add java.io.Serial to list of platform annotations for annotation processing Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3151
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3151
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3151
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3151
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3151
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.lang.annotation.Annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
52404
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
    34
import com.sun.tools.javac.code.Source.Feature;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    35
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    36
import com.sun.tools.javac.util.DefinedBy.Api;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    37
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Object providing state about a prior round of annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *
3151
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
    41
 * <p>The methods in this class do not take type annotations into account,
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
    42
 * as target types, not java elements.
41800a86aad3 6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents: 3149
diff changeset
    43
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5841
diff changeset
    44
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
public class JavacRoundEnvironment implements RoundEnvironment {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    // Default equals and hashCode methods are okay.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private final boolean processingOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private final boolean errorRaised;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private final ProcessingEnvironment processingEnv;
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
    55
    private final Elements eltUtils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
52404
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
    57
    private final boolean allowModules;
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
    58
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    // Caller must pass in an immutable set
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private final Set<? extends Element> rootElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    JavacRoundEnvironment(boolean processingOver,
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                          boolean errorRaised,
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
                          Set<? extends Element> rootElements,
52404
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
    65
                          JavacProcessingEnvironment processingEnv) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        this.processingOver = processingOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        this.errorRaised = errorRaised;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        this.rootElements = rootElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        this.processingEnv = processingEnv;
52404
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
    70
        this.allowModules = Feature.MODULES.allowedInSource(processingEnv.source);
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
    71
        this.eltUtils = processingEnv.getElementUtils();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]",
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                             errorRaised,
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                             rootElements,
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                             processingOver);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    81
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    public boolean processingOver() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        return processingOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * Returns {@code true} if an error was raised in the prior round
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * of processing; returns {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * @return {@code true} if an error was raised in the prior round
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * of processing; returns {@code false} otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    93
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    public boolean errorRaised() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        return errorRaised;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Returns the type elements specified by the prior round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * @return the types elements specified by the prior round, or an
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * empty set if there were none
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   104
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public Set<? extends Element> getRootElements() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        return rootElements;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * Returns the elements annotated with the given annotation type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Only type elements <i>included</i> in this round of annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * processing, or declarations of members, parameters, or type
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * parameters declared within those, are returned.  Included type
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 10192
diff changeset
   114
     * elements are {@linkplain #getRootElements specified
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * types} and any types nested within them.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * @param a  annotation type being requested
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * @return the elements annotated with the given annotation type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * or an empty set if there are none
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   121
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   123
        throwIfNotAnnotation(a);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   124
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        Set<Element> result = Collections.emptySet();
25690
b1dac768ab79 8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents: 23795
diff changeset
   126
        ElementScanner9<Set<Element>, TypeElement> scanner =
22441
05b907a2f359 8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents: 22163
diff changeset
   127
            new AnnotationSetScanner(result);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        for (Element element : rootElements)
22441
05b907a2f359 8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents: 22163
diff changeset
   130
            result = scanner.scan(element, a);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   135
    @DefinedBy(Api.ANNOTATION_PROCESSING)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   136
    public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   137
        // Don't bother to special-case annotations.length == 1 as
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   138
        // return getElementsAnnotatedWith(annotations[0]);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   139
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   140
        Set<TypeElement> annotationSet = new LinkedHashSet<>(annotations.length);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   141
        for (TypeElement annotation : annotations) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   142
            throwIfNotAnnotation(annotation);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   143
            annotationSet.add(annotation);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   144
        }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   145
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   146
        Set<Element> result = Collections.emptySet();
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   147
        ElementScanner9<Set<Element>, Set<TypeElement>> scanner =
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   148
            new AnnotationSetMultiScanner(result);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   149
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   150
        for (Element element : rootElements)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   151
            result = scanner.scan(element, annotationSet);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   152
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   153
        return result;
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   154
    }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   155
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    // Could be written as a local class inside getElementsAnnotatedWith
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    private class AnnotationSetScanner extends
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   158
        ElementScanningIncludingTypeParameters<Set<Element>, TypeElement> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        // Insertion-order preserving set
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   160
        private Set<Element> annotatedElements = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
22441
05b907a2f359 8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents: 22163
diff changeset
   162
        AnnotationSetScanner(Set<Element> defaultSet) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            super(defaultSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   166
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   167
        public Set<Element> scan(Element e, TypeElement annotation) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   168
            for (AnnotationMirror annotMirror :  eltUtils.getAllAnnotationMirrors(e)) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   169
                if (annotation.equals(mirrorAsElement(annotMirror))) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   170
                    annotatedElements.add(e);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   171
                    break;
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   172
                }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   173
            }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   174
            e.accept(this, annotation);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   175
            return annotatedElements;
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   176
        }
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   177
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   178
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   179
        public Set<Element> visitModule(ModuleElement e, TypeElement annotation) {
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   180
            // Do not scan a module
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   181
            return annotatedElements;
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   182
        }
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   183
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   184
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   185
        public Set<Element> visitPackage(PackageElement e, TypeElement annotation) {
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   186
            // Do not scan a package
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   187
            return annotatedElements;
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   188
        }
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   189
    }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   190
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   191
    // Could be written as a local class inside getElementsAnnotatedWithAny
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   192
    private class AnnotationSetMultiScanner extends
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   193
        ElementScanningIncludingTypeParameters<Set<Element>, Set<TypeElement>> {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   194
        // Insertion-order preserving set
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   195
        private Set<Element> annotatedElements = new LinkedHashSet<>();
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   196
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   197
        AnnotationSetMultiScanner(Set<Element> defaultSet) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   198
            super(defaultSet);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   199
        }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   200
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   201
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   202
        public Set<Element> scan(Element e, Set<TypeElement> annotations) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   203
            for (AnnotationMirror annotMirror : eltUtils.getAllAnnotationMirrors(e)) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   204
                if (annotations.contains(mirrorAsElement(annotMirror))) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   205
                    annotatedElements.add(e);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   206
                    break;
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   207
                }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   208
            }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   209
            e.accept(this, annotations);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   210
            return annotatedElements;
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   211
        }
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   212
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   213
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   214
        public Set<Element> visitModule(ModuleElement e, Set<TypeElement> annotations) {
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   215
            // Do not scan a module
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   216
            return annotatedElements;
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   217
        }
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   218
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   219
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   220
        public Set<Element> visitPackage(PackageElement e, Set<TypeElement> annotations) {
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   221
            // Do not scan a package
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   222
            return annotatedElements;
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   223
        }
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   224
    }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   225
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   226
    private static abstract class ElementScanningIncludingTypeParameters<R, P>
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   227
        extends ElementScanner9<R, P> {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   228
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   229
        protected ElementScanningIncludingTypeParameters(R defaultValue) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   230
            super(defaultValue);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   231
        }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   232
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   233
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   234
        public R visitType(TypeElement e, P p) {
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   235
            // Type parameters are not considered to be enclosed by a type
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   236
            scan(e.getTypeParameters(), p);
23795
62509b72088e 8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents: 22449
diff changeset
   237
            return super.visitType(e, p);
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   238
        }
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   239
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   240
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   241
        public R visitExecutable(ExecutableElement e, P p) {
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   242
            // Type parameters are not considered to be enclosed by an executable
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   243
            scan(e.getTypeParameters(), p);
23795
62509b72088e 8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents: 22449
diff changeset
   244
            return super.visitExecutable(e, p);
18669
99572d59c916 7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents: 14259
diff changeset
   245
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    /**
31751
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 26266
diff changeset
   249
     * {@inheritDoc}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   251
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   253
        throwIfNotAnnotation(a);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        String name = a.getCanonicalName();
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   255
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        if (name == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        else {
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   259
            TypeElement annotationType = annotationToElement(a);
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   260
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            if (annotationType == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                return getElementsAnnotatedWith(annotationType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    }
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   267
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   268
    @DefinedBy(Api.ANNOTATION_PROCESSING)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   269
    public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   270
        List<TypeElement> annotationsAsElements = new ArrayList<>(annotations.size());
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   271
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   272
        for (Class<? extends Annotation> annotation : annotations) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   273
            throwIfNotAnnotation(annotation);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   274
            String name = annotation.getCanonicalName();
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   275
            if (name == null)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   276
                continue;
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   277
            annotationsAsElements.add(annotationToElement(annotation));
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   278
        }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   279
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   280
        return getElementsAnnotatedWithAny(annotationsAsElements.toArray(new TypeElement[0]));
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   281
    }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   282
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   283
    private TypeElement annotationToElement(Class<? extends Annotation> annotation) {
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   284
        // First, try an element lookup based on the annotation's
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   285
        // canonical name. If that fails or is ambiguous, try a lookup
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   286
        // using a particular module, perhaps an unnamed one. This
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   287
        // offers more compatibility for compiling in single-module
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   288
        // mode where the runtime module of an annotation type may
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   289
        // differ from the single module being compiled.
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   290
        String name = annotation.getCanonicalName();
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   291
        TypeElement annotationElement = eltUtils.getTypeElement(name);
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   292
        if (annotationElement != null)
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   293
            return annotationElement;
52404
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
   294
        else if (allowModules) {
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   295
            String moduleName = Objects.requireNonNullElse(annotation.getModule().getName(), "");
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   296
            return eltUtils.getTypeElement(eltUtils.getModuleElement(moduleName), name);
52404
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
   297
        } else {
32408804acd4 8213103: RoundEnvironment.getElementsAnnotatedWith(Class) crashes with -source 8
jlahoda
parents: 51194
diff changeset
   298
            return null;
51194
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   299
        }
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   300
    }
766df90d6fb5 8190886: package-info handling in RoundEnvironment.getElementsAnnotatedWith
darcy
parents: 47216
diff changeset
   301
38606
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   302
    private Element mirrorAsElement(AnnotationMirror annotationMirror) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   303
        return annotationMirror.getAnnotationType().asElement();
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   304
    }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   305
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   306
    private static final String NOT_AN_ANNOTATION_TYPE =
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   307
        "The argument does not represent an annotation type: ";
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   308
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   309
    private void throwIfNotAnnotation(Class<? extends Annotation> a) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   310
        if (!a.isAnnotation())
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   311
            throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   312
    }
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   313
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   314
    private void throwIfNotAnnotation(TypeElement a) {
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   315
        if (a.getKind() != ElementKind.ANNOTATION_TYPE)
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   316
            throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
6b708cd90241 8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents: 31751
diff changeset
   317
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
}