1 /* 2 * Copyright (c) 2005, 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 6331574 27 * @summary test isModifiableClass 28 * @author Robert Field, Sun Microsystems 29 * 30 * @modules java.instrument 31 * @run build IsModifiableClassApp IsModifiableClassAgent 32 * @run shell MakeJAR3.sh IsModifiableClassAgent 'Can-Retransform-Classes: true' 33 * @run main/othervm -javaagent:IsModifiableClassAgent.jar IsModifiableClassApp 34 */ 35 import java.lang.instrument.*; 36 37 public class IsModifiableClassAgent 38 { 39 public static boolean fail = false; 40 public static boolean completed = false; 41 42 public static void 43 premain( String agentArgs, 44 Instrumentation instrumentation) 45 { 46 System.out.println("IsModifiableClassAgent started"); 47 48 Class[] allClasses = instrumentation.getAllLoadedClasses(); 49 int modCount = 0; 50 int unmodCount = 0; 51 52 for (int i = 0; i < allClasses.length; i++) 53 { 54 Class klass = allClasses[i]; 55 boolean isMod = instrumentation.isModifiableClass(klass); 56 if (isMod && klass.isArray()) { 57 System.err.println("Error: array class returned as modifiable: " + klass); 58 fail = true; 59 } 60 if (isMod && klass.isPrimitive()) { 61 System.err.println("Error: primitive class returned as modifiable: " + klass); 62 fail = true; 63 } 64 try { 65 instrumentation.retransformClasses(klass); 66 if (!isMod) { 67 System.err.println("Error: unmodifiable class retransformable: " + klass); 68 fail = true; 69 } 70 } catch (UnmodifiableClassException e) { 71 if (isMod) { 72 System.err.println("Error: modifiable class not retransformable: " + klass); 73 System.err.println(" exception: " + e); 74 fail = true; 75 } 76 } catch (Throwable e) { 77 System.err.println("Error: bad return from retransform: " + klass); 78 System.err.println(" ERROR: " + e); 79 fail = true; 80 } 81 if (isMod) { 82 ++modCount; 83 } else { 84 ++unmodCount; 85 } 86 } 87 System.out.println("modifiable: " + modCount + ". unmodifiable: " + unmodCount); 88 completed = true; 89 } 90 }