langtools/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java
author jfranck
Fri, 31 Aug 2012 10:37:46 +0100
changeset 13689 4d519199a6aa
parent 7681 1f0819a3341f
child 14258 8d2148961366
permissions -rw-r--r--
7151010: Add compiler support for repeating annotations Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
     2
 * Copyright (c) 2005, 2012, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.*;
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
    29
import java.io.ObjectInputStream;
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
    30
import java.io.IOException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.lang.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Array;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.LinkedHashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import sun.reflect.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.lang.model.type.MirroredTypeException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.lang.model.type.MirroredTypesException;
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;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * A generator of dynamic proxy implementations of
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * java.lang.annotation.Annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * <p> The "dynamic proxy return form" of an annotation element value is
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * the form used by sun.reflect.annotation.AnnotationInvocationHandler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5843
diff changeset
    53
 * <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
    54
 * If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * deletion without notice.</b>
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 class AnnotationProxyMaker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private final Attribute.Compound anno;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    private final Class<? extends Annotation> annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private AnnotationProxyMaker(Attribute.Compound anno,
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                                 Class<? extends Annotation> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        this.anno = anno;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        this.annoType = annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
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
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public static <A extends Annotation> A generateAnnotation(
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            Attribute.Compound anno, Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        AnnotationProxyMaker apm = new AnnotationProxyMaker(anno, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return annoType.cast(apm.generateAnnotation());
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
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
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    private Annotation generateAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        return AnnotationParser.annotationForMap(annoType,
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                                                 getAllReflectedValues());
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * Returns a map from element names to their values in "dynamic
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * proxy return form".  Includes all elements, whether explicit or
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    private Map<String, Object> getAllReflectedValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        Map<String, Object> res = new LinkedHashMap<String, Object>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        for (Map.Entry<MethodSymbol, Attribute> entry :
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                                                  getAllValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            MethodSymbol meth = entry.getKey();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            Object value = generateValue(meth, entry.getValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            if (value != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                res.put(meth.name.toString(), value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                // Ignore this element.  May (properly) lead to
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                // IncompleteAnnotationException somewhere down the line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * Returns a map from element symbols to their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * Includes all elements, whether explicit or defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    private Map<MethodSymbol, Attribute> getAllValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        Map<MethodSymbol, Attribute> res =
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            new LinkedHashMap<MethodSymbol, Attribute>();
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
5843
e659a8974c9c 6519115: MirroredTypeException thrown but should be MirroredTypesException
darcy
parents: 5520
diff changeset
   188
                ListBuffer<TypeMirror> elems = new ListBuffer<TypeMirror>();
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) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            value = null;       // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
         * Sets "value" to an ExceptionProxy indicating a type mismatch.
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
         */
6931
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   253
        private void typeMismatch(Method method, final Attribute attr) {
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   254
            class AnnotationTypeMismatchExceptionProxy extends ExceptionProxy {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                static final long serialVersionUID = 269;
6931
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   256
                transient final Method method;
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   257
                AnnotationTypeMismatchExceptionProxy(Method method) {
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   258
                    this.method = method;
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   259
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                    return "<error>";   // eg:  @Anno(value=<error>)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    return new AnnotationTypeMismatchException(method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                                attr.type.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                }
6931
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   267
            }
d6339142c51f 6990133: AnnotationProxyMaker.ValueVisitor$1 contains non-transient non-serializable field
jjg
parents: 5847
diff changeset
   268
            value = new AnnotationTypeMismatchExceptionProxy(method);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     * ExceptionProxy for MirroredTypeException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     * type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     */
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   278
    private static final class MirroredTypeExceptionProxy extends ExceptionProxy {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   281
        private transient TypeMirror type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        private final String typeString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        MirroredTypeExceptionProxy(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            type = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            typeString = t.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            return typeString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            return (type != null ? type : typeString).hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            return type != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                   obj instanceof MirroredTypeExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                   type.equals(((MirroredTypeExceptionProxy) obj).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            return new MirroredTypeException(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        }
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   306
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   307
        // Explicitly set all transient fields.
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   308
        private void readObject(ObjectInputStream s)
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   309
            throws IOException, ClassNotFoundException {
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   310
            s.defaultReadObject();
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   311
            type = null;
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   312
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
     * ExceptionProxy for MirroredTypesException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
     * types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     */
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   321
    private static final class MirroredTypesExceptionProxy extends ExceptionProxy {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   324
        private transient List<TypeMirror> types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        private final String typeStrings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        MirroredTypesExceptionProxy(List<TypeMirror> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            types = ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            typeStrings = ts.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            return typeStrings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            return (types != null ? types : typeStrings).hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            return types != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                   obj instanceof MirroredTypesExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                   types.equals(
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                      ((MirroredTypesExceptionProxy) obj).types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            return new MirroredTypesException(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        }
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   350
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   351
        // Explicitly set all transient fields.
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   352
        private void readObject(ObjectInputStream s)
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   353
            throws IOException, ClassNotFoundException {
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   354
            s.defaultReadObject();
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   355
            types = null;
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 1789
diff changeset
   356
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
}