jdk/src/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
author darcy
Tue, 19 Mar 2013 14:59:33 -0700
changeset 18199 bfd86c4f4249
parent 10357 c2cde4cd24a1
permissions -rw-r--r--
8001309: Better handling of annotation interfaces Reviewed-by: ahgross, smarks, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18199
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.reflect.annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.annotation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.annotation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * InvocationHandler for dynamic proxy implementation of Annotation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class AnnotationInvocationHandler implements InvocationHandler, Serializable {
10357
c2cde4cd24a1 7080038: (ann) Serializable types in sun.reflect.annotation do not declare serialVersionUIDs
darcy
parents: 5506
diff changeset
    43
    private static final long serialVersionUID = 6182022883658399397L;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
    44
    private final Class<? extends Annotation> type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final Map<String, Object> memberValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
    47
    AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this.memberValues = memberValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public Object invoke(Object proxy, Method method, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String member = method.getName();
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
    54
        Class<?>[] paramTypes = method.getParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // Handle Object and Annotation methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (member.equals("equals") && paramTypes.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            paramTypes[0] == Object.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            return equalsImpl(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        assert paramTypes.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (member.equals("toString"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return toStringImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (member.equals("hashCode"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return hashCodeImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (member.equals("annotationType"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // Handle annotation member accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Object result = memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new IncompleteAnnotationException(type, member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (result instanceof ExceptionProxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw ((ExceptionProxy) result).generateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (result.getClass().isArray() && Array.getLength(result) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            result = cloneArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * This method, which clones its array argument, would not be necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * if Cloneable had a public clone method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private Object cloneArray(Object array) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
    88
        Class<?> type = array.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (type == byte[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            byte[] byteArray = (byte[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return byteArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (type == char[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            char[] charArray = (char[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return charArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (type == double[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            double[] doubleArray = (double[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return doubleArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (type == float[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            float[] floatArray = (float[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return floatArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (type == int[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int[] intArray = (int[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return intArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (type == long[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            long[] longArray = (long[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return longArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (type == short[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            short[] shortArray = (short[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return shortArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (type == boolean[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            boolean[] booleanArray = (boolean[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return booleanArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Object[] objectArray = (Object[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return objectArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Implementation of dynamicProxy.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private String toStringImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        StringBuffer result = new StringBuffer(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        result.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        result.append(type.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        result.append('(');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        boolean firstMember = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (Map.Entry<String, Object> e : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (firstMember)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                firstMember = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                result.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            result.append(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            result.append(memberValueToString(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Translates a member value (in "dynamic proxy return form") into a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static String memberValueToString(Object value) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   155
        Class<?> type = value.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!type.isArray())    // primitive, string, class, enum const,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                // or annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return Arrays.toString((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return Arrays.toString((char[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return Arrays.toString((double[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return Arrays.toString((float[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return Arrays.toString((int[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return Arrays.toString((long[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return Arrays.toString((short[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (type == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return Arrays.toString((boolean[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return Arrays.toString((Object[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Implementation of dynamicProxy.equals(Object o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private Boolean equalsImpl(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (!type.isInstance(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (Method memberMethod : getMemberMethods()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            String member = memberMethod.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Object ourValue = memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            Object hisValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            AnnotationInvocationHandler hisHandler = asOneOfUs(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (hisHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                hisValue = hisHandler.memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    hisValue = memberMethod.invoke(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (!memberValueEquals(ourValue, hisValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns an object's invocation handler if that object is a dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * proxy with a handler of type AnnotationInvocationHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Returns null otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private AnnotationInvocationHandler asOneOfUs(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (Proxy.isProxyClass(o.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            InvocationHandler handler = Proxy.getInvocationHandler(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (handler instanceof AnnotationInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                return (AnnotationInvocationHandler) handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns true iff the two member values in "dynamic proxy return form"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * are equal using the appropriate equality function depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * member type.  The two values will be of the same type unless one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * the containing annotations is ill-formed.  If one of the containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * annotations is ill-formed, this method will return false unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * two members are identical object references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static boolean memberValueEquals(Object v1, Object v2) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   233
        Class<?> type = v1.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Check for primitive, string, class, enum const, annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // or ExceptionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (!type.isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return v1.equals(v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // Check for array of string, class, enum const, annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // or ExceptionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (v1 instanceof Object[] && v2 instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return Arrays.equals((Object[]) v1, (Object[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // Check for ill formed annotation(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (v2.getClass() != type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Deal with array of primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return Arrays.equals((byte[]) v1, (byte[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return Arrays.equals((char[]) v1, (char[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return Arrays.equals((double[]) v1, (double[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return Arrays.equals((float[]) v1, (float[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return Arrays.equals((int[]) v1, (int[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return Arrays.equals((long[]) v1, (long[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return Arrays.equals((short[]) v1, (short[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        assert type == boolean[].class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return Arrays.equals((boolean[]) v1, (boolean[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns the member methods for our annotation type.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * obtained lazily and cached, as they're expensive to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * and we only need them if our equals method is invoked (which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * be rare).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private Method[] getMemberMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (memberMethods == null) {
1508
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   276
            memberMethods = AccessController.doPrivileged(
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   277
                new PrivilegedAction<Method[]>() {
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   278
                    public Method[] run() {
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   279
                        final Method[] mm = type.getDeclaredMethods();
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   280
                        AccessibleObject.setAccessible(mm, true);
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   281
                        return mm;
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   282
                    }
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   283
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return memberMethods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private transient volatile Method[] memberMethods = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Implementation of dynamicProxy.hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private int hashCodeImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        for (Map.Entry<String, Object> e : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            result += (127 * e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                memberValueHashCode(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Computes hashCode of a member value (in "dynamic proxy return form")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private static int memberValueHashCode(Object value) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   305
        Class<?> type = value.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (!type.isArray())    // primitive, string, class, enum const,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                // or annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return value.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return Arrays.hashCode((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return Arrays.hashCode((char[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return Arrays.hashCode((double[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return Arrays.hashCode((float[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return Arrays.hashCode((int[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return Arrays.hashCode((long[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return Arrays.hashCode((short[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (type == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return Arrays.hashCode((boolean[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return Arrays.hashCode((Object[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // Check to make sure that types have not evolved incompatibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        AnnotationType annotationType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            annotationType = AnnotationType.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch(IllegalArgumentException e) {
18199
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   340
            // Class is no longer an annotation type; time to punch out
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   341
            throw new java.io.InvalidObjectException("Non-annotation type in annotation serial stream");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   344
        Map<String, Class<?>> memberTypes = annotationType.memberTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
18199
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   346
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   347
        // If there are annotation members without values, that
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   348
        // situation is handled by the invoke method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        for (Map.Entry<String, Object> memberValue : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            String name = memberValue.getKey();
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   351
            Class<?> memberType = memberTypes.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (memberType != null) {  // i.e. member still exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                Object value = memberValue.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                if (!(memberType.isInstance(value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                      value instanceof ExceptionProxy)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    memberValue.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        new AnnotationTypeMismatchExceptionProxy(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            value.getClass() + "[" + value + "]").setMember(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                annotationType.members().get(name)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}