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

COVERAGE SUMMARY FOR SOURCE FILE [DefaultConnectionFactoryCustomizer.java]

nameclass, %method, %block, %line, %
DefaultConnectionFactoryCustomizer.java100% (1/1)100% (5/5)72%  (86/120)75%  (25.6/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultConnectionFactoryCustomizer100% (1/1)100% (5/5)72%  (86/120)75%  (25.6/34)
emitSetConnection (Class, Class, ClassEmitter): void 100% (1/1)53%  (27/51)72%  (8.6/12)
methodExists (Class, String, Class []): boolean 100% (1/1)66%  (19/29)55%  (6/11)
DefaultConnectionFactoryCustomizer (): void 100% (1/1)100% (3/3)100% (1/1)
emitFields (Class, Class, ClassEmitter): void 100% (1/1)100% (14/14)100% (3/3)
emitGetConnection (Class, Class, ClassEmitter): void 100% (1/1)100% (23/23)100% (7/7)

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.customizer;
20 
21import static sf.qof.codegen.Constants.SIG_getConnection;
22import static sf.qof.codegen.Constants.SIG_setConnection;
23import static sf.qof.codegen.Constants.TYPE_Connection;
24import static sf.qof.codegen.Constants.TYPE_RuntimeException;
25 
26import java.lang.reflect.Method;
27import java.lang.reflect.Modifier;
28 
29import net.sf.cglib.core.ClassEmitter;
30import net.sf.cglib.core.CodeEmitter;
31import net.sf.cglib.core.Constants;
32 
33/**
34 * Provides the default implementation of a ConnectionFactoryCustomizer.
35 * 
36 * This customizer implements getConnection() and setConnection() methods and
37 * a private field to store it if the super class does not have a <code>getConnection()</code>
38 * method defined. 
39 * 
40 * @see ConnectionFactoryCustomizer
41 */
42public class DefaultConnectionFactoryCustomizer implements ConnectionFactoryCustomizer {
43 
44        private static final String FIELD_NAME_CONNECTION = "$connection";
45 
46        public void emitFields(Class<?>  queryDefinitionClass, Class<?> superClass, ClassEmitter ce) {
47          if (!methodExists(superClass, "getConnection", null)) {
48            ce.declare_field(Constants.ACC_PRIVATE, FIELD_NAME_CONNECTION, TYPE_Connection, null, null);
49          }
50        }
51 
52        public void emitGetConnection(Class<?>  queryDefinitionClass, Class<?> superClass, ClassEmitter ce) {
53          if (!methodExists(superClass, "getConnection", null)) {
54                  CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_getConnection, null, null);
55                  co.load_this();
56                  co.getfield(FIELD_NAME_CONNECTION);
57                  co.return_value();
58                  co.end_method();
59          }
60        }
61 
62        public void emitSetConnection(Class<?>  queryDefinitionClass, Class<?> superClass, ClassEmitter ce) {
63          if (!methodExists(superClass, "getConnection", null)) {
64                  CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_setConnection, null, null);
65                  co.load_this();
66                  co.load_arg(0);
67                  co.putfield(FIELD_NAME_CONNECTION);
68                  co.return_value();
69                  co.end_method();
70          } else if (!methodExists(superClass, "setConnection", new Class<?>[] {java.sql.Connection.class})) {
71            CodeEmitter co = ce.begin_method(Constants.ACC_PUBLIC, SIG_setConnection, null, null);
72            co.throw_exception(TYPE_RuntimeException, "Connection cannot be set");
73            co.end_method();
74          }
75        }
76 
77        private boolean methodExists(Class<?> superClass, String methodName, Class<?>[] parameterTypes) {
78          try {
79      Method method = superClass.getMethod(methodName, parameterTypes);
80      int modifiers = method.getModifiers();
81      if (Modifier.isPrivate(modifiers) || Modifier.isAbstract(modifiers)) {
82        return false;
83      }
84      if (method.getReturnType() != java.sql.Connection.class) {
85        return false;
86      }
87      return true;
88    } catch (SecurityException e) {
89    } catch (NoSuchMethodException e) {
90    }
91          return false;
92        }
93}

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