EMMA Coverage Report (generated Sat Nov 03 21:53:04 GMT 2007)
[all classes][sf.qof.util]

COVERAGE SUMMARY FOR SOURCE FILE [DefineClassHelper.java]

nameclass, %method, %block, %line, %
DefineClassHelper.java100% (3/3)89%  (8/9)87%  (99/114)73%  (11.7/16)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefineClassHelper$2100% (1/1)100% (2/2)76%  (39/51)60%  (6/10)
run (): Object 100% (1/1)75%  (36/48)56%  (5/9)
DefineClassHelper$2 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class DefineClassHelper100% (1/1)80%  (4/5)95%  (54/57)94%  (5.7/6)
DefineClassHelper (): void 0%   (0/1)0%   (0/3)0%   (0/1)
<static initializer> 100% (1/1)100% (12/12)100% (3/3)
access$000 (): Method 100% (1/1)100% (2/2)100% (1/1)
access$002 (Method): Method 100% (1/1)100% (4/4)100% (1/1)
defineClass (String, byte [], ClassLoader): Class 100% (1/1)100% (36/36)100% (2/2)
     
class DefineClassHelper$1100% (1/1)100% (2/2)100% (6/6)100% (2/2)
DefineClassHelper$1 (): void 100% (1/1)100% (3/3)100% (1/1)
run (): Object 100% (1/1)100% (3/3)100% (1/1)

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 */
19package sf.qof.util;
20 
21import java.lang.reflect.Method;
22import java.security.AccessController;
23import java.security.PrivilegedAction;
24import java.security.ProtectionDomain;
25 
26import net.sf.cglib.core.CodeGenerationException;
27import sf.qof.codegen.QueryObjectGenerator;
28 
29/**
30 * Helper class to call the defineClass method of the ClassLoader.
31 * 
32 * @see java.lang.ClassLoader
33 */
34public 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}

[all classes][sf.qof.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov