jdk/test/java/lang/reflect/annotationSharing/AnnotationSharing.java
author jfranck
Tue, 09 Sep 2014 10:48:01 +0200
changeset 26455 195a6f3e0cd0
permissions -rw-r--r--
8054987: (reflect) Add sharing of annotations between instances of Executable Reviewed-by: darcy, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26455
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     1
/*
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     4
 *
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     7
 * published by the Free Software Foundation.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     8
 *
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    13
 * accompanied this code).
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    14
 *
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    18
 *
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    21
 * questions.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    22
 */
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    23
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    24
/*
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    25
 * @test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    26
 * @bug 8054987
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    27
 * @summary Test sharing of annotations between Executable/Field instances.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    28
 *          Sharing should not be noticeable when performing mutating
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    29
 *          operations.
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    30
 * @run testng AnnotationSharing
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    31
 */
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    32
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    33
import java.lang.annotation.*;
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    34
import java.lang.reflect.*;
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    35
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    36
import org.testng.annotations.Test;
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    37
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    38
public class AnnotationSharing {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    39
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    40
    public void testMethodSharing() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    41
        Method[] m1 = AnnotationSharing.class.getMethods();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    42
        Method[] m2 = AnnotationSharing.class.getMethods();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    43
        validateSharingSafelyObservable(m1, m2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    44
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    45
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    46
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    47
    public void testDeclaredMethodSharing() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    48
        Method[] m3 = AnnotationSharing.class.getDeclaredMethods();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    49
        Method[] m4 = AnnotationSharing.class.getDeclaredMethods();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    50
        validateSharingSafelyObservable(m3, m4);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    51
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    52
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    53
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    54
    public void testFieldSharing() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    55
        Field[] f1 = AnnotationSharing.class.getFields();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    56
        Field[] f2 = AnnotationSharing.class.getFields();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    57
        validateSharingSafelyObservable(f1, f2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    58
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    59
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    60
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    61
    public void testDeclaredFieldsSharing() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    62
        Field[] f3 = AnnotationSharing.class.getDeclaredFields();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    63
        Field[] f4 = AnnotationSharing.class.getDeclaredFields();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    64
        validateSharingSafelyObservable(f3, f4);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    65
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    66
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    67
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    68
    public void testMethodSharingOccurs() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    69
        Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    70
        Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    71
        validateAnnotationSharing(mm1, mm2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    72
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    73
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    74
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    75
    public void testMethodSharingIsSafe() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    76
        Method mm1 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    77
        Method mm2 = AnnotationSharing.class.getDeclaredMethod("m", (Class<?>[])null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    78
        validateAnnotationSharingIsSafe(mm1, mm2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    79
        validateArrayValues(mm1.getAnnotation(Baz.class), mm2.getAnnotation(Baz.class));
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    80
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    81
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    82
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    83
    public void testFieldSharingOccurs() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    84
        Field ff1 = AnnotationSharing.class.getDeclaredField("f");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    85
        Field ff2 = AnnotationSharing.class.getDeclaredField("f");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    86
        validateAnnotationSharing(ff1, ff2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    87
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    88
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    89
    @Test
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    90
    public void testFieldSharingIsSafe() throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    91
        Field ff1 = AnnotationSharing.class.getDeclaredField("f");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    92
        Field ff2 = AnnotationSharing.class.getDeclaredField("f");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    93
        validateAnnotationSharingIsSafe(ff1, ff2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    94
        validateArrayValues(ff1.getAnnotation(Baz.class), ff2.getAnnotation(Baz.class));
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    95
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    96
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    97
    // Validate that AccessibleObject instances are not shared
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    98
    private static void validateSharingSafelyObservable(AccessibleObject[] m1, AccessibleObject[] m2)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
    99
            throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   100
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   101
        // Validate that setAccessible works
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   102
        for (AccessibleObject m : m1)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   103
            m.setAccessible(false);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   104
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   105
        for (AccessibleObject m : m2)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   106
            m.setAccessible(true);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   107
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   108
        for (AccessibleObject m : m1)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   109
            if (m.isAccessible())
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   110
                throw new RuntimeException(m + " should not be accessible");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   111
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   112
        for (AccessibleObject m : m2)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   113
            if (!m.isAccessible())
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   114
                throw new RuntimeException(m + " should be accessible");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   115
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   116
        // Validate that methods are still equal()
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   117
        for (int i = 0; i < m1.length; i++)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   118
            if (!m1[i].equals(m2[i]))
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   119
                throw new RuntimeException(m1[i] + " and " + m2[i] + " should be equal()");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   120
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   121
        // Validate that the arrays aren't shared
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   122
        for (int i = 0; i < m1.length; i++)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   123
            m1[i] = null;
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   124
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   125
        for (int i = 0; i < m2.length; i++)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   126
            if (m2[i] == null)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   127
                throw new RuntimeException("Detected sharing of AccessibleObject arrays");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   128
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   129
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   130
    // Validate that annotations are shared
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   131
    private static void validateAnnotationSharing(AccessibleObject m1, AccessibleObject m2) {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   132
        Bar b1 = m1.getAnnotation(Bar.class);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   133
        Bar b2 = m2.getAnnotation(Bar.class);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   134
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   135
        if (b1 != b2)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   136
            throw new RuntimeException(b1 + " and " + b2 + " should be ==");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   137
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   138
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   139
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   140
    // Validate that Method instances representing the annotation elements
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   141
    // behave as intended
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   142
    private static void validateAnnotationSharingIsSafe(AccessibleObject m1, AccessibleObject m2)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   143
            throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   144
        Bar b1 = m1.getAnnotation(Bar.class);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   145
        Bar b2 = m2.getAnnotation(Bar.class);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   146
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   147
        Method mm1 = b1.annotationType().getMethod("value", (Class<?>[]) null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   148
        Method mm2 = b2.annotationType().getMethod("value", (Class<?>[]) null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   149
        inner(mm1, mm2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   150
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   151
        mm1 = b1.getClass().getMethod("value", (Class<?>[]) null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   152
        mm2 = b2.getClass().getMethod("value", (Class<?>[]) null);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   153
        inner(mm1, mm2);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   154
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   155
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   156
    private static void inner(Method mm1, Method mm2)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   157
            throws Exception {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   158
        if (!mm1.equals(mm2))
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   159
            throw new RuntimeException(mm1 + " and " + mm2 + " should be equal()");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   160
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   161
        mm1.setAccessible(false);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   162
        mm2.setAccessible(true);
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   163
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   164
        if (mm1.isAccessible())
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   165
            throw new RuntimeException(mm1 + " should not be accessible");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   166
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   167
        if (!mm2.isAccessible())
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   168
            throw new RuntimeException(mm2 + " should be accessible");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   169
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   170
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   171
    // Validate that array element values are not shared
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   172
    private static void validateArrayValues(Baz a, Baz b) {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   173
        String[] s1 = a.value();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   174
        String[] s2 = b.value();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   175
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   176
        s1[0] = "22";
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   177
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   178
        if (!s2[0].equals("1"))
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   179
            throw new RuntimeException("Mutation of array elements should not be detectable");
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   180
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   181
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   182
    @Foo @Bar("val") @Baz({"1", "2"})
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   183
    public void m() {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   184
        return ;
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   185
    }
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   186
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   187
    @Foo @Bar("someValue") @Baz({"1", "22", "33"})
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   188
    public Object f = new Object();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   189
}
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   190
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   191
@Retention(RetentionPolicy.RUNTIME)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   192
@interface Foo {}
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   193
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   194
@Retention(RetentionPolicy.RUNTIME)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   195
@interface Bar {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   196
    String value();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   197
}
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   198
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   199
@Retention(RetentionPolicy.RUNTIME)
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   200
@interface Baz {
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   201
    String [] value();
195a6f3e0cd0 8054987: (reflect) Add sharing of annotations between instances of Executable
jfranck
parents:
diff changeset
   202
}