|
|||||||||||||||||||
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 | |||||||||||||||
HierarchicalPicoContainer.java | 100% | 100% | 100% | 100% |
|
1 |
/*****************************************************************************
|
|
2 |
* Copyright (Cc) 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 |
/*
|
|
12 |
TODO (Aslak):
|
|
13 |
|
|
14 |
1) Factor out a DependencyAnalyzer:
|
|
15 |
public interface DependencyAnalyzer {
|
|
16 |
InstantiationSpecification[] getOrderedInstantiationSpecifications();
|
|
17 |
}
|
|
18 |
|
|
19 |
ConstructorDependencyAnalyzer would emerge from refactoring this class.
|
|
20 |
|
|
21 |
2) Refactor the ContainerFactory's createComponent method to take a
|
|
22 |
InstantiationSpecification argument. This class/intf should contain'
|
|
23 |
everything needed to instantiate a component.
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package picocontainer.hierarchical;
|
|
28 |
|
|
29 |
import picocontainer.defaults.DefaultPicoContainer;
|
|
30 |
import picocontainer.ClassRegistrationPicoContainer;
|
|
31 |
import picocontainer.ComponentFactory;
|
|
32 |
import picocontainer.PicoContainer;
|
|
33 |
import picocontainer.defaults.DefaultComponentFactory;
|
|
34 |
import picocontainer.defaults.NullContainer;
|
|
35 |
import picocontainer.defaults.AmbiguousComponentResolutionException;
|
|
36 |
|
|
37 |
import java.util.List;
|
|
38 |
import java.util.Arrays;
|
|
39 |
import java.util.HashSet;
|
|
40 |
import java.util.Set;
|
|
41 |
|
|
42 |
public class HierarchicalPicoContainer extends DefaultPicoContainer implements ClassRegistrationPicoContainer { |
|
43 |
|
|
44 |
private final PicoContainer parentContainer;
|
|
45 |
|
|
46 | 43 |
public HierarchicalPicoContainer(ComponentFactory componentFactory, PicoContainer parentContainer) {
|
47 | 43 |
super(componentFactory);
|
48 |
|
|
49 | 43 |
if (parentContainer == null) { |
50 | 1 |
throw new NullPointerException("parentContainer cannot be null"); |
51 |
} |
|
52 | 42 |
this.parentContainer = parentContainer;
|
53 |
} |
|
54 |
|
|
55 |
public static class Default extends HierarchicalPicoContainer { |
|
56 | 33 |
public Default() {
|
57 | 33 |
super(new DefaultComponentFactory(), new NullContainer()); |
58 |
} |
|
59 |
|
|
60 |
} |
|
61 |
|
|
62 |
public static class WithParentContainer extends HierarchicalPicoContainer { |
|
63 | 1 |
public WithParentContainer(PicoContainer parentContainer) {
|
64 | 1 |
super(new DefaultComponentFactory(), parentContainer); |
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
public static class WithComponentFactory extends HierarchicalPicoContainer { |
|
69 | 1 |
public WithComponentFactory(ComponentFactory componentFactory) {
|
70 | 1 |
super(componentFactory, new NullContainer()); |
71 |
} |
|
72 |
} |
|
73 |
|
|
74 | 55 |
public Object getComponent(Class componentType) {
|
75 |
// First look in myself
|
|
76 | 55 |
Object result = super.getComponent(componentType);
|
77 |
|
|
78 |
// Then look in parent if we had nothing
|
|
79 | 55 |
if (result == null) { |
80 | 23 |
result = parentContainer.getComponent(componentType); |
81 |
} |
|
82 | 55 |
return result;
|
83 |
} |
|
84 |
|
|
85 | 3 |
public Class[] getComponentTypes() {
|
86 |
// Get my own types
|
|
87 | 3 |
List myTypes = Arrays.asList(super.getComponentTypes());
|
88 |
|
|
89 |
// Get those from my parent.
|
|
90 | 3 |
Set types = new HashSet(myTypes);
|
91 | 3 |
types.addAll(Arrays.asList(parentContainer.getComponentTypes())); |
92 |
|
|
93 | 3 |
return (Class[]) types.toArray(new Class[types.size()]); |
94 |
} |
|
95 |
|
|
96 |
} |
|
97 |
|
|