|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ImplementationHidingComponentFactory.java | 50% | 88.9% | 100% | 87.5% |
|
1 |
/*****************************************************************************
|
|
2 |
* Copyright (C) PicoContainer Organization. All rights reserved. *
|
|
3 |
* ------------------------------------------------------------------------- *
|
|
4 |
* The software in this package is published under the terms of the BSD *
|
|
5 |
* style license a copy of which has been included with this distribution in *
|
|
6 |
* the LICENSE.txt file. *
|
|
7 |
* *
|
|
8 |
* Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
|
|
9 |
*****************************************************************************/
|
|
10 |
|
|
11 |
package org.picocontainer.extras;
|
|
12 |
|
|
13 |
import org.picocontainer.internals.ComponentFactory;
|
|
14 |
import org.picocontainer.PicoInitializationException;
|
|
15 |
import org.picocontainer.PicoIntrospectionException;
|
|
16 |
import org.picocontainer.internals.ComponentSpecification;
|
|
17 |
|
|
18 |
import java.lang.reflect.InvocationHandler;
|
|
19 |
import java.lang.reflect.Method;
|
|
20 |
import java.lang.reflect.Proxy;
|
|
21 |
|
|
22 |
public class ImplementationHidingComponentFactory implements ComponentFactory { |
|
23 |
private final ComponentFactory componentFactory;
|
|
24 |
|
|
25 | 1 |
public ImplementationHidingComponentFactory(ComponentFactory componentFactory) {
|
26 | 1 |
this.componentFactory = componentFactory;
|
27 |
} |
|
28 |
|
|
29 | 1 |
public Object createComponent(ComponentSpecification componentSpec, Object[] instanceDependencies) throws PicoInitializationException { |
30 | 1 |
Object componentInstance = componentFactory.createComponent(componentSpec, instanceDependencies); |
31 | 1 |
if (componentInstance == null) { |
32 |
//TODO Should be subclass of PicoInit..
|
|
33 | 0 |
throw new NullPointerException("Unable to create componentInstance for componentSpec" + componentSpec); |
34 |
} |
|
35 |
// TODO: search for all interfaces for component-implementation instead
|
|
36 | 1 |
Class[] interfaces = new Class[]{(Class) componentSpec.getComponentKey()};
|
37 | 1 |
return Proxy.newProxyInstance(componentSpec.getComponentImplementation().getClassLoader(),
|
38 |
interfaces, new ImplementationHidingProxy(componentInstance));
|
|
39 |
} |
|
40 |
|
|
41 | 1 |
public Class[] getDependencies(Class componentImplementation) throws PicoIntrospectionException { |
42 | 1 |
return componentFactory.getDependencies(componentImplementation);
|
43 |
} |
|
44 |
|
|
45 |
private class ImplementationHidingProxy implements InvocationHandler { |
|
46 |
private Object componentInstance;
|
|
47 |
|
|
48 | 1 |
public ImplementationHidingProxy(Object componentInstance) {
|
49 | 1 |
this.componentInstance = componentInstance;
|
50 |
} |
|
51 |
|
|
52 | 1 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
53 | 1 |
return method.invoke(componentInstance, args);
|
54 |
} |
|
55 |
} |
|
56 |
} |
|
57 |
|
|