hotspot/test/runtime/RedefineTests/RedefineAnnotations.java
author jprovino
Wed, 11 Nov 2015 23:47:42 +0000
changeset 33911 af2cff5bd523
parent 30604 b8d532cb6420
child 38152 80e5da81fb2c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27401
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     1
/*
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27401
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27401
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     4
 *
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     8
 *
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    13
 * accompanied this code).
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    14
 *
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    18
 *
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    21
 * questions.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    22
 */
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    23
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    24
/*
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    25
 * @test
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    26
 * @library /testlibrary
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    27
 * @summary Test that type annotations are retained after a retransform
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27401
diff changeset
    28
 * @modules java.base/jdk.internal.org.objectweb.asm
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27401
diff changeset
    29
 *          java.instrument
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27401
diff changeset
    30
 *          jdk.jartool/sun.tools.jar
27401
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    31
 * @run main RedefineAnnotations buildagent
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    32
 * @run main/othervm -javaagent:redefineagent.jar RedefineAnnotations
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    33
 */
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    34
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 29678
diff changeset
    35
import static jdk.test.lib.Asserts.assertTrue;
27401
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    36
import java.io.FileNotFoundException;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    37
import java.io.PrintWriter;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    38
import java.lang.NoSuchFieldException;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    39
import java.lang.NoSuchMethodException;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    40
import java.lang.RuntimeException;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    41
import java.lang.annotation.Annotation;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    42
import java.lang.annotation.ElementType;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    43
import java.lang.annotation.Retention;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    44
import java.lang.annotation.RetentionPolicy;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    45
import java.lang.annotation.Target;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    46
import java.lang.instrument.ClassFileTransformer;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    47
import java.lang.instrument.IllegalClassFormatException;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    48
import java.lang.instrument.Instrumentation;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    49
import java.lang.instrument.UnmodifiableClassException;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    50
import java.lang.reflect.AnnotatedArrayType;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    51
import java.lang.reflect.AnnotatedParameterizedType;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    52
import java.lang.reflect.AnnotatedType;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    53
import java.lang.reflect.AnnotatedWildcardType;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    54
import java.lang.reflect.Executable;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    55
import java.lang.reflect.TypeVariable;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    56
import java.security.ProtectionDomain;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    57
import java.util.Arrays;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    58
import java.util.LinkedList;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    59
import java.util.List;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    60
import java.util.Map;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    61
import jdk.internal.org.objectweb.asm.ClassReader;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    62
import jdk.internal.org.objectweb.asm.ClassVisitor;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    63
import jdk.internal.org.objectweb.asm.ClassWriter;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    64
import jdk.internal.org.objectweb.asm.FieldVisitor;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    65
import static jdk.internal.org.objectweb.asm.Opcodes.ASM5;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    66
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    67
@Retention(RetentionPolicy.RUNTIME)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    68
@Target(ElementType.TYPE_USE)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    69
@interface TestAnn {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    70
    String site();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    71
}
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    72
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    73
public class RedefineAnnotations {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    74
    static Instrumentation inst;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    75
    public static void premain(String agentArgs, Instrumentation inst) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    76
        RedefineAnnotations.inst = inst;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    77
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    78
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    79
    static class Transformer implements ClassFileTransformer {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    80
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    81
        public byte[] asm(ClassLoader loader, String className,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    82
                Class<?> classBeingRedefined,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    83
                ProtectionDomain protectionDomain, byte[] classfileBuffer)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    84
            throws IllegalClassFormatException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    85
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    86
            ClassWriter cw = new ClassWriter(0);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    87
            ClassVisitor cv = new ReAddDummyFieldsClassVisitor(ASM5, cw) { };
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    88
            ClassReader cr = new ClassReader(classfileBuffer);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    89
            cr.accept(cv, 0);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    90
            return cw.toByteArray();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    91
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    92
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    93
        public class ReAddDummyFieldsClassVisitor extends ClassVisitor {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    94
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    95
            LinkedList<F> fields = new LinkedList<>();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    96
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    97
            public ReAddDummyFieldsClassVisitor(int api, ClassVisitor cv) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    98
                super(api, cv);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
    99
            }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   100
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   101
            @Override public FieldVisitor visitField(int access, String name,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   102
                    String desc, String signature, Object value) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   103
                if (name.startsWith("dummy")) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   104
                    // Remove dummy field
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   105
                    fields.addLast(new F(access, name, desc, signature, value));
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   106
                    return null;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   107
                }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   108
                return cv.visitField(access, name, desc, signature, value);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   109
            }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   110
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   111
            @Override public void visitEnd() {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   112
                F f;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   113
                while ((f = fields.pollFirst()) != null) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   114
                    // Re-add dummy fields
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   115
                    cv.visitField(f.access, f.name, f.desc, f.signature, f.value);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   116
                }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   117
            }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   118
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   119
            private class F {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   120
                private int access;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   121
                private String name;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   122
                private String desc;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   123
                private String signature;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   124
                private Object value;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   125
                F(int access, String name, String desc, String signature, Object value) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   126
                    this.access = access;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   127
                    this.name = name;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   128
                    this.desc = desc;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   129
                    this.signature = signature;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   130
                    this.value = value;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   131
                }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   132
            }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   133
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   134
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   135
        @Override public byte[] transform(ClassLoader loader, String className,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   136
                Class<?> classBeingRedefined,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   137
                ProtectionDomain protectionDomain, byte[] classfileBuffer)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   138
            throws IllegalClassFormatException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   139
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   140
            if (className.contains("TypeAnnotatedTestClass")) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   141
                try {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   142
                    // Here we remove and re-add the dummy fields. This shuffles the constant pool
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   143
                    return asm(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   144
                } catch (Throwable e) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   145
                    // The retransform native code that called this method does not propagate
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   146
                    // exceptions. Instead of getting an uninformative generic error, catch
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   147
                    // problems here and print it, then exit.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   148
                    e.printStackTrace();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   149
                    System.exit(1);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   150
                }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   151
            }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   152
            return null;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   153
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   154
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   155
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   156
    private static void buildAgent() {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   157
        try {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   158
            ClassFileInstaller.main("RedefineAnnotations");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   159
        } catch (Exception e) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   160
            throw new RuntimeException("Could not write agent classfile", e);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   161
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   162
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   163
        try {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   164
            PrintWriter pw = new PrintWriter("MANIFEST.MF");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   165
            pw.println("Premain-Class: RedefineAnnotations");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   166
            pw.println("Agent-Class: RedefineAnnotations");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   167
            pw.println("Can-Retransform-Classes: true");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   168
            pw.close();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   169
        } catch (FileNotFoundException e) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   170
            throw new RuntimeException("Could not write manifest file for the agent", e);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   171
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   172
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   173
        sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   174
        if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineAnnotations.class" })) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   175
            throw new RuntimeException("Could not write the agent jar file");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   176
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   177
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   178
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   179
    public static void main(String argv[]) throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   180
        if (argv.length == 1 && argv[0].equals("buildagent")) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   181
            buildAgent();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   182
            return;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   183
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   184
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   185
        if (inst == null) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   186
            throw new RuntimeException("Instrumentation object was null");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   187
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   188
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   189
        RedefineAnnotations test = new RedefineAnnotations();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   190
        test.testTransformAndVerify();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   191
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   192
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   193
    // Class type annotations
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   194
    private Annotation classTypeParameterTA;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   195
    private Annotation extendsTA;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   196
    private Annotation implementsTA;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   197
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   198
    // Field type annotations
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   199
    private Annotation fieldTA;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   200
    private Annotation innerTA;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   201
    private Annotation[] arrayTA = new Annotation[4];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   202
    private Annotation[] mapTA = new Annotation[5];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   203
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   204
    // Method type annotations
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   205
    private Annotation returnTA, methodTypeParameterTA, formalParameterTA, throwsTA;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   206
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   207
    private void testTransformAndVerify()
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   208
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   209
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   210
        Class<TypeAnnotatedTestClass> c = TypeAnnotatedTestClass.class;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   211
        Class<?> myClass = c;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   212
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   213
        /*
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   214
         * Verify that the expected annotations are where they should be before transform.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   215
         */
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   216
        verifyClassTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   217
        verifyFieldTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   218
        verifyMethodTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   219
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   220
        try {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   221
            inst.addTransformer(new Transformer(), true);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   222
            inst.retransformClasses(myClass);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   223
        } catch (UnmodifiableClassException e) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   224
            throw new RuntimeException(e);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   225
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   226
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   227
        /*
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   228
         * Verify that the expected annotations are where they should be after transform.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   229
         * Also verify that before and after are equal.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   230
         */
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   231
        verifyClassTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   232
        verifyFieldTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   233
        verifyMethodTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   234
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   235
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   236
    private void verifyClassTypeAnnotations(Class c) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   237
        Annotation anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   238
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   239
        anno = c.getTypeParameters()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   240
        verifyTestAnn(classTypeParameterTA, anno, "classTypeParameter");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   241
        classTypeParameterTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   242
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   243
        anno = c.getAnnotatedSuperclass().getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   244
        verifyTestAnn(extendsTA, anno, "extends");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   245
        extendsTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   246
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   247
        anno = c.getAnnotatedInterfaces()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   248
        verifyTestAnn(implementsTA, anno, "implements");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   249
        implementsTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   250
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   251
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   252
    private void verifyFieldTypeAnnotations(Class c)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   253
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   254
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   255
        verifyBasicFieldTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   256
        verifyInnerFieldTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   257
        verifyArrayFieldTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   258
        verifyMapFieldTypeAnnotations(c);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   259
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   260
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   261
    private void verifyBasicFieldTypeAnnotations(Class c)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   262
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   263
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   264
        Annotation anno = c.getDeclaredField("typeAnnotatedBoolean").getAnnotatedType().getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   265
        verifyTestAnn(fieldTA, anno, "field");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   266
        fieldTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   267
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   268
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   269
    private void verifyInnerFieldTypeAnnotations(Class c)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   270
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   271
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   272
        AnnotatedType at = c.getDeclaredField("typeAnnotatedInner").getAnnotatedType();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   273
        Annotation anno = at.getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   274
        verifyTestAnn(innerTA, anno, "inner");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   275
        innerTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   276
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   277
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   278
    private void verifyArrayFieldTypeAnnotations(Class c)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   279
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   280
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   281
        Annotation anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   282
        AnnotatedType at;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   283
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   284
        at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   285
        anno = at.getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   286
        verifyTestAnn(arrayTA[0], anno, "array1");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   287
        arrayTA[0] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   288
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   289
        for (int i = 1; i <= 3; i++) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   290
            at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   291
            anno = at.getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   292
            verifyTestAnn(arrayTA[i], anno, "array" + (i + 1));
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   293
            arrayTA[i] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   294
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   295
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   296
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   297
    private void verifyMapFieldTypeAnnotations(Class c)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   298
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   299
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   300
        Annotation anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   301
        AnnotatedType atBase;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   302
        AnnotatedType atParameter;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   303
        atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   304
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   305
        anno = atBase.getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   306
        verifyTestAnn(mapTA[0], anno, "map1");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   307
        mapTA[0] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   308
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   309
        atParameter =
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   310
            ((AnnotatedParameterizedType) atBase).
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   311
            getAnnotatedActualTypeArguments()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   312
        anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   313
        verifyTestAnn(mapTA[1], anno, "map2");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   314
        mapTA[1] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   315
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   316
        anno =
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   317
            ((AnnotatedWildcardType) atParameter).
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   318
            getAnnotatedUpperBounds()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   319
        verifyTestAnn(mapTA[2], anno, "map3");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   320
        mapTA[2] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   321
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   322
        atParameter =
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   323
            ((AnnotatedParameterizedType) atBase).
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   324
            getAnnotatedActualTypeArguments()[1];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   325
        anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   326
        verifyTestAnn(mapTA[3], anno, "map4");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   327
        mapTA[3] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   328
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   329
        anno =
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   330
            ((AnnotatedParameterizedType) atParameter).
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   331
            getAnnotatedActualTypeArguments()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   332
        verifyTestAnn(mapTA[4], anno, "map5");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   333
        mapTA[4] = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   334
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   335
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   336
    private void verifyMethodTypeAnnotations(Class c)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   337
        throws NoSuchFieldException, NoSuchMethodException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   338
        Annotation anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   339
        Executable typeAnnotatedMethod =
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   340
            c.getDeclaredMethod("typeAnnotatedMethod", TypeAnnotatedTestClass.class);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   341
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   342
        anno = typeAnnotatedMethod.getAnnotatedReturnType().getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   343
        verifyTestAnn(returnTA, anno, "return");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   344
        returnTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   345
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   346
        anno = typeAnnotatedMethod.getTypeParameters()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   347
        verifyTestAnn(methodTypeParameterTA, anno, "methodTypeParameter");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   348
        methodTypeParameterTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   349
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   350
        anno = typeAnnotatedMethod.getAnnotatedParameterTypes()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   351
        verifyTestAnn(formalParameterTA, anno, "formalParameter");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   352
        formalParameterTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   353
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   354
        anno = typeAnnotatedMethod.getAnnotatedExceptionTypes()[0].getAnnotations()[0];
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   355
        verifyTestAnn(throwsTA, anno, "throws");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   356
        throwsTA = anno;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   357
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   358
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   359
    private static void verifyTestAnn(Annotation verifyAgainst, Annotation anno, String expectedSite) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   360
        verifyTestAnnSite(anno, expectedSite);
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   361
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   362
        // When called before transform verifyAgainst will be null, when called
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   363
        // after transform it will be the annotation from before the transform
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   364
        if (verifyAgainst != null) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   365
            assertTrue(anno.equals(verifyAgainst),
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   366
                       "Annotations do not match before and after." +
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   367
                       " Before: \"" + verifyAgainst + "\", After: \"" + anno + "\"");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   368
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   369
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   370
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   371
    private static void verifyTestAnnSite(Annotation testAnn, String expectedSite) {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   372
        String expectedAnn = "@TestAnn(site=" + expectedSite + ")";
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   373
        assertTrue(testAnn.toString().equals(expectedAnn),
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   374
                   "Expected \"" + expectedAnn + "\", got \"" + testAnn + "\"");
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   375
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   376
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   377
    public static class TypeAnnotatedTestClass <@TestAnn(site="classTypeParameter") S,T>
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   378
            extends @TestAnn(site="extends") Thread
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   379
            implements @TestAnn(site="implements") Runnable {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   380
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   381
        public @TestAnn(site="field") boolean typeAnnotatedBoolean;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   382
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   383
        public
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   384
            RedefineAnnotations.
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   385
            @TestAnn(site="inner") TypeAnnotatedTestClass
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   386
            typeAnnotatedInner;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   387
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   388
        public
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   389
            @TestAnn(site="array4") boolean
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   390
            @TestAnn(site="array1") []
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   391
            @TestAnn(site="array2") []
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   392
            @TestAnn(site="array3") []
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   393
            typeAnnotatedArray;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   394
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   395
        public @TestAnn(site="map1") Map
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   396
            <@TestAnn(site="map2") ? extends @TestAnn(site="map3") String,
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   397
            @TestAnn(site="map4")  List<@TestAnn(site="map5")  Object>> typeAnnotatedMap;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   398
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   399
        public int dummy1;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   400
        public int dummy2;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   401
        public int dummy3;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   402
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   403
        @TestAnn(site="return") <@TestAnn(site="methodTypeParameter") U,V> Class
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   404
            typeAnnotatedMethod(@TestAnn(site="formalParameter") TypeAnnotatedTestClass arg)
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   405
            throws @TestAnn(site="throws") ClassNotFoundException {
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   406
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   407
            @TestAnn(site="local_variable_type") int foo = 0;
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   408
            throw new ClassNotFoundException();
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   409
        }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   410
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   411
        public void run() {}
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   412
    }
a4697c9502cb 8057043: Type annotations not retained during class redefine / retransform
aeriksso
parents:
diff changeset
   413
}