jdk/src/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
author martin
Fri, 24 Oct 2008 20:34:40 -0700
changeset 1508 fed1edd772d5
parent 715 f16baef3a20e
child 3959 05a07c0a273b
permissions -rw-r--r--
6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods Summary: Move call to getDeclaredMethods inside doPrivileged Reviewed-by: darcy, mullan Contributed-by: jjb@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final Class type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final Map<String, Object> memberValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    AnnotationInvocationHandler(Class type, Map<String, Object> memberValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.memberValues = memberValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public Object invoke(Object proxy, Method method, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String member = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Class[] paramTypes = method.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // Handle Object and Annotation methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (member.equals("equals") && paramTypes.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            paramTypes[0] == Object.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return equalsImpl(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        assert paramTypes.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (member.equals("toString"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return toStringImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (member.equals("hashCode"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return hashCodeImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (member.equals("annotationType"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Handle annotation member accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Object result = memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new IncompleteAnnotationException(type, member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (result instanceof ExceptionProxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw ((ExceptionProxy) result).generateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (result.getClass().isArray() && Array.getLength(result) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            result = cloneArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * This method, which clones its array argument, would not be necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * if Cloneable had a public clone method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Object cloneArray(Object array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Class type = array.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (type == byte[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            byte[] byteArray = (byte[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return byteArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (type == char[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            char[] charArray = (char[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return charArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (type == double[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            double[] doubleArray = (double[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return doubleArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (type == float[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            float[] floatArray = (float[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return floatArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (type == int[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int[] intArray = (int[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return intArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (type == long[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            long[] longArray = (long[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return longArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (type == short[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            short[] shortArray = (short[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return shortArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (type == boolean[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            boolean[] booleanArray = (boolean[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return booleanArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Object[] objectArray = (Object[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return objectArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
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
     * Implementation of dynamicProxy.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private String toStringImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        StringBuffer result = new StringBuffer(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        result.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        result.append(type.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        result.append('(');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        boolean firstMember = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (Map.Entry<String, Object> e : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (firstMember)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                firstMember = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                result.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            result.append(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            result.append(memberValueToString(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Translates a member value (in "dynamic proxy return form") into a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static String memberValueToString(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Class type = value.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (!type.isArray())    // primitive, string, class, enum const,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                // or annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return Arrays.toString((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return Arrays.toString((char[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return Arrays.toString((double[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return Arrays.toString((float[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return Arrays.toString((int[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return Arrays.toString((long[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return Arrays.toString((short[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (type == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return Arrays.toString((boolean[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return Arrays.toString((Object[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Implementation of dynamicProxy.equals(Object o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private Boolean equalsImpl(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!type.isInstance(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        for (Method memberMethod : getMemberMethods()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            String member = memberMethod.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            Object ourValue = memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Object hisValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            AnnotationInvocationHandler hisHandler = asOneOfUs(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (hisHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                hisValue = hisHandler.memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    hisValue = memberMethod.invoke(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (!memberValueEquals(ourValue, hisValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns an object's invocation handler if that object is a dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * proxy with a handler of type AnnotationInvocationHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns null otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private AnnotationInvocationHandler asOneOfUs(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (Proxy.isProxyClass(o.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            InvocationHandler handler = Proxy.getInvocationHandler(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (handler instanceof AnnotationInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                return (AnnotationInvocationHandler) handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns true iff the two member values in "dynamic proxy return form"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * are equal using the appropriate equality function depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * member type.  The two values will be of the same type unless one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * the containing annotations is ill-formed.  If one of the containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * annotations is ill-formed, this method will return false unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * two members are identical object references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static boolean memberValueEquals(Object v1, Object v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Class type = v1.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // Check for primitive, string, class, enum const, annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // or ExceptionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (!type.isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return v1.equals(v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Check for array of string, class, enum const, annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // or ExceptionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (v1 instanceof Object[] && v2 instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return Arrays.equals((Object[]) v1, (Object[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // Check for ill formed annotation(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (v2.getClass() != type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // Deal with array of primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return Arrays.equals((byte[]) v1, (byte[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return Arrays.equals((char[]) v1, (char[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return Arrays.equals((double[]) v1, (double[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return Arrays.equals((float[]) v1, (float[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return Arrays.equals((int[]) v1, (int[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return Arrays.equals((long[]) v1, (long[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return Arrays.equals((short[]) v1, (short[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        assert type == boolean[].class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return Arrays.equals((boolean[]) v1, (boolean[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns the member methods for our annotation type.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * obtained lazily and cached, as they're expensive to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * and we only need them if our equals method is invoked (which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * be rare).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private Method[] getMemberMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (memberMethods == null) {
1508
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   275
            memberMethods = AccessController.doPrivileged(
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   276
                new PrivilegedAction<Method[]>() {
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   277
                    public Method[] run() {
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   278
                        final Method[] mm = type.getDeclaredMethods();
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   279
                        AccessibleObject.setAccessible(mm, true);
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   280
                        return mm;
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   281
                    }
fed1edd772d5 6761678: (ann) SecurityException in AnnotationInvocationHandler.getMemberMethods
martin
parents: 715
diff changeset
   282
                });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return memberMethods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private transient volatile Method[] memberMethods = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Implementation of dynamicProxy.hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private int hashCodeImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        for (Map.Entry<String, Object> e : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            result += (127 * e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                memberValueHashCode(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Computes hashCode of a member value (in "dynamic proxy return form")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private static int memberValueHashCode(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Class type = value.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (!type.isArray())    // primitive, string, class, enum const,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                // or annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return value.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return Arrays.hashCode((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return Arrays.hashCode((char[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return Arrays.hashCode((double[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return Arrays.hashCode((float[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return Arrays.hashCode((int[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return Arrays.hashCode((long[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return Arrays.hashCode((short[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (type == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return Arrays.hashCode((boolean[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return Arrays.hashCode((Object[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // Check to make sure that types have not evolved incompatibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        AnnotationType annotationType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            annotationType = AnnotationType.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } catch(IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // Class is no longer an annotation type; all bets are off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Map<String, Class> memberTypes = annotationType.memberTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        for (Map.Entry<String, Object> memberValue : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            String name = memberValue.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Class memberType = memberTypes.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (memberType != null) {  // i.e. member still exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                Object value = memberValue.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (!(memberType.isInstance(value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                      value instanceof ExceptionProxy)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    memberValue.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        new AnnotationTypeMismatchExceptionProxy(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            value.getClass() + "[" + value + "]").setMember(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                annotationType.members().get(name)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}