| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.util; |
| 20 | |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.security.AccessController; |
| 23 | import java.security.PrivilegedAction; |
| 24 | import java.security.ProtectionDomain; |
| 25 | |
| 26 | import net.sf.cglib.core.CodeGenerationException; |
| 27 | import sf.qof.codegen.QueryObjectGenerator; |
| 28 | |
| 29 | /** |
| 30 | * Helper class to call the defineClass method of the ClassLoader. |
| 31 | * |
| 32 | * @see java.lang.ClassLoader |
| 33 | */ |
| 34 | public class DefineClassHelper { |
| 35 | |
| 36 | private static Method DEFINE_CLASS; |
| 37 | |
| 38 | private static final ProtectionDomain PROTECTION_DOMAIN; |
| 39 | |
| 40 | static { |
| 41 | PROTECTION_DOMAIN = (ProtectionDomain) AccessController.doPrivileged(new PrivilegedAction<Object>() { |
| 42 | public Object run() { |
| 43 | return QueryObjectGenerator.class.getProtectionDomain(); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | AccessController.doPrivileged(new PrivilegedAction<Object>() { |
| 48 | public Object run() { |
| 49 | try { |
| 50 | Class<?> loader = Class.forName("java.lang.ClassLoader"); // JVM crash w/o this |
| 51 | DEFINE_CLASS = loader.getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class, |
| 52 | Integer.TYPE, Integer.TYPE, ProtectionDomain.class }); |
| 53 | DEFINE_CLASS.setAccessible(true); |
| 54 | } catch (ClassNotFoundException e) { |
| 55 | throw new CodeGenerationException(e); |
| 56 | } catch (NoSuchMethodException e) { |
| 57 | throw new CodeGenerationException(e); |
| 58 | } |
| 59 | return null; |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Calls <code>ClassLoader.defineClass</code> and returns a <code>Class</code> |
| 66 | * instance if successful. |
| 67 | * |
| 68 | * @param <T> type of the class |
| 69 | * @param className class name |
| 70 | * @param byteCode array containing the byte code of the class |
| 71 | * @param loader class loader |
| 72 | * @return newly defined class |
| 73 | * @throws Exception error occurred |
| 74 | * |
| 75 | * @see java.lang.ClassLoader |
| 76 | */ |
| 77 | @SuppressWarnings("unchecked") |
| 78 | public static <T> Class<T> defineClass(String className, byte[] byteCode, ClassLoader loader) throws Exception { |
| 79 | Object[] args = new Object[] { className, byteCode, new Integer(0), new Integer(byteCode.length), |
| 80 | PROTECTION_DOMAIN }; |
| 81 | return (Class<T>) DEFINE_CLASS.invoke(loader, args); |
| 82 | } |
| 83 | |
| 84 | } |