langtools/src/share/classes/com/sun/tools/javac/comp/Annotate.java
author emc
Thu, 03 Apr 2014 20:28:23 -0400
changeset 23814 06ab27895804
parent 23397 825c76b83afe
child 24895 dd091d389fbf
permissions -rw-r--r--
8038263: Refactor annotation handling after actualEnterAnnotations Summary: Move all repeating annotations code into Annotate, rework annotations pipeline into a more completer-like design, eliminate a cast from enterAnnotations/enterTypeAnnotations Reviewed-by: jjg, jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
     2
 * Copyright (c) 2003, 2014, 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: 1264
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: 1264
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: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
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.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    28
import java.util.HashMap;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    29
import java.util.LinkedHashMap;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    30
import java.util.Map;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    31
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    32
import javax.tools.JavaFileObject;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    33
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.*;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    35
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    41
import static com.sun.tools.javac.code.Kinds.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
    42
import static com.sun.tools.javac.code.TypeTag.ARRAY;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
    43
import static com.sun.tools.javac.code.TypeTag.CLASS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    44
import static com.sun.tools.javac.tree.JCTree.Tag.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    45
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
/** Enter annotations on symbols.  Annotations accumulate in a queue,
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  which is processed at the top level of any set of recursive calls
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  requesting it be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    50
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    51
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
public class Annotate {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
    56
    protected static final Context.Key<Annotate> annotateKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public static Annotate instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        Annotate instance = context.get(annotateKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            instance = new Annotate(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    65
    private final Attr attr;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    66
    private final TreeMaker make;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    67
    private final Log log;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    68
    private final Symtab syms;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    69
    private final Names names;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    70
    private final Resolve rs;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    71
    private final Types types;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    72
    private final ConstFold cfolder;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    73
    private final Check chk;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    74
    private final Lint lint;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    75
    private final DeferredLintHandler deferredLintHandler;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    76
    private final Source source;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    77
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    78
    private boolean allowTypeAnnos;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    79
    private boolean allowRepeatedAnnos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    protected Annotate(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        context.put(annotateKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        syms = Symtab.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    87
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        chk = Check.instance(context);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    92
        source = Source.instance(context);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    93
        lint = Lint.instance(context);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    94
        deferredLintHandler = DeferredLintHandler.instance(context);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    95
        allowRepeatedAnnos = source.allowRepeatedAnnotations();
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
    96
        allowTypeAnnos = source.allowTypeAnnotations();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
 * Queue maintenance
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    private int enterCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   105
    ListBuffer<Worker> q = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   106
    ListBuffer<Worker> typesQ = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   107
    ListBuffer<Worker> repeatedQ = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   108
    ListBuffer<Worker> afterRepeatedQ = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   109
    ListBuffer<Worker> validateQ = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   110
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   111
    public void earlier(Worker a) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   112
        q.prepend(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   113
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   115
    public void normal(Worker a) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        q.append(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   119
    public void typeAnnotation(Worker a) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   120
        typesQ.append(a);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   123
    public void repeated(Worker a) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   124
        repeatedQ.append(a);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   125
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   126
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   127
    public void afterRepeated(Worker a) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   128
        afterRepeatedQ.append(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   129
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   130
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   131
    public void validate(Worker a) {
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21015
diff changeset
   132
        validateQ.append(a);
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21015
diff changeset
   133
    }
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21015
diff changeset
   134
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /** Called when the Enter phase starts. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public void enterStart() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    /** Called after the Enter phase completes. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    public void enterDone() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
21043
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
   146
    /** Variant which allows for a delayed flush of annotations.
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
   147
     * Needed by ClassReader */
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
   148
    public void enterDoneWithoutFlush() {
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
   149
        enterCount--;
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
   150
    }
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
   151
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        if (enterCount != 0) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        try {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   156
            while (q.nonEmpty()) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   157
                q.next().run();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   158
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   159
            while (typesQ.nonEmpty()) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   160
                typesQ.next().run();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   161
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   162
            while (repeatedQ.nonEmpty()) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   163
                repeatedQ.next().run();
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   164
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   165
            while (afterRepeatedQ.nonEmpty()) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   166
                afterRepeatedQ.next().run();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   167
            }
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21015
diff changeset
   168
            while (validateQ.nonEmpty()) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   169
                validateQ.next().run();
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21015
diff changeset
   170
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   176
    /** A client that needs to run during {@link #flush()} registers an worker
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   177
     *  into one of the queues defined in this class. The queues are: {@link #earlier(Worker)},
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   178
     *  {@link #normal(Worker)}, {@link #typeAnnotation(Worker)}, {@link #repeated(Worker)},
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   179
     *  {@link #afterRepeated(Worker)}, {@link #validate(Worker)}.
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   180
     *  The {@link Worker#run()} method will called inside the {@link #flush()}
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   181
     *  call. Queues are empties in the abovementioned order.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   183
    public interface Worker {
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   184
        void run();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        String toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   188
    /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   189
     * This context contains all the information needed to synthesize new
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   190
     * annotations trees by the completer for repeating annotations.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   191
     */
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   192
    private class AnnotationContext<T extends Attribute.Compound> {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   193
        public final Env<AttrContext> env;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   194
        public final Map<Symbol.TypeSymbol, ListBuffer<T>> annotated;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   195
        public final Map<T, JCDiagnostic.DiagnosticPosition> pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   196
        public final boolean isTypeCompound;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   197
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   198
        public AnnotationContext(Env<AttrContext> env,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   199
                                 Map<Symbol.TypeSymbol, ListBuffer<T>> annotated,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   200
                                 Map<T, JCDiagnostic.DiagnosticPosition> pos,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   201
                                 boolean isTypeCompound) {
13840
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   202
            Assert.checkNonNull(env);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   203
            Assert.checkNonNull(annotated);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   204
            Assert.checkNonNull(pos);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   205
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   206
            this.env = env;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   207
            this.annotated = annotated;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   208
            this.pos = pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   209
            this.isTypeCompound = isTypeCompound;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   210
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   211
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   212
        public String toString() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   213
            StringBuilder sb = new StringBuilder();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   214
            sb.append("RepeatedContext[");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   215
            for (Map.Entry<Symbol.TypeSymbol, ListBuffer<T>> entry :
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   216
                     annotated.entrySet()) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   217
                sb.append(" ");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   218
                sb.append(entry.getKey());
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   219
                sb.append(" = { ");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   220
                sb.append(entry.getValue());
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   221
                sb.append(" }");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   222
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   223
            sb.append(" ]");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   224
            return sb.toString();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   225
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   226
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   227
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   228
    private static class Placeholder<T extends Attribute.Compound> extends Attribute.Compound {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   229
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   230
        private final Annotate.AnnotationContext<T> ctx;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   231
        private final List<T> placeholderFor;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   232
        private final Symbol on;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   233
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   234
        public Placeholder(Annotate.AnnotationContext<T> ctx,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   235
                           List<T> placeholderFor, Symbol on) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   236
            super(on.type, List.<Pair<Symbol.MethodSymbol, Attribute>>nil(),
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   237
                  placeholderFor.head.position);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   238
            this.ctx = ctx;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   239
            this.placeholderFor = placeholderFor;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   240
            this.on = on;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   241
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   242
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   243
        @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   244
        public String toString() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   245
            return "<placeholder: " + placeholderFor + " on: " + on + ">";
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   246
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   247
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   248
        public List<T> getPlaceholderFor() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   249
            return placeholderFor;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   250
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   251
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   252
        public Annotate.AnnotationContext<T> getRepeatedContext() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   253
            return ctx;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   254
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   255
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   257
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
 * Compute an attribute from its annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    /** Process a single compound annotation, returning its
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     *  Attribute. Used from MemberEnter for attaching the attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     *  to the annotated symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    Attribute.Compound enterAnnotation(JCAnnotation a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                                       Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                                       Env<AttrContext> env) {
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   269
        List<Pair<MethodSymbol,Attribute>> elems =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   270
            enterAttributeValues(a, expected, env);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   271
        Attribute.Compound ac = new Attribute.Compound(a.type, elems);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   272
        a.attribute = ac;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   273
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   274
        return ac;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   275
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   276
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   277
    Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a,
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   278
                                               Type expected,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   279
                                               Env<AttrContext> env) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   280
        List<Pair<MethodSymbol,Attribute>> elems =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   281
            enterAttributeValues(a, expected, env);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   282
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   283
        if (a.attribute == null || !(a.attribute instanceof Attribute.TypeCompound)) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   284
            // Create a new TypeCompound
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   285
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   286
            Attribute.TypeCompound tc =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   287
                new Attribute.TypeCompound(a.type, elems,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   288
                // TODO: Eventually, we will get rid of this use of
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   289
                // unknown, because we'll get a position from
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   290
                // MemberEnter (task 8027262).
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   291
                                           TypeAnnotationPosition.unknown);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   292
            a.attribute = tc;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   293
            return tc;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   294
        } else {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   295
            // Use an existing TypeCompound
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   296
            return (Attribute.TypeCompound)a.attribute;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   297
        }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   298
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16342
diff changeset
   299
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   300
    private List<Pair<MethodSymbol,Attribute>>
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   301
            enterAttributeValues(JCAnnotation a,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   302
                                 Type expected,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   303
                                 Env<AttrContext> env) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   304
        // The annotation might have had its type attributed (but not
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   305
        // checked) by attr.attribAnnotationTypes during MemberEnter,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   306
        // in which case we do not need to do it again.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        Type at = (a.annotationType.type != null ? a.annotationType.type
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                  : attr.attribType(a.annotationType, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        a.type = chk.checkType(a.annotationType.pos(), at, expected);
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   310
        boolean isError = a.type.isErroneous();
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   311
        if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0 && !isError) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            log.error(a.annotationType.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                      "not.annotation.type", a.type.toString());
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   314
            isError = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        List<JCExpression> args = a.args;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   317
        if (args.length() == 1 && !args.head.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            // special case: elided "value=" assumed
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            args.head = make.at(args.head.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                Assign(make.Ident(names.value), args.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        ListBuffer<Pair<MethodSymbol,Attribute>> buf =
21713
b3fcc9c0fea3 8027375: javac asserts on nested erroneous annotations
jfranck
parents: 21043
diff changeset
   323
            new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            JCExpression t = tl.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   326
            if (!t.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                log.error(t.pos(), "annotation.value.must.be.name.value");
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   328
                enterAttributeValue(t.type = syms.errType, t, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            JCAssign assign = (JCAssign)t;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   332
            if (!assign.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                log.error(t.pos(), "annotation.value.must.be.name.value");
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   334
                enterAttributeValue(t.type = syms.errType, t, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            JCIdent left = (JCIdent)assign.lhs;
19122
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 17578
diff changeset
   338
            Symbol method = rs.resolveQualifiedMethod(assign.rhs.pos(),
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   339
                                                          env,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   340
                                                          a.type,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   341
                                                          left.name,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   342
                                                          List.<Type>nil(),
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   343
                                                          null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            left.sym = method;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            left.type = method.type;
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   346
            if (method.owner != a.type.tsym && !isError)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                log.error(left.pos(), "no.annotation.member", left.name, a.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            Type result = method.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            Attribute value = enterAttributeValue(result, assign.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            if (!method.type.isErroneous())
21713
b3fcc9c0fea3 8027375: javac asserts on nested erroneous annotations
jfranck
parents: 21043
diff changeset
   351
                buf.append(new Pair<>((MethodSymbol)method, value));
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   352
            t.type = result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        }
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   354
        return buf.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    Attribute enterAttributeValue(Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                                  JCExpression tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                                  Env<AttrContext> env) {
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   360
        //first, try completing the attribution value sym - if a completion
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   361
        //error is thrown, we should recover gracefully, and display an
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   362
        //ordinary resolution diagnostic.
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   363
        try {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   364
            expected.tsym.complete();
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   365
        } catch(CompletionFailure e) {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   366
            log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   367
            expected = syms.errType;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   368
        }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   369
        if (expected.hasTag(ARRAY)) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   370
            if (!tree.hasTag(NEWARRAY)) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   371
                tree = make.at(tree.pos).
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   372
                    NewArray(null, List.<JCExpression>nil(), List.of(tree));
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   373
            }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   374
            JCNewArray na = (JCNewArray)tree;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   375
            if (na.elemtype != null) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   376
                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   377
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   378
            ListBuffer<Attribute> buf = new ListBuffer<>();
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   379
            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   380
                buf.append(enterAttributeValue(types.elemtype(expected),
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   381
                                               l.head,
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   382
                                               env));
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   383
            }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   384
            na.type = expected;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   385
            return new Attribute.
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   386
                Array(expected, buf.toArray(new Attribute[buf.length()]));
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   387
        }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   388
        if (tree.hasTag(NEWARRAY)) { //error recovery
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   389
            if (!expected.isErroneous())
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   390
                log.error(tree.pos(), "annotation.value.not.allowable.type");
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   391
            JCNewArray na = (JCNewArray)tree;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   392
            if (na.elemtype != null) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   393
                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   394
            }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   395
            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   396
                enterAttributeValue(syms.errType,
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   397
                                    l.head,
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   398
                                    env);
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   399
            }
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   400
            return new Attribute.Error(syms.errType);
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   401
        }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   402
        if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   403
            if (tree.hasTag(ANNOTATION)) {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   404
                return enterAnnotation((JCAnnotation)tree, expected, env);
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   405
            } else {
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   406
                log.error(tree.pos(), "annotation.value.must.be.annotation");
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   407
                expected = syms.errType;
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   408
            }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   409
        }
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   410
        if (tree.hasTag(ANNOTATION)) { //error recovery
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   411
            if (!expected.isErroneous())
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   412
                log.error(tree.pos(), "annotation.not.valid.for.type", expected);
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   413
            enterAnnotation((JCAnnotation)tree, syms.errType, env);
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   414
            return new Attribute.Error(((JCAnnotation)tree).annotationType.type);
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   415
        }
22439
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   416
        if (expected.isPrimitive() ||
4785d87cd2d6 8029376: Full attribution of unresolvable annotations
jlahoda
parents: 22165
diff changeset
   417
            (types.isSameType(expected, syms.stringType) && !expected.hasTag(TypeTag.ERROR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            Type result = attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            if (result.isErroneous())
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   420
                return new Attribute.Error(result.getOriginalType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            if (result.constValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                log.error(tree.pos(), "attribute.value.must.be.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            result = cfolder.coerce(result, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            return new Attribute.Constant(expected, result.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        if (expected.tsym == syms.classType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            Type result = attr.attribExpr(tree, env, expected);
19507
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   430
            if (result.isErroneous()) {
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   431
                // Does it look like an unresolved class literal?
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   432
                if (TreeInfo.name(tree) == names._class &&
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   433
                    ((JCFieldAccess) tree).selected.type.isErroneous()) {
19507
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   434
                    Name n = (((JCFieldAccess) tree).selected).type.tsym.flatName();
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   435
                    return new Attribute.UnresolvedClass(expected,
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   436
                            types.createErrorType(n,
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   437
                                    syms.unknownSymbol, syms.classType));
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   438
                } else {
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   439
                    return new Attribute.Error(result.getOriginalType());
19507
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   440
                }
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   441
            }
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   442
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   443
            // Class literals look like field accesses of a field named class
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 19122
diff changeset
   444
            // at the tree level
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            if (TreeInfo.name(tree) != names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                log.error(tree.pos(), "annotation.value.must.be.class.literal");
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   447
                return new Attribute.Error(syms.errType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            return new Attribute.Class(types,
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                                       (((JCFieldAccess) tree).selected).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        }
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14054
diff changeset
   452
        if (expected.hasTag(CLASS) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            (expected.tsym.flags() & Flags.ENUM) != 0) {
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   454
            Type result = attr.attribExpr(tree, env, expected);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            Symbol sym = TreeInfo.symbol(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                TreeInfo.nonstaticSelect(tree) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                sym.kind != Kinds.VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                (sym.flags() & Flags.ENUM) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
21015
f3bec12a63e7 6278240: Exception from AnnotationValue.getValue() should list the found type not the required type
jlahoda
parents: 20599
diff changeset
   461
                return new Attribute.Error(result.getOriginalType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            VarSymbol enumerator = (VarSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            return new Attribute.Enum(expected, enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        }
20599
5201f7144a3c 8022765: Compiler crashes with exception on wrong usage of an annotation.
jlahoda
parents: 19507
diff changeset
   466
        //error recovery:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        if (!expected.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            log.error(tree.pos(), "annotation.value.not.allowable.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        return new Attribute.Error(attr.attribExpr(tree, env, expected));
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   471
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   472
    /* *********************************
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   473
     * Support for repeating annotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   474
     ***********************************/
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   475
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   476
    /* Process repeated annotations. This method returns the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   477
     * synthesized container annotation or null IFF all repeating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   478
     * annotation are invalid.  This method reports errors/warnings.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   479
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   480
    private <T extends Attribute.Compound> T processRepeatedAnnotations(List<T> annotations,
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   481
            AnnotationContext<T> ctx,
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   482
            Symbol on) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   483
        T firstOccurrence = annotations.head;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   484
        List<Attribute> repeated = List.nil();
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   485
        Type origAnnoType = null;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   486
        Type arrayOfOrigAnnoType = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   487
        Type targetContainerType = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   488
        MethodSymbol containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   489
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   490
        Assert.check(!annotations.isEmpty() &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   491
                     !annotations.tail.isEmpty()); // i.e. size() > 1
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   492
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   493
        int count = 0;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   494
        for (List<T> al = annotations;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   495
             !al.isEmpty();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   496
             al = al.tail)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   497
        {
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   498
            count++;
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   499
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   500
            // There must be more than a single anno in the annotation list
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   501
            Assert.check(count > 1 || !al.tail.isEmpty());
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   502
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   503
            T currentAnno = al.head;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   504
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   505
            origAnnoType = currentAnno.type;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   506
            if (arrayOfOrigAnnoType == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   507
                arrayOfOrigAnnoType = types.makeArrayType(origAnnoType);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   508
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   509
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   510
            // Only report errors if this isn't the first occurrence I.E. count > 1
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   511
            boolean reportError = count > 1;
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   512
            Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno), reportError);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   513
            if (currentContainerType == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   514
                continue;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   515
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   516
            // Assert that the target Container is == for all repeated
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   517
            // annos of the same annotation type, the types should
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   518
            // come from the same Symbol, i.e. be '=='
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   519
            Assert.check(targetContainerType == null || currentContainerType == targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   520
            targetContainerType = currentContainerType;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   521
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   522
            containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   523
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   524
            if (containerValueSymbol == null) { // Check of CA type failed
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   525
                // errors are already reported
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   526
                continue;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   527
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   528
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   529
            repeated = repeated.prepend(currentAnno);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   530
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   531
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   532
        if (!repeated.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   533
            repeated = repeated.reverse();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   534
            TreeMaker m = make.at(ctx.pos.get(firstOccurrence));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   535
            Pair<MethodSymbol, Attribute> p =
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   536
                    new Pair<MethodSymbol, Attribute>(containerValueSymbol,
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21713
diff changeset
   537
                               new Attribute.Array(arrayOfOrigAnnoType, repeated));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   538
            if (ctx.isTypeCompound) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   539
                /* TODO: the following code would be cleaner:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   540
                Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   541
                        ((Attribute.TypeCompound)annotations.head).position);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   542
                JCTypeAnnotation annoTree = m.TypeAnnotation(at);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   543
                at = enterTypeAnnotation(annoTree, targetContainerType, ctx.env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   544
                */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   545
                // However, we directly construct the TypeCompound to keep the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   546
                // direct relation to the contained TypeCompounds.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   547
                Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   548
                        ((Attribute.TypeCompound)annotations.head).position);
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   549
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   550
                // TODO: annotation applicability checks from below?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   551
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   552
                at.setSynthesized(true);
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   553
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   554
                @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   555
                T x = (T) at;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   556
                return x;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   557
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   558
                Attribute.Compound c = new Attribute.Compound(targetContainerType, List.of(p));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   559
                JCAnnotation annoTree = m.Annotation(c);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   560
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   561
                if (!chk.annotationApplicable(annoTree, on))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   562
                    log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType);
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14359
diff changeset
   563
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   564
                if (!chk.validateAnnotationDeferErrors(annoTree))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   565
                    log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   566
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   567
                c = enterAnnotation(annoTree, targetContainerType, ctx.env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   568
                c.setSynthesized(true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   569
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   570
                @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   571
                T x = (T) c;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   572
                return x;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   573
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   574
        } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   575
            return null; // errors should have been reported elsewhere
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   576
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   577
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   578
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   579
    /** Fetches the actual Type that should be the containing annotation. */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   580
    private Type getContainingType(Attribute.Compound currentAnno,
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   581
            DiagnosticPosition pos,
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   582
            boolean reportError)
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   583
    {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   584
        Type origAnnoType = currentAnno.type;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   585
        TypeSymbol origAnnoDecl = origAnnoType.tsym;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   586
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   587
        // Fetch the Repeatable annotation from the current
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   588
        // annotation's declaration, or null if it has none
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   589
        Attribute.Compound ca = origAnnoDecl.attribute(syms.repeatableType.tsym);
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   590
        if (ca == null) { // has no Repeatable annotation
16342
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   591
            if (reportError)
28d6ae9ed67c 7196531: Duplicate error messages on repeating annotations
jfranck
parents: 15385
diff changeset
   592
                log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.repeatableType);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   593
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   594
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   595
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   596
        return filterSame(extractContainingType(ca, pos, origAnnoDecl),
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   597
                          origAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   598
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   599
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   600
    // returns null if t is same as 's', returns 't' otherwise
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   601
    private Type filterSame(Type t, Type s) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   602
        if (t == null || s == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   603
            return t;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   604
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   605
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   606
        return types.isSameType(t, s) ? null : t;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   607
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   608
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   609
    /** Extract the actual Type to be used for a containing annotation. */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   610
    private Type extractContainingType(Attribute.Compound ca,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   611
            DiagnosticPosition pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   612
            TypeSymbol annoDecl)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   613
    {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   614
        // The next three checks check that the Repeatable annotation
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   615
        // on the declaration of the annotation type that is repeating is
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   616
        // valid.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   617
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   618
        // Repeatable must have at least one element
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   619
        if (ca.values.isEmpty()) {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   620
            log.error(pos, "invalid.repeatable.annotation", annoDecl);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   621
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   622
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   623
        Pair<MethodSymbol,Attribute> p = ca.values.head;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   624
        Name name = p.fst.name;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   625
        if (name != names.value) { // should contain only one element, named "value"
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   626
            log.error(pos, "invalid.repeatable.annotation", annoDecl);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   627
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   628
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   629
        if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   630
            log.error(pos, "invalid.repeatable.annotation", annoDecl);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   631
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   632
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   633
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   634
        return ((Attribute.Class)p.snd).getValue();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   635
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   636
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   637
    /* Validate that the suggested targetContainerType Type is a valid
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   638
     * container type for repeated instances of originalAnnoType
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   639
     * annotations. Return null and report errors if this is not the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   640
     * case, return the MethodSymbol of the value element in
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   641
     * targetContainerType if it is suitable (this is needed to
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   642
     * synthesize the container). */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   643
    private MethodSymbol validateContainer(Type targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   644
                                           Type originalAnnoType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   645
                                           DiagnosticPosition pos) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   646
        MethodSymbol containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   647
        boolean fatalError = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   648
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   649
        // Validate that there is a (and only 1) value method
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   650
        Scope scope = targetContainerType.tsym.members();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   651
        int nr_value_elems = 0;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   652
        boolean error = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   653
        for(Symbol elm : scope.getElementsByName(names.value)) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   654
            nr_value_elems++;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   655
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   656
            if (nr_value_elems == 1 &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   657
                elm.kind == Kinds.MTH) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   658
                containerValueSymbol = (MethodSymbol)elm;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   659
            } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   660
                error = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   661
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   662
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   663
        if (error) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   664
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   665
                      "invalid.repeatable.annotation.multiple.values",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   666
                      targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   667
                      nr_value_elems);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   668
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   669
        } else if (nr_value_elems == 0) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   670
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   671
                      "invalid.repeatable.annotation.no.value",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   672
                      targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   673
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   674
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   675
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   676
        // validate that the 'value' element is a method
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   677
        // probably "impossible" to fail this
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   678
        if (containerValueSymbol.kind != Kinds.MTH) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   679
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   680
                      "invalid.repeatable.annotation.invalid.value",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   681
                      targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   682
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   683
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   684
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   685
        // validate that the 'value' element has the correct return type
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   686
        // i.e. array of original anno
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   687
        Type valueRetType = containerValueSymbol.type.getReturnType();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   688
        Type expectedType = types.makeArrayType(originalAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   689
        if (!(types.isArray(valueRetType) &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   690
              types.isSameType(expectedType, valueRetType))) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   691
            log.error(pos,
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   692
                      "invalid.repeatable.annotation.value.return",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   693
                      targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   694
                      valueRetType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   695
                      expectedType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   696
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   697
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   698
        if (error) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   699
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   700
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   701
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14961
diff changeset
   702
        // The conditions for a valid containing annotation are made
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   703
        // in Check.validateRepeatedAnnotaton();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   704
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   705
        return fatalError ? null : containerValueSymbol;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   706
    }
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   707
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   708
    private <T extends Attribute.Compound> AnnotationContext<T>
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   709
            prepareEnterAnnotations(List<JCAnnotation> annotations,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   710
                                    Env<AttrContext> env,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   711
                                    Symbol sym,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   712
                                    AttributeCreator<T> creator,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   713
                                    boolean isTypeCompound) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   714
        Map<TypeSymbol, ListBuffer<T>> annotated = new LinkedHashMap<>();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   715
        Map<T, DiagnosticPosition> pos = new HashMap<>();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   716
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   717
        for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   718
            JCAnnotation a = al.head;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   719
            T c = creator.create(a, syms.annotationType, env);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   720
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   721
            Assert.checkNonNull(c, "Failed to create annotation");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   722
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   723
            if (annotated.containsKey(a.type.tsym)) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   724
                if (!allowRepeatedAnnos) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   725
                    log.error(a.pos(), "repeatable.annotations.not.supported.in.source");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   726
                    allowRepeatedAnnos = true;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   727
                }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   728
                ListBuffer<T> l = annotated.get(a.type.tsym);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   729
                l = l.append(c);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   730
                annotated.put(a.type.tsym, l);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   731
                pos.put(c, a.pos());
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   732
            } else {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   733
                annotated.put(a.type.tsym, ListBuffer.of(c));
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   734
                pos.put(c, a.pos());
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   735
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   736
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   737
            // Note: @Deprecated has no effect on local variables and parameters
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   738
            if (!c.type.isErroneous()
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   739
                && sym.owner.kind != MTH
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   740
                && types.isSameType(c.type, syms.deprecatedType)) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   741
                sym.flags_field |= Flags.DEPRECATED;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   742
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   743
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   744
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   745
        return new AnnotationContext<>(env, annotated, pos,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   746
                                             isTypeCompound);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   747
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   748
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   749
    // Gather up annotations into a map from type symbols to lists of
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   750
    // Compound attributes, then continue on with repeating
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   751
    // annotations processing
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   752
    private <T extends Attribute.Compound>
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   753
            void attachAttributesLater(final List<JCAnnotation> annotations,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   754
                                       final Env<AttrContext> env,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   755
                                       final Symbol sym,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   756
                                       final boolean isTypeCompound,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   757
                                       final AttributeCreator<T> creator,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   758
                                       final AttributeAttacher<T> attacher) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   759
        final AnnotationContext<T> ctx =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   760
            prepareEnterAnnotations(annotations, env, sym, creator, isTypeCompound);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   761
        final Map<Symbol.TypeSymbol, ListBuffer<T>> annotated =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   762
            ctx.annotated;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   763
        boolean hasRepeated = false;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   764
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   765
        List<T> buf = List.<T>nil();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   766
        for (ListBuffer<T> lb : annotated.values()) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   767
            if (lb.size() == 1) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   768
                buf = buf.prepend(lb.first());
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   769
            } else {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   770
                @SuppressWarnings("unchecked")
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   771
                T res = (T) new Placeholder<>(ctx, lb.toList(), sym);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   772
                buf = buf.prepend(res);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   773
                hasRepeated = true;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   774
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   775
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   776
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   777
        final List<T> attrs = buf.reverse();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   778
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   779
        if (!isTypeCompound) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   780
            // Attach declaration attributes early, so
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   781
            // that @Repeatable and other annotations get attached.
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   782
            // Since the attacher uses setDeclarationAttributes, this
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   783
            // will be overwritten later.
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   784
            attacher.attach(sym, attrs);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   785
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   786
        if (hasRepeated) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   787
            repeated(new Annotate.Worker() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   788
                    @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   789
                    public String toString() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   790
                        return "repeated annotation pass of: " + sym + " in: " + sym.owner;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   791
                    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   792
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   793
                    @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   794
                    public void run() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   795
                        JavaFileObject oldSource =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   796
                            log.useSource(env.toplevel.sourcefile);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   797
                        try {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   798
                            attacher.attach(sym, replacePlaceholders(attrs, ctx, sym));
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   799
                        } finally {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   800
                            log.useSource(oldSource);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   801
                        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   802
                    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   803
                });
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   804
        } else {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   805
            attacher.attach(sym, attrs);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   806
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   807
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   808
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   809
    private interface AttributeAttacher<T extends Attribute.Compound> {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   810
        public void attach(Symbol sym, List<T> attrs);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   811
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   812
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   813
    private final AttributeAttacher<Attribute.Compound> declAnnotationsAttacher =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   814
        new AttributeAttacher<Attribute.Compound>() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   815
            @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   816
            public void attach(Symbol sym, List<Attribute.Compound> attrs) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   817
                sym.resetAnnotations();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   818
                sym.setDeclarationAttributes(attrs);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   819
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   820
        };
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   821
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   822
    private final AttributeAttacher<Attribute.TypeCompound> typeAnnotationsAttacher =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   823
        new AttributeAttacher<Attribute.TypeCompound>() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   824
            @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   825
            public void attach(Symbol sym, List<Attribute.TypeCompound> attrs) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   826
                sym.appendUniqueTypeAttributes(attrs);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   827
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   828
        };
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   829
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   830
    private <T extends Attribute.Compound> List<T>
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   831
            replacePlaceholders(List<T> buf,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   832
                                Annotate.AnnotationContext<T> ctx,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   833
                                Symbol sym) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   834
        List<T> result = List.nil();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   835
        for (T a : buf) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   836
            if (a instanceof Placeholder) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   837
                @SuppressWarnings("unchecked")
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   838
                    T replacement = replaceOne((Placeholder<T>) a, ctx, sym);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   839
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   840
                if (null != replacement) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   841
                    result = result.prepend(replacement);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   842
                }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   843
            } else {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   844
                result = result.prepend(a);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   845
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   846
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   847
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   848
        return result.reverse();
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   849
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   850
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   851
    private <T extends Attribute.Compound> T replaceOne(Placeholder<T> placeholder,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   852
                                                        Annotate.AnnotationContext<T> ctx,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   853
                                                        Symbol sym) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   854
        // Process repeated annotations
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   855
        T validRepeated =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   856
            processRepeatedAnnotations(placeholder.getPlaceholderFor(),
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   857
                                       ctx, sym);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   858
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   859
        if (validRepeated != null) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   860
            // Check that the container isn't manually
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   861
            // present along with repeated instances of
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   862
            // its contained annotation.
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   863
            ListBuffer<T> manualContainer = ctx.annotated.get(validRepeated.type.tsym);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   864
            if (manualContainer != null) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   865
                log.error(ctx.pos.get(manualContainer.first()), "invalid.repeatable.annotation.repeated.and.container.present",
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   866
                        manualContainer.first().type.tsym);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   867
            }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   868
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   869
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   870
        // A null return will delete the Placeholder
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   871
        return validRepeated;
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   872
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   873
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   874
/* ********************************************************************
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   875
 * Annotation processing
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   876
 *********************************************************************/
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   877
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   878
    /** Queue annotations for later processing. */
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   879
    void annotateLater(final List<JCAnnotation> annotations,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   880
                       final Env<AttrContext> localEnv,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   881
                       final Symbol s,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   882
                       final DiagnosticPosition deferPos) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   883
        if (annotations.isEmpty()) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   884
            return;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   885
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   886
        if (s.kind != PCK) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   887
            s.resetAnnotations(); // mark Annotations as incomplete for now
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   888
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   889
        normal(new Annotate.Worker() {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   890
                @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   891
                public String toString() {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   892
                    return "annotate " + annotations + " onto " + s + " in " + s.owner;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   893
                }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   894
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   895
                @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   896
                public void run() {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   897
                    Assert.check(s.kind == PCK || s.annotationsPendingCompletion());
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   898
                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   899
                    DiagnosticPosition prevLintPos =
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   900
                        deferPos != null
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   901
                        ? deferredLintHandler.setPos(deferPos)
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   902
                        : deferredLintHandler.immediate();
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   903
                    Lint prevLint = deferPos != null ? null : chk.setLint(lint);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   904
                    try {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   905
                        if (s.hasAnnotations() &&
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   906
                            annotations.nonEmpty())
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   907
                            log.error(annotations.head.pos,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   908
                                      "already.annotated",
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   909
                                      kindName(s), s);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   910
                        actualEnterAnnotations(annotations, localEnv, s);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   911
                    } finally {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   912
                        if (prevLint != null)
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   913
                            chk.setLint(prevLint);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   914
                        deferredLintHandler.setPos(prevLintPos);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   915
                        log.useSource(prev);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   916
                    }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   917
                }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   918
            });
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   919
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   920
        validate(new Annotate.Worker() { //validate annotations
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   921
            @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   922
            public void run() {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   923
                JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   924
                try {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   925
                    chk.validateAnnotations(annotations, s);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   926
                } finally {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   927
                    log.useSource(prev);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   928
                }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   929
            }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   930
        });
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   931
    }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   932
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   933
    private interface AttributeCreator<T extends Attribute.Compound> {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   934
        public T create(JCAnnotation a, Type expected, Env<AttrContext> env);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   935
    }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   936
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   937
    // TODO: When SE8 features can be used, these can go away and be
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   938
    // replaced by method refs.
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   939
    private final AttributeCreator<Attribute.Compound> enterAnnotationsCreator =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   940
        new AttributeCreator<Attribute.Compound>() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   941
        @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   942
        public Attribute.Compound create(JCAnnotation a,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   943
                                         Type expected,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   944
                                         Env<AttrContext> env) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   945
            return enterAnnotation(a, syms.annotationType, env);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   946
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   947
    };
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   948
    private final AttributeCreator<Attribute.TypeCompound> enterTypeAnnotationsCreator =
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   949
        new AttributeCreator<Attribute.TypeCompound>() {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   950
        @Override
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   951
        public Attribute.TypeCompound create(JCAnnotation a,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   952
                                             Type expected,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   953
                                             Env<AttrContext> env) {
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   954
            return enterTypeAnnotation(a, syms.annotationType, env);
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   955
        }
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   956
    };
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   957
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   958
    /** Enter a set of annotations. */
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   959
    private void actualEnterAnnotations(List<JCAnnotation> annotations,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   960
                                        Env<AttrContext> env,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   961
                                        Symbol s) {
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   962
        Assert.checkNonNull(s, "Symbol argument to actualEnterAnnotations is null");
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   963
        attachAttributesLater(annotations, env, s, false,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   964
                              enterAnnotationsCreator,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   965
                              declAnnotationsAttacher);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   966
    }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   967
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   968
    /*
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   969
     * If the symbol is non-null, attach the type annotation to it.
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   970
     */
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   971
    private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations,
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   972
                                            final Env<AttrContext> env,
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   973
                                            final Symbol s,
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   974
                                            final DiagnosticPosition deferPos) {
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   975
        Assert.checkNonNull(s, "Symbol argument to actualEnterTypeAnnotations is nul/");
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   976
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   977
        DiagnosticPosition prevLintPos = null;
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   978
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   979
        if (deferPos != null) {
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   980
            prevLintPos = deferredLintHandler.setPos(deferPos);
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   981
        }
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   982
        try {
23814
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   983
            attachAttributesLater(annotations, env, s, true,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   984
                                  enterTypeAnnotationsCreator,
06ab27895804 8038263: Refactor annotation handling after actualEnterAnnotations
emc
parents: 23397
diff changeset
   985
                                  typeAnnotationsAttacher);
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   986
        } finally {
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   987
            if (prevLintPos != null)
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   988
                deferredLintHandler.setPos(prevLintPos);
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   989
            log.useSource(prev);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   990
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   991
    }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
   992
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   993
    public void annotateTypeLater(final JCTree tree,
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   994
                                  final Env<AttrContext> env,
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   995
                                  final Symbol sym,
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   996
                                  final DiagnosticPosition deferPos) {
23134
010b1b0d25fb 8036142: Remove redundant call to annotateTypeLater with null Symbol
emc
parents: 23129
diff changeset
   997
        Assert.checkNonNull(sym);
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   998
        normal(new Annotate.Worker() {
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
   999
                @Override
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1000
                public String toString() {
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1001
                    return "type annotate " + tree + " onto " + sym + " in " + sym.owner;
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1002
                }
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1003
                @Override
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1004
                public void run() {
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1005
                    tree.accept(new TypeAnnotate(env, sym, deferPos));
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1006
                }
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1007
            });
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1008
    }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1009
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1010
    /**
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1011
     * We need to use a TreeScanner, because it is not enough to visit the top-level
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1012
     * annotations. We also need to visit type arguments, etc.
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1013
     */
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1014
    private class TypeAnnotate extends TreeScanner {
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1015
        private final Env<AttrContext> env;
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1016
        private final Symbol sym;
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1017
        private DiagnosticPosition deferPos;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1018
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1019
        public TypeAnnotate(final Env<AttrContext> env,
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1020
                            final Symbol sym,
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1021
                            final DiagnosticPosition deferPos) {
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1022
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1023
            this.env = env;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1024
            this.sym = sym;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1025
            this.deferPos = deferPos;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1026
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1027
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1028
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1029
        public void visitAnnotatedType(final JCAnnotatedType tree) {
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1030
            actualEnterTypeAnnotations(tree.annotations, env, sym, deferPos);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1031
            super.visitAnnotatedType(tree);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1032
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1033
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1034
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1035
        public void visitTypeParameter(final JCTypeParameter tree) {
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1036
            actualEnterTypeAnnotations(tree.annotations, env, sym, deferPos);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1037
            super.visitTypeParameter(tree);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1038
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1039
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1040
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1041
        public void visitNewArray(final JCNewArray tree) {
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1042
            actualEnterTypeAnnotations(tree.annotations, env, sym, deferPos);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1043
            for (List<JCAnnotation> dimAnnos : tree.dimAnnotations)
23129
6d3b2e246dac 8035766: Execute TypeAnnotate visitor at queue flush time
emc
parents: 23126
diff changeset
  1044
                actualEnterTypeAnnotations(dimAnnos, env, sym, deferPos);
23126
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1045
            super.visitNewArray(tree);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1046
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1047
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1048
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1049
        public void visitMethodDef(final JCMethodDecl tree) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1050
            scan(tree.mods);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1051
            scan(tree.restype);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1052
            scan(tree.typarams);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1053
            scan(tree.recvparam);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1054
            scan(tree.params);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1055
            scan(tree.thrown);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1056
            scan(tree.defaultValue);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1057
            // Do not annotate the body, just the signature.
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1058
            // scan(tree.body);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1059
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1060
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1061
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1062
        public void visitVarDef(final JCVariableDecl tree) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1063
            DiagnosticPosition prevPos = deferPos;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1064
            deferPos = tree.pos();
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1065
            try {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1066
                if (sym != null && sym.kind == Kinds.VAR) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1067
                    // Don't visit a parameter once when the sym is the method
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1068
                    // and once when the sym is the parameter.
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1069
                    scan(tree.mods);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1070
                    scan(tree.vartype);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1071
                }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1072
                scan(tree.init);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1073
            } finally {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1074
                deferPos = prevPos;
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1075
            }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1076
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1077
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1078
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1079
        public void visitClassDef(JCClassDecl tree) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1080
            // We can only hit a classdef if it is declared within
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1081
            // a method. Ignore it - the class will be visited
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1082
            // separately later.
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1083
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1084
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1085
        @Override
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1086
        public void visitNewClass(JCNewClass tree) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1087
            if (tree.def == null) {
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1088
                // For an anonymous class instantiation the class
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1089
                // will be visited separately.
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1090
                super.visitNewClass(tree);
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1091
            }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1092
        }
1994a0d3b8de 8035758: Move annotation codepaths from MemberEnter.java to Annotate.java
emc
parents: 22702
diff changeset
  1093
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
  1094
}