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