|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Delegator | Line # 47 | 24 | 0% | 5 | 1 | 96.3% |
0.962963
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(8) | |||
Result | |||
0.9259259
|
org.perfectjpattern.core.extras.delegate.TestDelegator.testDelegator org.perfectjpattern.core.extras.delegate.TestDelegator.testDelegator | 1 PASS | |
0.5925926
|
org.perfectjpattern.core.behavioral.delegate.TestExample.testCommand org.perfectjpattern.core.behavioral.delegate.TestExample.testCommand | 1 PASS | |
0.5555556
|
org.perfectjpattern.jee.integration.dao.jpa.TestExample.testJpaCmpGenericDao org.perfectjpattern.jee.integration.dao.jpa.TestExample.testJpaCmpGenericDao | 1 PASS | |
0.5555556
|
org.perfectjpattern.jee.integration.dao.spring.TestExample.testSpringGenericDao org.perfectjpattern.jee.integration.dao.spring.TestExample.testSpringGenericDao | 1 PASS | |
0.5555556
|
org.perfectjpattern.core.behavioral.visitor.TestVisitor.testSimpleUsage org.perfectjpattern.core.behavioral.visitor.TestVisitor.testSimpleUsage | 1 PASS | |
0.5555556
|
org.perfectjpattern.core.behavioral.visitor.TestExample.testVisitor org.perfectjpattern.core.behavioral.visitor.TestExample.testVisitor | 1 PASS | |
0.5555556
|
org.perfectjpattern.jee.integration.dao.hibernate.TestExample.testHibernateGenericDao org.perfectjpattern.jee.integration.dao.hibernate.TestExample.testHibernateGenericDao | 1 PASS | |
0.18518518
|
org.perfectjpattern.core.extras.delegate.TestDelegator.testIsSuitableMethod org.perfectjpattern.core.extras.delegate.TestDelegator.testIsSuitableMethod | 1 PASS | |
1 | //---------------------------------------------------------------------- | |
2 | // | |
3 | // PerfectJPattern: "Design patterns are good but components are better!" | |
4 | // Delegator.java Copyright (c) 2009 Giovanni Azua Garcia | |
5 | // bravegag@hotmail.com | |
6 | // | |
7 | // This program is free software; you can redistribute it and/or | |
8 | // modify it under the terms of the GNU General Public License | |
9 | // as published by the Free Software Foundation; either version 3 | |
10 | // of the License, or (at your option) any later version. | |
11 | // | |
12 | // This program is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | // | |
17 | // You should have received a copy of the GNU General Public License | |
18 | // along with this program; if not, see <http://www.gnu.org/licenses/>. | |
19 | // | |
20 | //---------------------------------------------------------------------- | |
21 | package org.perfectjpattern.core.extras.delegate; | |
22 | ||
23 | import java.lang.reflect.*; | |
24 | ||
25 | import org.apache.commons.lang.*; | |
26 | import org.perfectjpattern.core.api.extras.delegate.*; | |
27 | ||
28 | ||
29 | /** | |
30 | * Implementation of <code>IDelegator</code> interface for creating | |
31 | * Delegate instances based user-defined interface types. | |
32 | * <br/><br/> | |
33 | * <b>Notes</b>: Base source code implemented by Steve Lewis and Wilhelm | |
34 | * Fitzpatrick and adapted to fit PerfectJPattern componentization | |
35 | * criteria and code conventions. | |
36 | * | |
37 | * @param <I> Interface type built by this <code>Delegator</code> instance. | |
38 | * | |
39 | * @author <a href="mailto:smlewis@lordjoe.com">Steve Lewis</a> | |
40 | * @author <a href="mailto:wilhelmf@agileinformatics.com">Wilhelm | |
41 | * Fitzpatrick</a> | |
42 | * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> | |
43 | * @version $Revision: 1.0 $ $Date: Jun 25, 2007 7:20:28 AM $ | |
44 | */ | |
45 | @SuppressWarnings("unchecked") | |
46 | public | |
47 | class Delegator<I> | |
48 | extends AbstractDelegator<I> | |
49 | { | |
50 | //------------------------------------------------------------------------ | |
51 | // public | |
52 | //------------------------------------------------------------------------ | |
53 | /** | |
54 | * Creates a <code>Delegator<E></code> from the type interface. | |
55 | * | |
56 | * @param anInterface The interface to build references from. | |
57 | * @throws IllegalArgumentException 'anInterface' must not be null. | |
58 | * @throws IllegalArgumentException 'anInterface' must be an interface type. | |
59 | */ | |
60 | 47 | public |
61 | Delegator(Class<I> anInterface) | |
62 | { | |
63 | 47 | super(anInterface); |
64 | ||
65 | 47 | Validate.notNull(anInterface, "'anInterface' must not be null"); |
66 | 47 | Validate.isTrue(anInterface.isInterface(), |
67 | "'anInterface' must be an interface type."); | |
68 | ||
69 | 47 | theInterface = anInterface; |
70 | } | |
71 | ||
72 | //------------------------------------------------------------------------ | |
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | 1 | @SuppressWarnings("unchecked") |
77 | public I | |
78 | build(Class aTarget, String aMethodName) | |
79 | throws IllegalArgumentException, UnsupportedOperationException | |
80 | { | |
81 | 1 | Validate.notNull(aTarget, "'aTarget' must not be null"); |
82 | 1 | Validate.notNull(aMethodName, "'aMethodName' must not be null"); |
83 | ||
84 | 1 | DelegateProxy myProxy = new DelegateProxy(null, aTarget, aMethodName, |
85 | this); | |
86 | ||
87 | 1 | Class myInterface = theInterface; |
88 | ||
89 | // build dynamic proxy | |
90 | 1 | Class[] myInterfaces = {myInterface, IDelegate.class }; |
91 | 1 | I myDelegate = null; |
92 | 1 | try |
93 | { | |
94 | // try with the target | |
95 | 1 | myDelegate = (I) Proxy.newProxyInstance(aTarget.getClassLoader(), |
96 | myInterfaces, myProxy); | |
97 | } | |
98 | catch (IllegalArgumentException anException) | |
99 | { | |
100 | // retry with the Interface class | |
101 | 0 | myDelegate = (I) Proxy.newProxyInstance(theInterface. |
102 | getClassLoader(), myInterfaces, myProxy); | |
103 | } | |
104 | ||
105 | 1 | return myDelegate; |
106 | } | |
107 | ||
108 | //------------------------------------------------------------------------ | |
109 | /** | |
110 | * {@inheritDoc} | |
111 | */ | |
112 | 40 | @SuppressWarnings("unchecked") |
113 | public I | |
114 | build(Object aTarget, String aMethodName) | |
115 | throws IllegalArgumentException, UnsupportedOperationException | |
116 | { | |
117 | 40 | Validate.notNull(aTarget, "'aTarget' must not be null"); |
118 | 40 | Validate.notNull(aMethodName, "'aMethodName' must not be null"); |
119 | ||
120 | 40 | DelegateProxy myProxy = new DelegateProxy(aTarget, |
121 | aTarget.getClass(), aMethodName, this); | |
122 | ||
123 | 40 | Class myInterface = theInterface; |
124 | ||
125 | // build dynamic proxy | |
126 | 40 | Class[] myInterfaces = {myInterface, IDelegate.class }; |
127 | ||
128 | 40 | IDelegate myDelegate = null; |
129 | 40 | try |
130 | { | |
131 | // try with the target | |
132 | 40 | myDelegate = (IDelegate) java.lang.reflect.Proxy.newProxyInstance( |
133 | aTarget.getClass().getClassLoader(), myInterfaces, myProxy); | |
134 | } | |
135 | catch (IllegalArgumentException anException) | |
136 | { | |
137 | // retry with the Interface class | |
138 | 3 | myDelegate = (IDelegate) java.lang.reflect.Proxy.newProxyInstance( |
139 | theInterface.getClassLoader(), myInterfaces, myProxy); | |
140 | } | |
141 | ||
142 | 40 | return (I) myDelegate; |
143 | } | |
144 | ||
145 | //------------------------------------------------------------------------ | |
146 | // members | |
147 | //------------------------------------------------------------------------ | |
148 | private final Class theInterface; | |
149 | } |
|