langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java
author emc
Tue, 21 Oct 2014 09:01:51 -0400
changeset 27224 228abfa87080
parent 25874 83c19f00452c
permissions -rw-r--r--
8054457: Refactor Symbol kinds from small ints to an enum Summary: Replace bitmap logic in symbol.kind and pkind with an enum-based API Reviewed-by: mcimadamore, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 22163
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5003
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: 5003
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: 5003
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5003
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5003
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.model;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13689
diff changeset
    28
import java.io.IOException;
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
    29
import java.io.ObjectInputStream;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.lang.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Array;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.LinkedHashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import sun.reflect.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.type.MirroredTypeException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.type.MirroredTypesException;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13689
diff changeset
    39
import javax.lang.model.type.TypeMirror;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13689
diff changeset
    40
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.code.Type.ArrayType;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 13689
diff changeset
    44
import com.sun.tools.javac.util.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 22163
diff changeset
    46
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
    47
import static com.sun.tools.javac.code.Kinds.Kind.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * A generator of dynamic proxy implementations of
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * java.lang.annotation.Annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * <p> The "dynamic proxy return form" of an annotation element value is
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * the form used by sun.reflect.annotation.AnnotationInvocationHandler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5843
diff changeset
    56
 * <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5843
diff changeset
    57
 * If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
public class AnnotationProxyMaker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    private final Attribute.Compound anno;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private final Class<? extends Annotation> annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private AnnotationProxyMaker(Attribute.Compound anno,
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                                 Class<? extends Annotation> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        this.anno = anno;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        this.annoType = annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    public static <A extends Annotation> A generateAnnotation(
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            Attribute.Compound anno, Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        AnnotationProxyMaker apm = new AnnotationProxyMaker(anno, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return annoType.cast(apm.generateAnnotation());
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private Annotation generateAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return AnnotationParser.annotationForMap(annoType,
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                                                 getAllReflectedValues());
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * Returns a map from element names to their values in "dynamic
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * proxy return form".  Includes all elements, whether explicit or
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    private Map<String, Object> getAllReflectedValues() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19507
diff changeset
    99
        Map<String, Object> res = new LinkedHashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        for (Map.Entry<MethodSymbol, Attribute> entry :
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                                                  getAllValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            MethodSymbol meth = entry.getKey();
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            Object value = generateValue(meth, entry.getValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            if (value != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                res.put(meth.name.toString(), value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                // Ignore this element.  May (properly) lead to
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                // IncompleteAnnotationException somewhere down the line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * Returns a map from element symbols to their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * Includes all elements, whether explicit or defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    private Map<MethodSymbol, Attribute> getAllValues() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19507
diff changeset
   120
        Map<MethodSymbol, Attribute> res = new LinkedHashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        // First find the default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        ClassSymbol sym = (ClassSymbol) anno.type.tsym;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 22163
diff changeset
   124
        for (Symbol s : sym.members().getSymbols(NON_RECURSIVE)) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
   125
            if (s.kind == MTH) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 22163
diff changeset
   126
                MethodSymbol m = (MethodSymbol) s;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                Attribute def = m.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                if (def != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                    res.put(m, def);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        // Next find the explicit values, possibly overriding defaults.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        for (Pair<MethodSymbol, Attribute> p : anno.values)
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            res.put(p.fst, p.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * Converts an element value to its "dynamic proxy return form".
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * Returns an exception proxy on some errors, but may return null if
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * a useful exception cannot or should not be generated at this point.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    private Object generateValue(MethodSymbol meth, Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        ValueVisitor vv = new ValueVisitor(meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return vv.getValue(attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    private class ValueVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        private MethodSymbol meth;      // annotation element being visited
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        private Class<?> returnClass;   // return type of annotation element
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        private Object value;           // value in "dynamic proxy return form"
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        ValueVisitor(MethodSymbol meth) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        Object getValue(Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            Method method;              // runtime method of annotation element
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                method = annoType.getMethod(meth.name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            } catch (NoSuchMethodException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            returnClass = method.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            attr.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            if (!(value instanceof ExceptionProxy) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                !AnnotationType.invocationHandlerReturnType(returnClass)
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                                                        .isInstance(value)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                typeMismatch(method, attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            return value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        public void visitConstant(Attribute.Constant c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            value = c.getValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        public void visitClass(Attribute.Class c) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
   182
            value = new MirroredTypeExceptionProxy(c.classType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        public void visitArray(Attribute.Array a) {
5843
e659a8974c9c 6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents: 5520
diff changeset
   186
            Name elemName = ((ArrayType) a.type).elemtype.tsym.getQualifiedName();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
5843
e659a8974c9c 6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents: 5520
diff changeset
   188
            if (elemName.equals(elemName.table.names.java_lang_Class)) {   // Class[]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                // Construct a proxy for a MirroredTypesException
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19507
diff changeset
   190
                ListBuffer<TypeMirror> elems = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                for (Attribute value : a.values) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
   192
                    Type elem = ((Attribute.Class) value).classType;
5843
e659a8974c9c 6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents: 5520
diff changeset
   193
                    elems.append(elem);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                }
5843
e659a8974c9c 6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents: 5520
diff changeset
   195
                value = new MirroredTypesExceptionProxy(elems.toList());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                int len = a.values.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                Class<?> returnClassSaved = returnClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                returnClass = returnClass.getComponentType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                    Object res = Array.newInstance(returnClass, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                    for (int i = 0; i < len; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                        a.values[i].accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                        if (value == null || value instanceof ExceptionProxy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                            Array.set(res, i, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                            value = null;       // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                    value = res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                    returnClass = returnClassSaved;
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   222
        @SuppressWarnings({"unchecked", "rawtypes"})
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            if (returnClass.isEnum()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                String constName = e.value.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                try {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   227
                    value = Enum.valueOf((Class)returnClass, constName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                } catch (IllegalArgumentException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                    value = new EnumConstantNotPresentExceptionProxy(
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   230
                                        (Class<Enum<?>>) returnClass, constName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                value = null;   // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        public void visitCompound(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                Class<? extends Annotation> nested =
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                    returnClass.asSubclass(Annotation.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                value = generateAnnotation(c, nested);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            } catch (ClassCastException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                value = null;   // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        public void visitError(Attribute.Error e) {
19507
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 16557
diff changeset
   248
            if (e instanceof Attribute.UnresolvedClass)
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 16557
diff changeset
   249
                value = new MirroredTypeExceptionProxy(((Attribute.UnresolvedClass)e).classType);
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 16557
diff changeset
   250
            else
323f001d6be1 8019243: AnnotationTypeMismatchException instead of MirroredTypeException
jfranck
parents: 16557
diff changeset
   251
                value = null;       // indicates a type mismatch
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
         * Sets "value" to an ExceptionProxy indicating a type mismatch.
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
         */
6931
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   258
        private void typeMismatch(Method method, final Attribute attr) {
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   259
            class AnnotationTypeMismatchExceptionProxy extends ExceptionProxy {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                static final long serialVersionUID = 269;
6931
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   261
                transient final Method method;
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   262
                AnnotationTypeMismatchExceptionProxy(Method method) {
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   263
                    this.method = method;
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   264
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                    return "<error>";   // eg:  @Anno(value=<error>)
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    return new AnnotationTypeMismatchException(method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                                attr.type.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                }
6931
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   272
            }
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   273
            value = new AnnotationTypeMismatchExceptionProxy(method);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * ExceptionProxy for MirroredTypeException.
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents: 14258
diff changeset
   280
     * The toString, hashCode, and equals methods forward to the underlying
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     * type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     */
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   283
    private static final class MirroredTypeExceptionProxy extends ExceptionProxy {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   286
        private transient TypeMirror type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        private final String typeString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        MirroredTypeExceptionProxy(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            type = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            typeString = t.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            return typeString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            return (type != null ? type : typeString).hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            return type != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                   obj instanceof MirroredTypeExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                   type.equals(((MirroredTypeExceptionProxy) obj).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            return new MirroredTypeException(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        }
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   311
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   312
        // Explicitly set all transient fields.
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   313
        private void readObject(ObjectInputStream s)
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   314
            throws IOException, ClassNotFoundException {
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   315
            s.defaultReadObject();
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   316
            type = null;
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   317
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     * ExceptionProxy for MirroredTypesException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     */
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   326
    private static final class MirroredTypesExceptionProxy extends ExceptionProxy {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   329
        private transient List<TypeMirror> types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        private final String typeStrings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        MirroredTypesExceptionProxy(List<TypeMirror> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            types = ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            typeStrings = ts.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            return typeStrings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            return (types != null ? types : typeStrings).hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            return types != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                   obj instanceof MirroredTypesExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                   types.equals(
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                      ((MirroredTypesExceptionProxy) obj).types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            return new MirroredTypesException(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        }
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   355
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   356
        // Explicitly set all transient fields.
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   357
        private void readObject(ObjectInputStream s)
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   358
            throws IOException, ClassNotFoundException {
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   359
            s.defaultReadObject();
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   360
            types = null;
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   361
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
}