001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.instrumentation; 020 021import java.lang.instrument.ClassDefinition; 022import java.lang.instrument.Instrumentation; 023import java.lang.instrument.UnmodifiableClassException; 024 025import lucee.commons.lang.ExceptionUtil; 026 027public class InstrumentationUtil { 028 029 /** 030 * redefine the class with the given byte array 031 * @param clazz 032 * @param barr 033 * @return 034 */ 035 public static boolean redefineClassEL(Class clazz, byte[] barr){ 036 Instrumentation inst = InstrumentationFactory.getInstance(); 037 if(inst!=null && inst.isRedefineClassesSupported()) { 038 try { 039 inst.redefineClasses(new ClassDefinition(clazz,barr)); 040 return true; 041 } 042 catch (Throwable t) { 043 ExceptionUtil.rethrowIfNecessary(t); 044 t.printStackTrace(); 045 } 046 } 047 return false; 048 } 049 050 public static void redefineClass(Class clazz, byte[] barr) throws ClassNotFoundException, UnmodifiableClassException{ 051 Instrumentation inst = InstrumentationFactory.getInstance(); 052 inst.redefineClasses(new ClassDefinition(clazz,barr)); 053 } 054 055 public static boolean isSupported() { 056 Instrumentation inst = InstrumentationFactory.getInstance(); 057 return (inst!=null && inst.isRedefineClassesSupported()); 058 } 059}