langtools/src/share/classes/com/sun/tools/apt/mirror/declaration/AnnotationProxyMaker.java
author darcy
Thu, 25 Feb 2010 11:04:11 -0800
changeset 5003 fd0b30cdbe5c
parent 3378 22011d9a9398
child 5520 86e4b9a9da40
permissions -rw-r--r--
6929645: Address various findbugs warnings in langtools Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
2212
1d3dc0e0ba0c 6814575: Update copyright year
xdono
parents: 1870
diff changeset
     2
 * Copyright 2004-2009 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.apt.mirror.declaration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
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.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import sun.reflect.annotation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.mirror.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.mirror.type.MirroredTypeException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.mirror.type.MirroredTypesException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.apt.mirror.AptEnv;
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.util.Name;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * A generator of dynamic proxy implementations of
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * java.lang.annotation.Annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * <p> The "dynamic proxy return form" of an attribute element value is
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * the form used by sun.reflect.annotation.AnnotationInvocationHandler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
3378
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2212
diff changeset
    52
@SuppressWarnings("deprecation")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
class AnnotationProxyMaker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private final AptEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private final Attribute.Compound attrs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    private final Class<? extends Annotation> annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private AnnotationProxyMaker(AptEnv env,
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
                                 Attribute.Compound attrs,
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
                                 Class<? extends Annotation> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        this.attrs = attrs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        this.annoType = annoType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public static <A extends Annotation> A generateAnnotation(
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            AptEnv env, Attribute.Compound attrs, Class<A> annoType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        AnnotationProxyMaker apm = new AnnotationProxyMaker(env, attrs, annoType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        return annoType.cast(apm.generateAnnotation());
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    }
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
     * Returns a dynamic proxy for an annotation mirror.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    private Annotation generateAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        return AnnotationParser.annotationForMap(annoType,
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                                                 getAllReflectedValues());
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * Returns a map from element names to their values in "dynamic
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * proxy return form".  Includes all elements, whether explicit or
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    private Map<String, Object> getAllReflectedValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        Map<String, Object> res = new LinkedHashMap<String, Object>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        for (Map.Entry<MethodSymbol, Attribute> entry :
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                                                  getAllValues().entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            MethodSymbol meth = entry.getKey();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            Object value = generateValue(meth, entry.getValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            if (value != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                res.put(meth.name.toString(), value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                // Ignore this element.  May lead to
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                // IncompleteAnnotationException somewhere down the line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * Returns a map from element symbols to their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Includes all elements, whether explicit or defaulted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    private Map<MethodSymbol, Attribute> getAllValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        Map<MethodSymbol, Attribute> res =
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            new LinkedHashMap<MethodSymbol, Attribute>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        // First find the default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        ClassSymbol sym = (ClassSymbol) attrs.type.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            if (e.sym.kind == Kinds.MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                MethodSymbol m = (MethodSymbol) e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                Attribute def = m.defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                if (def != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                    res.put(m, def);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                }
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 : attrs.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            res.put(p.fst, p.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * Converts an element value to its "dynamic proxy return form".
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * Returns an exception proxy on some errors, but may return null if
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * a useful exception cannot or should not be generated at this point.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    private Object generateValue(MethodSymbol meth, Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        ValueVisitor vv = new ValueVisitor(meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        return vv.getValue(attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    private class ValueVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        private MethodSymbol meth;      // annotation element being visited
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        private Class<?> runtimeType;   // runtime type of annotation element
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        private Object value;           // value in "dynamic proxy return form"
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        ValueVisitor(MethodSymbol meth) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        Object getValue(Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            Method method;              // runtime method of annotation element
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                method = annoType.getMethod(meth.name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            } catch (NoSuchMethodException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            runtimeType = method.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            attr.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            if (!(value instanceof ExceptionProxy) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                !AnnotationType.invocationHandlerReturnType(runtimeType)
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                                                        .isInstance(value)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                typeMismatch(method, attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            return value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        public void visitConstant(Attribute.Constant c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            value = Constants.decodeConstant(c.value, c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        public void visitClass(Attribute.Class c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            value = new MirroredTypeExceptionProxy(
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                                env.typeMaker.getType(c.type));
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) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            Type elemtype = env.jctypes.elemtype(a.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            if (elemtype.tsym == env.symtab.classType.tsym) {   // Class[]
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                // Construct a proxy for a MirroredTypesException
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                ArrayList<TypeMirror> elems = new ArrayList<TypeMirror>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                for (int i = 0; i < a.values.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                    Type elem = ((Attribute.Class) a.values[i]).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                    elems.add(env.typeMaker.getType(elem));
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                value = new MirroredTypesExceptionProxy(elems);
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<?> runtimeTypeSaved = runtimeType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                runtimeType = runtimeType.getComponentType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                    Object res = Array.newInstance(runtimeType, 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
                    runtimeType = runtimeTypeSaved;
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: 10
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 (runtimeType.isEnum()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                String constName = e.value.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                    value = Enum.valueOf((Class)runtimeType, constName);
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: 10
diff changeset
   228
                                                        (Class<Enum<?>>)runtimeType, 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
                    runtimeType.asSubclass(Annotation.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                value = generateAnnotation(env, 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
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        private void typeMismatch(final Method method, final Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            value = new ExceptionProxy() {
1870
57a1138dffc8 6795903: fix latent build warnings in langtools repository
jjg
parents: 1789
diff changeset
   255
                private static final long serialVersionUID = 8473323277815075163L;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                    return "<error>";   // eg:  @Anno(value=<error>)
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                    return new AnnotationTypeMismatchException(method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                                attr.type.toString());
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
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * ExceptionProxy for MirroredTypeException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     * type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     */
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 3378
diff changeset
   273
    private static final class MirroredTypeExceptionProxy extends ExceptionProxy {
1870
57a1138dffc8 6795903: fix latent build warnings in langtools repository
jjg
parents: 1789
diff changeset
   274
        private static final long serialVersionUID = 6662035281599933545L;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        private MirroredTypeException ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        MirroredTypeExceptionProxy(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            // It would be safer if we could construct the exception in
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            // generateException(), but there would be no way to do
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            // that properly following deserialization.
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            ex = new MirroredTypeException(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            return ex.getQualifiedName();
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 int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            TypeMirror t = ex.getTypeMirror();
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            return (t != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                    ? t.hashCode()
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                    : ex.getQualifiedName().hashCode();
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 boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            TypeMirror t = ex.getTypeMirror();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            return t != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                   obj instanceof MirroredTypeExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                   t.equals(
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                        ((MirroredTypeExceptionProxy) obj).ex.getTypeMirror());
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            return (RuntimeException) ex.fillInStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
     * ExceptionProxy for MirroredTypesException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
     * The toString, hashCode, and equals methods foward to the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
     * types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     */
5003
fd0b30cdbe5c 6929645: Address various findbugs warnings in langtools
darcy
parents: 3378
diff changeset
   315
    private static final class MirroredTypesExceptionProxy extends ExceptionProxy {
1870
57a1138dffc8 6795903: fix latent build warnings in langtools repository
jjg
parents: 1789
diff changeset
   316
        private static final long serialVersionUID = -6670822532616693951L;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        private MirroredTypesException ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        MirroredTypesExceptionProxy(Collection<TypeMirror> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            // It would be safer if we could construct the exception in
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            // generateException(), but there would be no way to do
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            // that properly following deserialization.
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            ex = new MirroredTypesException(ts);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            return ex.getQualifiedNames().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            Collection<TypeMirror> ts = ex.getTypeMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            return (ts != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                    ? ts.hashCode()
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                    : ex.getQualifiedNames().hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            Collection<TypeMirror> ts = ex.getTypeMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            return ts != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                   obj instanceof MirroredTypesExceptionProxy &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                   ts.equals(
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                      ((MirroredTypesExceptionProxy) obj).ex.getTypeMirrors());
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        protected RuntimeException generateException() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            return (RuntimeException) ex.fillInStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
}