langtools/src/share/classes/com/sun/tools/javac/code/Annotations.java
author rfield
Fri, 15 Feb 2013 18:40:38 -0800
changeset 16300 7cf27559c8df
parent 15385 ee1eebe7e210
child 17578 46ac954e4a84
permissions -rw-r--r--
8004969: Generate $deserializeLambda$ method 8006763: super in method reference used in anonymous class - ClassFormatError is produced 8005632: Inner classes within lambdas cause build failures 8005653: Lambdas containing inner classes referencing external type variables do not correctly parameterize the inner classes Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     1
/*
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14804
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     4
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    10
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    15
 * accompanied this code).
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    16
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    20
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    23
 * questions.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    24
 */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    25
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    26
package com.sun.tools.javac.code;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    27
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    28
import java.util.Map;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13689
diff changeset
    29
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    30
import javax.tools.JavaFileObject;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    31
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    32
import com.sun.tools.javac.comp.Annotate;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    33
import com.sun.tools.javac.comp.AttrContext;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    34
import com.sun.tools.javac.comp.Env;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13689
diff changeset
    35
import com.sun.tools.javac.util.*;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    36
import com.sun.tools.javac.util.Assert;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    37
import com.sun.tools.javac.util.List;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    38
import com.sun.tools.javac.util.Log;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    39
import com.sun.tools.javac.util.Pair;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    40
import static com.sun.tools.javac.code.Kinds.PCK;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    41
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    42
/**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    43
 * Container for all annotations (attributes in javac) on a Symbol.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    44
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    45
 * This class is explicitly mutable. Its contents will change when attributes
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    46
 * are annotated onto the Symbol. However this class depends on the facts that
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    47
 * List (in javac) is immutable.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    48
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    49
 * An instance of this class can be in one of three states:
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    50
 *
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    51
 * NOT_STARTED indicates that the Symbol this instance belongs to has not been
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    52
 * annotated (yet). Specifically if the declaration is not annotated this
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    53
 * instance will never move past NOT_STARTED. You can never go back to
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    54
 * NOT_STARTED.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    55
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    56
 * IN_PROGRESS annotations have been found on the declaration. Will be processed
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    57
 * later. You can reset to IN_PROGRESS. While IN_PROGRESS you can set the list
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    58
 * of attributes (and this moves out of the IN_PROGRESS state).
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    59
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    60
 * "unnamed" this Annotations contains some attributes, possibly the final set.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    61
 * While in this state you can only prepend or append to the attributes not set
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    62
 * it directly. You can also move back to the IN_PROGRESS state using reset().
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    63
 *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    64
 * <p><b>This is NOT part of any supported API. If you write code that depends
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    65
 * on this, you do so at your own risk. This code and its internal interfaces
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    66
 * are subject to change or deletion without notice.</b>
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    67
 */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    68
public class Annotations {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    69
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    70
    private static final List<Attribute.Compound> DECL_NOT_STARTED = List.of(null);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    71
    private static final List<Attribute.Compound> DECL_IN_PROGRESS = List.of(null);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    72
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    73
    /*
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    74
     * This field should never be null
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    75
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    76
    private List<Attribute.Compound> attributes = DECL_NOT_STARTED;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    77
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    78
    /*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    79
     * This field should never be null
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    80
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    81
    private List<Attribute.TypeCompound> type_attributes = List.<Attribute.TypeCompound>nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    82
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    83
    /*
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    84
     * The Symbol this Annotations instance belongs to
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    85
     */
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14258
diff changeset
    86
    private final Symbol sym;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    87
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14258
diff changeset
    88
    public Annotations(Symbol sym) {
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14258
diff changeset
    89
        this.sym = sym;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    90
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    91
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    92
    public List<Attribute.Compound> getDeclarationAttributes() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    93
        return filterDeclSentinels(attributes);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    94
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
    95
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    96
    public List<Attribute.TypeCompound> getTypeAttributes() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    97
        return type_attributes;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    98
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
    99
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   100
    public void setDeclarationAttributes(List<Attribute.Compound> a) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   101
        Assert.check(pendingCompletion() || !isStarted());
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   102
        if (a == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   103
            throw new NullPointerException();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   104
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   105
        attributes = a;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   106
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   107
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   108
    public void setTypeAttributes(List<Attribute.TypeCompound> a) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   109
        if (a == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   110
            throw new NullPointerException();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   111
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   112
        type_attributes = a;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   113
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   114
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   115
    public void setAttributes(Annotations other) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   116
        if (other == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   117
            throw new NullPointerException();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   118
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   119
        setDeclarationAttributes(other.getDeclarationAttributes());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   120
        setTypeAttributes(other.getTypeAttributes());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   121
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   122
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   123
    public void setDeclarationAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.Compound> ctx) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   124
        Assert.check(pendingCompletion() || (!isStarted() && sym.kind == PCK));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   125
        this.setDeclarationAttributes(getAttributesForCompletion(ctx));
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   126
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   127
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   128
    public void appendTypeAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.TypeCompound> ctx) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   129
        this.appendUniqueTypes(getAttributesForCompletion(ctx));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   130
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   131
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   132
    private <T extends Attribute.Compound> List<T> getAttributesForCompletion(
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   133
            final Annotate.AnnotateRepeatedContext<T> ctx) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   134
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   135
        Map<Symbol.TypeSymbol, ListBuffer<T>> annotated = ctx.annotated;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   136
        boolean atLeastOneRepeated = false;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   137
        List<T> buf = List.<T>nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   138
        for (ListBuffer<T> lb : annotated.values()) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   139
            if (lb.size() == 1) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   140
                buf = buf.prepend(lb.first());
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   141
            } else { // repeated
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   142
                // This will break when other subtypes of Attributs.Compound
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   143
                // are introduced, because PlaceHolder is a subtype of TypeCompound.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   144
                T res;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   145
                @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   146
                T ph = (T) new Placeholder<T>(ctx, lb.toList(), sym);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   147
                res = ph;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   148
                buf = buf.prepend(res);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   149
                atLeastOneRepeated = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   150
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   151
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   152
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   153
        if (atLeastOneRepeated) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   154
            // The Symbol s is now annotated with a combination of
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   155
            // finished non-repeating annotations and placeholders for
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   156
            // repeating annotations.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   157
            //
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   158
            // We need to do this in two passes because when creating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   159
            // a container for a repeating annotation we must
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14804
diff changeset
   160
            // guarantee that the @Repeatable on the
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   161
            // contained annotation is fully annotated
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   162
            //
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   163
            // The way we force this order is to do all repeating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   164
            // annotations in a pass after all non-repeating are
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14804
diff changeset
   165
            // finished. This will work because @Repeatable
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   166
            // is non-repeating and therefore will be annotated in the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   167
            // fist pass.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   168
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   169
            // Queue a pass that will replace Attribute.Placeholders
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   170
            // with Attribute.Compound (made from synthesized containers).
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   171
            ctx.annotateRepeated(new Annotate.Annotator() {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   172
                @Override
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   173
                public String toString() {
14804
f93a8d60b9a4 8001114: Container annotation is not checked for semantic correctness
jfranck
parents: 14258
diff changeset
   174
                    return "repeated annotation pass of: " + sym + " in: " + sym.owner;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   175
                }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   176
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   177
                @Override
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   178
                public void enterAnnotation() {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   179
                    complete(ctx);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   180
                }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   181
            });
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   182
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   183
        // Add non-repeating attributes
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   184
        return buf.reverse();
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   185
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   186
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   187
    public Annotations reset() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   188
        attributes = DECL_IN_PROGRESS;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   189
        return this;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   190
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   191
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   192
    public boolean isEmpty() {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   193
        return !isStarted()
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   194
                || pendingCompletion()
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   195
                || attributes.isEmpty();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   196
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   197
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   198
    public boolean isTypesEmpty() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   199
        return type_attributes.isEmpty();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   200
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   201
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   202
    public boolean pendingCompletion() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   203
        return attributes == DECL_IN_PROGRESS;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   204
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   205
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   206
    public Annotations append(List<Attribute.Compound> l) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   207
        attributes = filterDeclSentinels(attributes);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   208
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   209
        if (l.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   210
            ; // no-op
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   211
        } else if (attributes.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   212
            attributes = l;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   213
        } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   214
            attributes = attributes.appendList(l);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   215
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   216
        return this;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   217
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   218
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   219
    public Annotations appendUniqueTypes(List<Attribute.TypeCompound> l) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   220
        if (l.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   221
            ; // no-op
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   222
        } else if (type_attributes.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   223
            type_attributes = l;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   224
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   225
            // TODO: in case we expect a large number of annotations, this
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   226
            // might be inefficient.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   227
            for (Attribute.TypeCompound tc : l) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   228
                if (!type_attributes.contains(tc))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   229
                    type_attributes = type_attributes.append(tc);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   230
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   231
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   232
        return this;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   233
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   234
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   235
    public Annotations prepend(List<Attribute.Compound> l) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   236
        attributes = filterDeclSentinels(attributes);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   237
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   238
        if (l.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   239
            ; // no-op
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   240
        } else if (attributes.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   241
            attributes = l;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   242
        } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   243
            attributes = attributes.prependList(l);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   244
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   245
        return this;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   246
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   247
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   248
    private List<Attribute.Compound> filterDeclSentinels(List<Attribute.Compound> a) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   249
        return (a == DECL_IN_PROGRESS || a == DECL_NOT_STARTED)
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   250
                ? List.<Attribute.Compound>nil()
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   251
                : a;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   252
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   253
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   254
    private boolean isStarted() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   255
        return attributes != DECL_NOT_STARTED;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   256
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   257
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   258
    private List<Attribute.Compound> getPlaceholders() {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   259
        List<Attribute.Compound> res = List.<Attribute.Compound>nil();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   260
        for (Attribute.Compound a : filterDeclSentinels(attributes)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   261
            if (a instanceof Placeholder) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   262
                res = res.prepend(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   263
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   264
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   265
        return res.reverse();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   266
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   267
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   268
    private List<Attribute.TypeCompound> getTypePlaceholders() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   269
        List<Attribute.TypeCompound> res = List.<Attribute.TypeCompound>nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   270
        for (Attribute.TypeCompound a : type_attributes) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   271
            if (a instanceof Placeholder) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   272
                res = res.prepend(a);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   273
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   274
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   275
        return res.reverse();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   276
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   277
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   278
    /*
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   279
     * Replace Placeholders for repeating annotations with their containers
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   280
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   281
    private <T extends Attribute.Compound> void complete(Annotate.AnnotateRepeatedContext<T> ctx) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   282
        Log log = ctx.log;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   283
        Env<AttrContext> env = ctx.env;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   284
        JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   285
        try {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   286
            // TODO: can we reduce duplication in the following branches?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   287
            if (ctx.isTypeCompound) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   288
                Assert.check(!isTypesEmpty());
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   289
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   290
                if (isTypesEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   291
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   292
                }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   293
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   294
                List<Attribute.TypeCompound> result = List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   295
                for (Attribute.TypeCompound a : getTypeAttributes()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   296
                    if (a instanceof Placeholder) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   297
                        @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   298
                        Placeholder<Attribute.TypeCompound> ph = (Placeholder<Attribute.TypeCompound>) a;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   299
                        Attribute.TypeCompound replacement = replaceOne(ph, ph.getRepeatedContext());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   300
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   301
                        if (null != replacement) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   302
                            result = result.prepend(replacement);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   303
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   304
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   305
                        result = result.prepend(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   306
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   307
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   308
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   309
                type_attributes = result.reverse();
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   310
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   311
                Assert.check(Annotations.this.getTypePlaceholders().isEmpty());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   312
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   313
                Assert.check(!pendingCompletion());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   314
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   315
                if (isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   316
                    return;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   317
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   318
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   319
                List<Attribute.Compound> result = List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   320
                for (Attribute.Compound a : getDeclarationAttributes()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   321
                    if (a instanceof Placeholder) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   322
                        @SuppressWarnings("unchecked")
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   323
                        Attribute.Compound replacement = replaceOne((Placeholder<T>) a, ctx);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   324
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   325
                        if (null != replacement) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   326
                            result = result.prepend(replacement);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   327
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   328
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   329
                        result = result.prepend(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   330
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   331
                }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   332
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   333
                attributes = result.reverse();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   334
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   335
                Assert.check(Annotations.this.getPlaceholders().isEmpty());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   336
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   337
        } finally {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   338
            log.useSource(oldSource);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   339
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   340
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   341
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   342
    private <T extends Attribute.Compound> T replaceOne(Placeholder<T> placeholder, Annotate.AnnotateRepeatedContext<T> ctx) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   343
        Log log = ctx.log;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   344
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   345
        // Process repeated annotations
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   346
        T validRepeated = ctx.processRepeatedAnnotations(placeholder.getPlaceholderFor(), sym);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   347
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   348
        if (validRepeated != null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   349
            // Check that the container isn't manually
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   350
            // present along with repeated instances of
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   351
            // its contained annotation.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   352
            ListBuffer<T> manualContainer = ctx.annotated.get(validRepeated.type.tsym);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   353
            if (manualContainer != null) {
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14804
diff changeset
   354
                log.error(ctx.pos.get(manualContainer.first()), "invalid.repeatable.annotation.repeated.and.container.present",
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   355
                        manualContainer.first().type.tsym);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   356
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   357
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   358
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   359
        // A null return will delete the Placeholder
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   360
        return validRepeated;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   361
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   362
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   363
    private static class Placeholder<T extends Attribute.Compound> extends Attribute.TypeCompound {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   364
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   365
        private final Annotate.AnnotateRepeatedContext<T> ctx;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   366
        private final List<T> placeholderFor;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   367
        private final Symbol on;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   368
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   369
        public Placeholder(Annotate.AnnotateRepeatedContext<T> ctx, List<T> placeholderFor, Symbol on) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   370
            super(on.type, List.<Pair<Symbol.MethodSymbol, Attribute>>nil(),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   371
                    ctx.isTypeCompound ?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   372
                            ((Attribute.TypeCompound)placeholderFor.head).position :
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   373
                                null);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   374
            this.ctx = ctx;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   375
            this.placeholderFor = placeholderFor;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   376
            this.on = on;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   377
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   378
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   379
        @Override
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   380
        public String toString() {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   381
            return "<placeholder: " + placeholderFor + " on: " + on + ">";
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   382
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   383
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   384
        public List<T> getPlaceholderFor() {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   385
            return placeholderFor;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   386
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   387
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   388
        public Annotate.AnnotateRepeatedContext<T> getRepeatedContext() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   389
            return ctx;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15356
diff changeset
   390
        }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   391
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents:
diff changeset
   392
}