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