1 /* 2 * Copyright (c) 2003, 2015, 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 /** 25 * @test 26 * @bug 4882798 5067523 27 * @summary insure redefine is supported. exercise a class, then redefine it and do it again 28 * @author Gabriel Adauto, Wily Technology 29 * 30 * @modules java.instrument 31 * @run build RedefineClassesTests 32 * @run shell RedefineSetUp.sh 33 * @run shell MakeJAR.sh redefineAgent 34 * @run main/othervm -javaagent:redefineAgent.jar RedefineClassesTests RedefineClassesTests 35 */ 36 37 import java.io.*; 38 import java.lang.instrument.*; 39 import java.lang.reflect.*; 40 public class 41 RedefineClassesTests 42 extends ASimpleInstrumentationTestCase 43 { 44 45 /** 46 * Constructor for RedefineClassesTests. 47 * @param name 48 */ 49 public RedefineClassesTests(String name) 50 { 51 super(name); 52 } 53 54 public static void 55 main (String[] args) 56 throws Throwable { 57 ATestCaseScaffold test = new RedefineClassesTests(args[0]); 58 test.runTest(); 59 } 60 61 protected final void 62 doRunTest() 63 throws Throwable { 64 testIsRedefineClassesSupported(); 65 testSimpleRedefineClasses(); 66 testUnmodifiableClassException(); 67 } 68 69 70 public void 71 testIsRedefineClassesSupported() 72 { 73 boolean canRedef = fInst.isRedefineClassesSupported(); 74 assertTrue("Cannot redefine classes", canRedef); 75 } 76 77 public void 78 testSimpleRedefineClasses() 79 throws Throwable 80 { 81 // first load the class and prove that it is the right one 82 ExampleRedefine ex = new ExampleRedefine(); 83 84 // with this version of the class, doSomething is a nop 85 int firstGet = ex.get(); 86 ex.doSomething(); 87 int secondGet = ex.get(); 88 89 assertEquals(firstGet, secondGet); 90 91 // now redefine the class. This will change doSomething to be an increment 92 93 // this class is stored in a different place (scratch directory) to avoid collisions 94 File f = new File("Different_ExampleRedefine.class"); 95 System.out.println("Reading test class from " + f); 96 InputStream redefineStream = new FileInputStream(f); 97 98 byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); 99 100 ClassDefinition redefineParamBlock = new ClassDefinition( ExampleRedefine.class, 101 redefineBuffer); 102 103 fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock}); 104 105 int thirdGet = ex.get(); 106 ex.doSomething(); 107 int fourthGet = ex.get(); 108 assertEquals(thirdGet + 1, fourthGet); 109 } 110 111 public void 112 testUnmodifiableClassException() 113 throws Throwable 114 { 115 System.out.println("Testing UnmodifiableClassException"); 116 117 // Load any class 118 File f = new File("Different_ExampleRedefine.class"); 119 InputStream redefineStream = new FileInputStream(f); 120 byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); 121 122 System.out.println("Try to redefine class for primitive type"); 123 try { 124 ClassDefinition redefineParamBlock = 125 new ClassDefinition( byte.class, redefineBuffer ); 126 fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock}); 127 fail(); 128 } catch (UnmodifiableClassException x) { 129 } 130 131 System.out.println("Try to redefine class for array type"); 132 try { 133 ClassDefinition redefineParamBlock = 134 new ClassDefinition( byte[].class, redefineBuffer ); 135 fInst.redefineClasses(new ClassDefinition[] {redefineParamBlock}); 136 fail(); 137 } catch (UnmodifiableClassException x) { 138 } 139 140 } 141 142 }