langtools/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 1264 076a3cde30d5
child 5003 fd0b30cdbe5c
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 1260
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.lang.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Array;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Method;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.LinkedHashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import sun.reflect.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.type.TypeMirror;
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;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.code.Type.ArrayType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * A generator of dynamic proxy implementations of
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * java.lang.annotation.Annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * <p> The "dynamic proxy return form" of an annotation element value is
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * the form used by sun.reflect.annotation.AnnotationInvocationHandler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
public class AnnotationProxyMaker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private final Attribute.Compound anno;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private final Class<? extends Annotation> annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private AnnotationProxyMaker(Attribute.Compound anno,
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
                                 Class<? extends Annotation> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        this.anno = anno;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        this.annoType = annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public static <A extends Annotation> A generateAnnotation(
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            Attribute.Compound anno, Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        AnnotationProxyMaker apm = new AnnotationProxyMaker(anno, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return annoType.cast(apm.generateAnnotation());
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private Annotation generateAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return AnnotationParser.annotationForMap(annoType,
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                                                 getAllReflectedValues());
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * Returns a map from element names to their values in "dynamic
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * proxy return form".  Includes all elements, whether explicit or
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    private Map<String, Object> getAllReflectedValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        Map<String, Object> res = new LinkedHashMap<String, Object>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        for (Map.Entry<MethodSymbol, Attribute> entry :
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                                                  getAllValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            MethodSymbol meth = entry.getKey();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            Object value = generateValue(meth, entry.getValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            if (value != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                res.put(meth.name.toString(), value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                // Ignore this element.  May (properly) lead to
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                // IncompleteAnnotationException somewhere down the line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Returns a map from element symbols to their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * Includes all elements, whether explicit or defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private Map<MethodSymbol, Attribute> getAllValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        Map<MethodSymbol, Attribute> res =
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            new LinkedHashMap<MethodSymbol, Attribute>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        // First find the default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        ClassSymbol sym = (ClassSymbol) anno.type.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            if (e.sym.kind == Kinds.MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                MethodSymbol m = (MethodSymbol) e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                Attribute def = m.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                if (def != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    res.put(m, def);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        // Next find the explicit values, possibly overriding defaults.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        for (Pair<MethodSymbol, Attribute> p : anno.values)
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            res.put(p.fst, p.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * Converts an element value to its "dynamic proxy return form".
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * Returns an exception proxy on some errors, but may return null if
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * a useful exception cannot or should not be generated at this point.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    private Object generateValue(MethodSymbol meth, Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        ValueVisitor vv = new ValueVisitor(meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        return vv.getValue(attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    private class ValueVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        private MethodSymbol meth;      // annotation element being visited
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        private Class<?> returnClass;   // return type of annotation element
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        private Object value;           // value in "dynamic proxy return form"
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        ValueVisitor(MethodSymbol meth) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        Object getValue(Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            Method method;              // runtime method of annotation element
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                method = annoType.getMethod(meth.name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            } catch (NoSuchMethodException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            returnClass = method.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            attr.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            if (!(value instanceof ExceptionProxy) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                !AnnotationType.invocationHandlerReturnType(returnClass)
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                                                        .isInstance(value)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                typeMismatch(method, attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        public void visitConstant(Attribute.Constant c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            value = c.getValue();
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 visitClass(Attribute.Class c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            value = new MirroredTypeExceptionProxy(c.type);
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 visitArray(Attribute.Array a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            Name elemName = ((ArrayType) a.type).elemtype.tsym.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   184
            if (elemName == elemName.table.names.java_lang_Class) {   // Class[]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                // Construct a proxy for a MirroredTypesException
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                List<TypeMirror> elems = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                for (Attribute value : a.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                    Type elem = ((Attribute.Class) value).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                    elems.add(elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                value = new MirroredTypesExceptionProxy(elems);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                int len = a.values.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                Class<?> returnClassSaved = returnClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                returnClass = returnClass.getComponentType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                    Object res = Array.newInstance(returnClass, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                    for (int i = 0; i < len; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                        a.values[i].accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                        if (value == null || value instanceof ExceptionProxy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                            Array.set(res, i, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                            value = null;       // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                    value = res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                    returnClass = returnClassSaved;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   218
        @SuppressWarnings({"unchecked", "rawtypes"})
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            if (returnClass.isEnum()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                String constName = e.value.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                try {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   223
                    value = Enum.valueOf((Class)returnClass, constName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                } catch (IllegalArgumentException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                    value = new EnumConstantNotPresentExceptionProxy(
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   226
                                        (Class<Enum<?>>) returnClass, constName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                value = null;   // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        public void visitCompound(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                Class<? extends Annotation> nested =
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                    returnClass.asSubclass(Annotation.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                value = generateAnnotation(c, nested);
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            } catch (ClassCastException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                value = null;   // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            value = null;       // indicates a type mismatch
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
         * Sets "value" to an ExceptionProxy indicating a type mismatch.
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        private void typeMismatch(final Method method, final Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            value = new ExceptionProxy() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    return "<error>";   // eg:  @Anno(value=<error>)
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                    return new AnnotationTypeMismatchException(method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                                attr.type.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * ExceptionProxy for MirroredTypeException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    private static class MirroredTypeExceptionProxy extends ExceptionProxy {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        private transient final TypeMirror type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        private final String typeString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        MirroredTypeExceptionProxy(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            type = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            typeString = t.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            return typeString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            return (type != null ? type : typeString).hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            return type != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                   obj instanceof MirroredTypeExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                   type.equals(((MirroredTypeExceptionProxy) obj).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            return new MirroredTypeException(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
     * ExceptionProxy for MirroredTypesException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     * types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    private static class MirroredTypesExceptionProxy extends ExceptionProxy {
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        static final long serialVersionUID = 269;
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        private transient final List<TypeMirror> types;
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        private final String typeStrings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        MirroredTypesExceptionProxy(List<TypeMirror> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            types = ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            typeStrings = ts.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            return typeStrings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            return (types != null ? types : typeStrings).hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            return types != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                   obj instanceof MirroredTypesExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                   types.equals(
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                      ((MirroredTypesExceptionProxy) obj).types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            return new MirroredTypesException(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
}