langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Attribute.java
author chegar
Sun, 17 Aug 2014 15:52:32 +0100
changeset 25874 83c19f00452c
parent 24895 langtools/src/share/classes/com/sun/tools/javac/code/Attribute.java@dd091d389fbf
child 26266 2d24bda701dc
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14961
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 3149
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: 3149
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: 3149
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
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.code;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.LinkedHashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.element.AnnotationMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.AnnotationValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.AnnotationValueVisitor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/** An annotation value.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    39
 *  <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
    40
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
public abstract class Attribute implements AnnotationValue {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    /** The type of the annotation element. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    public Type type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public Attribute(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        this.type = type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public abstract void accept(Visitor v);
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public Object getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
14961
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
    63
    public boolean isSynthesized() {
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
    64
        return false;
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
    65
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21041
diff changeset
    67
    public TypeAnnotationPosition getPosition() { return null; }
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
    68
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** The value for an annotation element of primitive type or String. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public static class Constant extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        public final Object value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        public void accept(Visitor v) { v.visitConstant(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        public Constant(Type type, Object value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            super(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            this.value = value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            return Constants.format(value, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        public Object getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            return Constants.decode(value, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            if (value instanceof String)
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                return v.visitString((String) value, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            if (value instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                int i = (Integer) value;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17578
diff changeset
    88
                switch (type.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                case BOOLEAN:   return v.visitBoolean(i != 0, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                case CHAR:      return v.visitChar((char) i, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                case BYTE:      return v.visitByte((byte) i, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                case SHORT:     return v.visitShort((short) i, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                case INT:       return v.visitInt(i, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            }
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17578
diff changeset
    96
            switch (type.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            case LONG:          return v.visitLong((Long) value, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            case FLOAT:         return v.visitFloat((Float) value, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            case DOUBLE:        return v.visitDouble((Double) value, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            throw new AssertionError("Bad annotation element value: " + value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    /** The value for an annotation element of type java.lang.Class,
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     *  represented as a ClassSymbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    public static class Class extends Attribute {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   109
        public final Type classType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        public void accept(Visitor v) { v.visitClass(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        public Class(Types types, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            super(makeClassType(types, type));
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   113
            this.classType = type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        static Type makeClassType(Types types, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            Type arg = type.isPrimitive()
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                ? types.boxedClass(type).type
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                : types.erasure(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            return new Type.ClassType(types.syms.classType.getEnclosingType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                                      List.of(arg),
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23798
diff changeset
   121
                                      types.syms.classType.tsym,
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23798
diff changeset
   122
                                      Type.noAnnotations);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        public String toString() {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   125
            return classType + ".class";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        public Type getValue() {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   128
            return classType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   131
            return v.visitType(classType, p);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /** A compound annotation element value, the type of which is an
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     *  attribute interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    public static class Compound extends Attribute implements AnnotationMirror {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        /** The attributes values, as pairs.  Each pair contains a
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
         *  reference to the accessing method in the attribute interface
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
         *  and the value to be returned when that method is called to
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
         *  access this attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        public final List<Pair<MethodSymbol,Attribute>> values;
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   145
        public final TypeAnnotationPosition position;
14961
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   146
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   147
        private boolean synthesized = false;
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   148
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   149
        @Override
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   150
        public boolean isSynthesized() {
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   151
            return synthesized;
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   152
        }
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   153
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   154
        public void setSynthesized(boolean synthesized) {
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   155
            this.synthesized = synthesized;
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   156
        }
e731935052af 8005098: Provide isSynthesized() information on Attribute.Compound
jfranck
parents: 14359
diff changeset
   157
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        public Compound(Type type,
23798
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   159
                        List<Pair<MethodSymbol,Attribute>> values,
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   160
                        TypeAnnotationPosition position) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            super(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            this.values = values;
23798
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   163
            this.position = position;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
23798
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   165
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   166
        public Compound(Type type,
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   167
                        List<Pair<MethodSymbol,Attribute>> values) {
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   168
            this(type, values, null);
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   169
        }
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   170
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   171
        @Override
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   172
        public TypeAnnotationPosition getPosition() {
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   173
            return position;
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   174
        }
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   175
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        public void accept(Visitor v) { v.visitCompound(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
         * Returns a string representation of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
         * String is of one of the forms:
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
         *     @com.example.foo(name1=val1, name2=val2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
         *     @com.example.foo(val)
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
         *     @com.example.foo
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
         * Omit parens for marker annotations, and omit "value=" when allowed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            StringBuilder buf = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            buf.append("@");
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            buf.append(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            int len = values.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            if (len > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                buf.append('(');
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                for (Pair<MethodSymbol, Attribute> value : values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                    if (!first) buf.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                    Name name = value.fst.name;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   199
                    if (len > 1 || name != name.table.names.value) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                        buf.append(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                        buf.append('=');
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                    buf.append(value.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                buf.append(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        public Attribute member(Name member) {
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   211
            Pair<MethodSymbol,Attribute> res = getElemPair(member);
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   212
            return res == null ? null : res.snd;
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   213
        }
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   214
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   215
        private Pair<MethodSymbol, Attribute> getElemPair(Name member) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            for (Pair<MethodSymbol,Attribute> pair : values)
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   217
                if (pair.fst.name == member) return pair;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        public Attribute.Compound getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            return v.visitAnnotation(this, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        public DeclaredType getAnnotationType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            return (DeclaredType) type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        public Map<MethodSymbol, Attribute> getElementValues() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21041
diff changeset
   234
            Map<MethodSymbol, Attribute> valmap = new LinkedHashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            for (Pair<MethodSymbol, Attribute> value : values)
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                valmap.put(value.fst, value.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            return valmap;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        }
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   239
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   240
        public TypeCompound toTypeCompound() {
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   241
            // It is safe to alias the position.
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   242
            return new TypeCompound(this, this.position);
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   243
        }
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24396
diff changeset
   244
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14961
diff changeset
   247
    public static class TypeCompound extends Compound {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14961
diff changeset
   248
        public TypeCompound(Compound compound,
23798
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   249
                             TypeAnnotationPosition position) {
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   250
            super(compound.type, compound.values, position);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   251
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   252
23798
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   253
        public TypeCompound(Type type,
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   254
                             List<Pair<MethodSymbol,Attribute>> values,
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   255
                             TypeAnnotationPosition position) {
ca0f80391182 8035768: Move TypeAnnotationPosition from Attribute.Compound to Attribute.TypeCompound
emc
parents: 22163
diff changeset
   256
            super(type, values, position);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   257
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14961
diff changeset
   258
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14961
diff changeset
   259
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    /** The value for an annotation element of an array type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    public static class Array extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        public final Attribute[] values;
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        public Array(Type type, Attribute[] values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            super(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            this.values = values;
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   268
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   269
        public Array(Type type, List<Attribute> values) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   270
            super(type);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   271
            this.values = values.toArray(new Attribute[values.size()]);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   272
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8032
diff changeset
   273
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        public void accept(Visitor v) { v.visitArray(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            StringBuilder buf = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            buf.append('{');
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            for (Attribute value : values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                    buf.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                buf.append(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            buf.append('}');
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        public List<Attribute> getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            return List.from(values);
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            return v.visitArray(getValue(), p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        }
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   294
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   295
        @Override
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   296
        public TypeAnnotationPosition getPosition() {
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   297
            if (values.length != 0)
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   298
                return values[0].getPosition();
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   299
            else
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   300
                return null;
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 19507
diff changeset
   301
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    /** The value for an annotation element of an enum type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    public static class Enum extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        public VarSymbol value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        public Enum(Type type, VarSymbol value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            super(type);
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
   310
            this.value = Assert.checkNonNull(value);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        public void accept(Visitor v) { v.visitEnum(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            return value.enclClass() + "." + value;     // qualified name
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        public VarSymbol getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            return value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            return v.visitEnumConstant(value, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    public static class Error extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        public Error(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            super(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        public void accept(Visitor v) { v.visitError(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            return "<error>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        public String getValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            return toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            return v.visitString(toString(), p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
19507
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   340
    public static class UnresolvedClass extends Error {
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   341
        public Type classType;
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   342
        public UnresolvedClass(Type type, Type classType) {
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   343
            super(type);
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   344
            this.classType = classType;
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   345
        }
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   346
    }
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 18646
diff changeset
   347
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    /** A visitor type for dynamic dispatch on the kind of attribute value. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    public static interface Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        void visitConstant(Attribute.Constant value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        void visitClass(Attribute.Class clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        void visitCompound(Attribute.Compound compound);
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        void visitArray(Attribute.Array array);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        void visitEnum(Attribute.Enum e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        void visitError(Attribute.Error e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    }
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   357
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   358
    /** A mirror of java.lang.annotation.RetentionPolicy. */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   359
    public static enum RetentionPolicy {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   360
        SOURCE,
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   361
        CLASS,
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   362
        RUNTIME
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   363
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
}