jdk/test/java/lang/instrument/RedefineMethodWithAnnotationsApp.java
author stefank
Tue, 22 Jan 2013 15:25:37 +0100
changeset 16022 3f1545eccc6f
permissions -rw-r--r--
7140852: Add test for 7022100 Reviewed-by: sspitsyn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16022
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     1
/*
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     4
 *
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     8
 *
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    13
 * accompanied this code).
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    14
 *
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    18
 *
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    21
 * questions.
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    22
 */
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    23
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    24
import java.io.File;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    25
import java.io.FileInputStream;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    26
import java.io.InputStream;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    27
import java.lang.annotation.Annotation;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    28
import java.lang.instrument.ClassDefinition;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    29
import java.lang.annotation.Retention;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    30
import java.lang.annotation.RetentionPolicy;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    31
import java.lang.reflect.Method;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    32
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    33
public class RedefineMethodWithAnnotationsApp {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    34
    public static void main(String args[]) throws Exception {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    35
        System.out.println("Hello from RedefineMethodWithAnnotationsApp!");
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    36
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    37
        new RedefineMethodWithAnnotationsApp().doTest();
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    38
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    39
        System.exit(0);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    40
    }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    41
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    42
    private void doTest() throws Exception {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    43
        doMethodParameterAnnotationsTest();
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    44
    }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    45
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    46
    private void doMethodParameterAnnotationsTest() throws Exception {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    47
        verifyMethodParameterAnnotationsValue(
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    48
                ParameterAnnotation.STRING_VALUE_1);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    49
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    50
        doRedefine(RedefineMethodWithAnnotationsTarget.class);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    51
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    52
        verifyMethodParameterAnnotationsValue(
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    53
                ParameterAnnotation.STRING_VALUE_2);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    54
    }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    55
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    56
    private static void verifyMethodParameterAnnotationsValue(
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    57
            String expectedValue) throws Exception {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    58
        Class<RedefineMethodWithAnnotationsTarget> c =
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    59
                RedefineMethodWithAnnotationsTarget.class;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    60
        Method method = c.getMethod("annotatedMethod", String.class);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    61
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    62
        Annotation [][] parametersAnnotations =
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    63
                method.getParameterAnnotations();
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    64
        if (parametersAnnotations.length != 1) {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    65
            throw new Exception("Incorrect number of parameters to method: " +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    66
                    method.getName() + "." +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    67
                    " Expected: 1," +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    68
                    " got: " + parametersAnnotations.length);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    69
        }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    70
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    71
        Annotation[] parameterAnnotations = parametersAnnotations[0];
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    72
        if (parameterAnnotations.length != 1) {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    73
            throw new Exception("Incorrect number of annotations." +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    74
                    " Expected: 1" +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    75
                    ", got " + parameterAnnotations.length);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    76
        }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    77
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    78
        Annotation parameterAnnotation = parameterAnnotations[0];
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    79
        if (!(parameterAnnotation instanceof ParameterAnnotation)) {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    80
            throw new Exception("Incorrect Annotation class." +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    81
                    " Expected: " + ParameterAnnotation.class.getName() +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    82
                    ", got: " + parameterAnnotation.getClass().getName());
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    83
        }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    84
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    85
        ParameterAnnotation pa = (ParameterAnnotation)parameterAnnotation;
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    86
        String annotationValue = pa.value();
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    87
        if (!expectedValue.equals(annotationValue)) {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    88
            throw new Exception("Incorrect parameter annotation value." +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    89
                    " Expected: " + expectedValue +
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    90
                    ", got: " + annotationValue);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    91
        }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    92
    }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    93
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    94
    private static void doRedefine(Class<?> clazz) throws Exception {
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    95
        // Load the second version of this class.
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    96
        File f = new File(clazz.getName() + ".class");
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    97
        System.out.println("Reading test class from " + f);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    98
        InputStream redefineStream = new FileInputStream(f);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
    99
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   100
        byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   101
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   102
        ClassDefinition redefineParamBlock = new ClassDefinition(
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   103
                clazz, redefineBuffer);
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   104
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   105
        RedefineMethodWithAnnotationsAgent.getInstrumentation().redefineClasses(
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   106
                new ClassDefinition[] {redefineParamBlock});
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   107
    }
3f1545eccc6f 7140852: Add test for 7022100
stefank
parents:
diff changeset
   108
}