jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
author darcy
Thu, 16 Jun 2016 13:00:34 -0700
changeset 39043 815e52744068
parent 38770 e8746fa36f1a
child 40116 9bde91c0d0ef
permissions -rw-r--r--
8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation Reviewed-by: mchung, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38770
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
     2
 * Copyright (c) 2003, 2016, 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
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
    28
import java.io.ObjectInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.annotation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
38770
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
    33
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * InvocationHandler for dynamic proxy implementation of Annotation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class AnnotationInvocationHandler implements InvocationHandler, Serializable {
10357
c2cde4cd24a1 7080038: (ann) Serializable types in sun.reflect.annotation do not declare serialVersionUIDs
darcy
parents: 5506
diff changeset
    44
    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
    45
    private final Class<? extends Annotation> type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final Map<String, Object> memberValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
    48
    AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) {
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    49
        Class<?>[] superInterfaces = type.getInterfaces();
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    50
        if (!type.isAnnotation() ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    51
            superInterfaces.length != 1 ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    52
            superInterfaces[0] != java.lang.annotation.Annotation.class)
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    53
            throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.memberValues = memberValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public Object invoke(Object proxy, Method method, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        String member = method.getName();
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
    60
        Class<?>[] paramTypes = method.getParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // Handle Object and Annotation methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (member.equals("equals") && paramTypes.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            paramTypes[0] == Object.class)
39043
815e52744068 8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation
darcy
parents: 38770
diff changeset
    65
            return equalsImpl(proxy, args[0]);
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    66
        if (paramTypes.length != 0)
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    67
            throw new AssertionError("Too many parameters for an annotation method");
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    68
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    69
        switch(member) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    70
        case "toString":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return toStringImpl();
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    72
        case "hashCode":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return hashCodeImpl();
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    74
        case "annotationType":
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return type;
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
    76
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Handle annotation member accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Object result = memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new IncompleteAnnotationException(type, member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (result instanceof ExceptionProxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw ((ExceptionProxy) result).generateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (result.getClass().isArray() && Array.getLength(result) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            result = cloneArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * This method, which clones its array argument, would not be necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * if Cloneable had a public clone method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    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
    98
        Class<?> type = array.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (type == byte[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            byte[] byteArray = (byte[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return byteArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (type == char[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            char[] charArray = (char[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return charArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (type == double[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            double[] doubleArray = (double[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return doubleArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (type == float[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            float[] floatArray = (float[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return floatArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (type == int[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int[] intArray = (int[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return intArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (type == long[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            long[] longArray = (long[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return longArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (type == short[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            short[] shortArray = (short[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return shortArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (type == boolean[].class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            boolean[] booleanArray = (boolean[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return booleanArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Object[] objectArray = (Object[])array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return objectArray.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Implementation of dynamicProxy.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private String toStringImpl() {
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   142
        StringBuilder result = new StringBuilder(128);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        result.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        result.append(type.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        result.append('(');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        boolean firstMember = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        for (Map.Entry<String, Object> e : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (firstMember)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                firstMember = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                result.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            result.append(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            result.append(memberValueToString(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
38770
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   162
     * Translates a member value (in "dynamic proxy return form") into a string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    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
   165
        Class<?> type = value.getClass();
38770
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   166
        if (!type.isArray()) {   // primitive, string, class, enum const,
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   167
                                 // or annotation
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   168
            if (type == Class.class)
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   169
                return classValueToString((Class<?>) value);
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   170
            else
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   171
                return value.toString();
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   172
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return Arrays.toString((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return Arrays.toString((char[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return Arrays.toString((double[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return Arrays.toString((float[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return Arrays.toString((int[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return Arrays.toString((long[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return Arrays.toString((short[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (type == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return Arrays.toString((boolean[]) value);
38770
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   190
        if (type == Class[].class)
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   191
            return classArrayValueToString((Class<?>[])value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return Arrays.toString((Object[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
38770
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   196
     * Translates a Class value to a form suitable for use in the
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   197
     * string representation of an annotation.
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   198
     */
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   199
    private static String classValueToString(Class<?> clazz) {
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   200
        return clazz.getName() + ".class" ;
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   201
    }
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   202
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   203
    private static String classArrayValueToString(Class<?>[] classes) {
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   204
        return Arrays.stream(classes)
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   205
            .map(AnnotationInvocationHandler::classValueToString)
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   206
            .collect(Collectors.joining(", ", "{", "}"));
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   207
    }
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   208
e8746fa36f1a 5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents: 35295
diff changeset
   209
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Implementation of dynamicProxy.equals(Object o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
39043
815e52744068 8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation
darcy
parents: 38770
diff changeset
   212
    private Boolean equalsImpl(Object proxy, Object o) {
815e52744068 8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation
darcy
parents: 38770
diff changeset
   213
        if (o == proxy)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (!type.isInstance(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (Method memberMethod : getMemberMethods()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            String member = memberMethod.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            Object ourValue = memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            Object hisValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            AnnotationInvocationHandler hisHandler = asOneOfUs(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (hisHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                hisValue = hisHandler.memberValues.get(member);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    hisValue = memberMethod.invoke(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (!memberValueEquals(ourValue, hisValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns an object's invocation handler if that object is a dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * proxy with a handler of type AnnotationInvocationHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns null otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private AnnotationInvocationHandler asOneOfUs(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (Proxy.isProxyClass(o.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            InvocationHandler handler = Proxy.getInvocationHandler(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (handler instanceof AnnotationInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                return (AnnotationInvocationHandler) handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns true iff the two member values in "dynamic proxy return form"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * are equal using the appropriate equality function depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * member type.  The two values will be of the same type unless one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the containing annotations is ill-formed.  If one of the containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * annotations is ill-formed, this method will return false unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * two members are identical object references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    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
   263
        Class<?> type = v1.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Check for primitive, string, class, enum const, annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // or ExceptionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (!type.isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return v1.equals(v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // Check for array of string, class, enum const, annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // or ExceptionProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (v1 instanceof Object[] && v2 instanceof Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return Arrays.equals((Object[]) v1, (Object[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Check for ill formed annotation(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (v2.getClass() != type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // Deal with array of primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return Arrays.equals((byte[]) v1, (byte[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return Arrays.equals((char[]) v1, (char[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return Arrays.equals((double[]) v1, (double[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return Arrays.equals((float[]) v1, (float[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return Arrays.equals((int[]) v1, (int[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return Arrays.equals((long[]) v1, (long[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return Arrays.equals((short[]) v1, (short[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        assert type == boolean[].class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return Arrays.equals((boolean[]) v1, (boolean[]) v2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns the member methods for our annotation type.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * obtained lazily and cached, as they're expensive to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * and we only need them if our equals method is invoked (which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * be rare).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private Method[] getMemberMethods() {
28056
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   305
        Method[] value = memberMethods;
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   306
        if (value == null) {
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   307
            value = computeMemberMethods();
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   308
            memberMethods = value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
28056
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   310
        return value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
28056
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   312
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   313
    private Method[] computeMemberMethods() {
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   314
        return AccessController.doPrivileged(
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   315
            new PrivilegedAction<Method[]>() {
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   316
                public Method[] run() {
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   317
                    final Method[] methods = type.getDeclaredMethods();
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   318
                    validateAnnotationMethods(methods);
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   319
                    AccessibleObject.setAccessible(methods, true);
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   320
                    return methods;
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   321
                }});
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   322
    }
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27069
diff changeset
   323
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 28056
diff changeset
   324
    private transient volatile Method[] memberMethods;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
27069
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   327
     * Validates that a method is structurally appropriate for an
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   328
     * annotation type. As of Java SE 8, annotation types cannot
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   329
     * contain static methods and the declared methods of an
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   330
     * annotation type must take zero arguments and there are
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   331
     * restrictions on the return type.
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   332
     */
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   333
    private void validateAnnotationMethods(Method[] memberMethods) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   334
        /*
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   335
         * Specification citations below are from JLS
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   336
         * 9.6.1. Annotation Type Elements
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   337
         */
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   338
        boolean valid = true;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   339
        for(Method method : memberMethods) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   340
            /*
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   341
             * "By virtue of the AnnotationTypeElementDeclaration
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   342
             * production, a method declaration in an annotation type
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   343
             * declaration cannot have formal parameters, type
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   344
             * parameters, or a throws clause.
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   345
             *
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   346
             * "By virtue of the AnnotationTypeElementModifier
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   347
             * production, a method declaration in an annotation type
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   348
             * declaration cannot be default or static."
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   349
             */
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   350
            if (method.getModifiers() != (Modifier.PUBLIC | Modifier.ABSTRACT) ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   351
                method.isDefault() ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   352
                method.getParameterCount() != 0 ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   353
                method.getExceptionTypes().length != 0) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   354
                valid = false;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   355
                break;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   356
            }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   357
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   358
            /*
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   359
             * "It is a compile-time error if the return type of a
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   360
             * method declared in an annotation type is not one of the
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   361
             * following: a primitive type, String, Class, any
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   362
             * parameterized invocation of Class, an enum type
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   363
             * (section 8.9), an annotation type, or an array type
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   364
             * (chapter 10) whose element type is one of the preceding
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   365
             * types."
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   366
             */
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   367
            Class<?> returnType = method.getReturnType();
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   368
            if (returnType.isArray()) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   369
                returnType = returnType.getComponentType();
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   370
                if (returnType.isArray()) { // Only single dimensional arrays
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   371
                    valid = false;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   372
                    break;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   373
                }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   374
            }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   375
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   376
            if (!((returnType.isPrimitive() && returnType != void.class) ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   377
                  returnType == java.lang.String.class ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   378
                  returnType == java.lang.Class.class ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   379
                  returnType.isEnum() ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   380
                  returnType.isAnnotation())) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   381
                valid = false;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   382
                break;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   383
            }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   384
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   385
            /*
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   386
             * "It is a compile-time error if any method declared in an
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   387
             * annotation type has a signature that is
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   388
             * override-equivalent to that of any public or protected
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   389
             * method declared in class Object or in the interface
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   390
             * java.lang.annotation.Annotation."
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   391
             *
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   392
             * The methods in Object or Annotation meeting the other
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   393
             * criteria (no arguments, contrained return type, etc.)
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   394
             * above are:
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   395
             *
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   396
             * String toString()
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   397
             * int hashCode()
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   398
             * Class<? extends Annotation> annotationType()
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   399
             */
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   400
            String methodName = method.getName();
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   401
            if ((methodName.equals("toString") && returnType == java.lang.String.class) ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   402
                (methodName.equals("hashCode") && returnType == int.class) ||
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   403
                (methodName.equals("annotationType") && returnType == java.lang.Class.class)) {
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   404
                valid = false;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   405
                break;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   406
            }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   407
        }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   408
        if (valid)
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   409
            return;
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   410
        else
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   411
            throw new AnnotationFormatError("Malformed method on an annotation type");
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   412
    }
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   413
ee10bb4f6dc4 8035781: Improve equality for annotations
darcy
parents: 25859
diff changeset
   414
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Implementation of dynamicProxy.hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private int hashCodeImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        for (Map.Entry<String, Object> e : memberValues.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            result += (127 * e.getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                memberValueHashCode(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Computes hashCode of a member value (in "dynamic proxy return form")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    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
   430
        Class<?> type = value.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (!type.isArray())    // primitive, string, class, enum const,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                // or annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return value.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (type == byte[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return Arrays.hashCode((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (type == char[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return Arrays.hashCode((char[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (type == double[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return Arrays.hashCode((double[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (type == float[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return Arrays.hashCode((float[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (type == int[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            return Arrays.hashCode((int[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (type == long[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return Arrays.hashCode((long[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (type == short[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return Arrays.hashCode((short[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (type == boolean[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return Arrays.hashCode((boolean[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return Arrays.hashCode((Object[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        throws java.io.IOException, ClassNotFoundException {
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   456
        ObjectInputStream.GetField fields = s.readFields();
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   457
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   458
        @SuppressWarnings("unchecked")
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   459
        Class<? extends Annotation> t = (Class<? extends Annotation>)fields.get("type", null);
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   460
        @SuppressWarnings("unchecked")
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   461
        Map<String, Object> streamVals = (Map<String, Object>)fields.get("memberValues", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // Check to make sure that types have not evolved incompatibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        AnnotationType annotationType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        try {
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   467
            annotationType = AnnotationType.getInstance(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        } catch(IllegalArgumentException e) {
18199
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   469
            // Class is no longer an annotation type; time to punch out
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   470
            throw new java.io.InvalidObjectException("Non-annotation type in annotation serial stream");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   473
        Map<String, Class<?>> memberTypes = annotationType.memberTypes();
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   474
        // consistent with runtime Map type
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   475
        Map<String, Object> mv = new LinkedHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
18199
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   477
        // If there are annotation members without values, that
bfd86c4f4249 8001309: Better handling of annotation interfaces
darcy
parents: 10357
diff changeset
   478
        // situation is handled by the invoke method.
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   479
        for (Map.Entry<String, Object> memberValue : streamVals.entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            String name = memberValue.getKey();
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   481
            Object value = null;
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 1508
diff changeset
   482
            Class<?> memberType = memberTypes.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            if (memberType != null) {  // i.e. member still exists
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   484
                value = memberValue.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (!(memberType.isInstance(value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                      value instanceof ExceptionProxy)) {
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   487
                    value = new AnnotationTypeMismatchExceptionProxy(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            value.getClass() + "[" + value + "]").setMember(
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   489
                                annotationType.members().get(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
35295
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   492
            mv.put(name, value);
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   493
        }
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   494
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   495
        UnsafeAccessor.setType(this, t);
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   496
        UnsafeAccessor.setMemberValues(this, mv);
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   497
    }
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   498
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   499
    private static class UnsafeAccessor {
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   500
        private static final jdk.internal.misc.Unsafe unsafe;
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   501
        private static final long typeOffset;
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   502
        private static final long memberValuesOffset;
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   503
        static {
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   504
            try {
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   505
                unsafe = jdk.internal.misc.Unsafe.getUnsafe();
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   506
                typeOffset = unsafe.objectFieldOffset
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   507
                        (AnnotationInvocationHandler.class.getDeclaredField("type"));
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   508
                memberValuesOffset = unsafe.objectFieldOffset
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   509
                        (AnnotationInvocationHandler.class.getDeclaredField("memberValues"));
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   510
            } catch (Exception ex) {
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   511
                throw new ExceptionInInitializerError(ex);
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   512
            }
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   513
        }
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   514
        static void setType(AnnotationInvocationHandler o,
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   515
                            Class<? extends Annotation> type) {
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   516
            unsafe.putObject(o, typeOffset, type);
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   517
        }
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   518
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   519
        static void setMemberValues(AnnotationInvocationHandler o,
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   520
                                    Map<String, Object> memberValues) {
80c40839a17e 8143185: Cleanup for handling proxies
chegar
parents: 34774
diff changeset
   521
            unsafe.putObject(o, memberValuesOffset, memberValues);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
}