| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.component.annotation; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.Field; |
| 23 |
|
import java.lang.reflect.Type; |
| 24 |
|
import java.util.ArrayList; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.ServiceLoader; |
| 27 |
|
|
| 28 |
|
import javax.inject.Named; |
| 29 |
|
import javax.inject.Singleton; |
| 30 |
|
|
| 31 |
|
import org.xwiki.component.descriptor.ComponentDependency; |
| 32 |
|
import org.xwiki.component.descriptor.ComponentDescriptor; |
| 33 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
| 34 |
|
import org.xwiki.component.descriptor.DefaultComponentDescriptor; |
| 35 |
|
import org.xwiki.component.util.ReflectionUtils; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
@version |
| 41 |
|
@since |
| 42 |
|
@see |
| 43 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (59) |
Complexity: 14 |
Complexity Density: 0.35 |
|
| 44 |
|
public class ComponentDescriptorFactory |
| 45 |
|
{ |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
private ServiceLoader<ComponentDependencyFactory> componentDependencyFactories = ServiceLoader |
| 52 |
|
.load(ComponentDependencyFactory.class); |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
@param |
| 59 |
|
@param |
| 60 |
|
@return |
| 61 |
|
@deprecated@link |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 63 |
5 |
@Deprecated... |
| 64 |
|
public List<ComponentDescriptor> createComponentDescriptors(Class<?> componentClass, |
| 65 |
|
Class<?> componentRoleClass) |
| 66 |
|
{ |
| 67 |
5 |
return createComponentDescriptors(componentClass, (Type) componentRoleClass); |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@param |
| 75 |
|
@param |
| 76 |
|
@return |
| 77 |
|
@since |
| 78 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 6 |
Complexity Density: 0.43 |
|
| 79 |
727178 |
public List<ComponentDescriptor> createComponentDescriptors(Class<?> componentClass, Type componentRoleType)... |
| 80 |
|
{ |
| 81 |
727178 |
List<ComponentDescriptor> descriptors = new ArrayList<ComponentDescriptor>(); |
| 82 |
|
|
| 83 |
|
|
| 84 |
727178 |
String[] hints; |
| 85 |
727178 |
Named named = componentClass.getAnnotation(Named.class); |
| 86 |
727178 |
if (named != null) { |
| 87 |
425537 |
hints = new String[] {named.value()}; |
| 88 |
|
} else { |
| 89 |
|
|
| 90 |
|
|
| 91 |
301641 |
Component component = componentClass.getAnnotation(Component.class); |
| 92 |
301641 |
if (component != null && component.hints().length > 0) { |
| 93 |
10122 |
hints = component.hints(); |
| 94 |
|
} else { |
| 95 |
291519 |
if (component != null && component.value().trim().length() > 0) { |
| 96 |
14 |
hints = new String[] {component.value().trim()}; |
| 97 |
|
} else { |
| 98 |
291505 |
hints = new String[] {"default"}; |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
|
| 104 |
727178 |
for (String hint : hints) { |
| 105 |
742093 |
descriptors.add(createComponentDescriptor(componentClass, hint, componentRoleType)); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
727178 |
return descriptors; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
@param |
| 115 |
|
@param |
| 116 |
|
@param |
| 117 |
|
@return |
| 118 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 119 |
742093 |
private ComponentDescriptor createComponentDescriptor(Class<?> componentClass, String hint,... |
| 120 |
|
Type componentRoleType) |
| 121 |
|
{ |
| 122 |
742093 |
DefaultComponentDescriptor descriptor = new DefaultComponentDescriptor(); |
| 123 |
742093 |
descriptor.setRoleType(componentRoleType); |
| 124 |
742093 |
descriptor.setImplementation(componentClass); |
| 125 |
742093 |
descriptor.setRoleHint(hint); |
| 126 |
742093 |
descriptor.setInstantiationStrategy(createComponentInstantiationStrategy(componentClass)); |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
742093 |
for (Field field : ReflectionUtils.getAllFields(componentClass)) { |
| 133 |
4075801 |
ComponentDependency dependency = createComponentDependency(field); |
| 134 |
4075801 |
if (dependency != null) { |
| 135 |
1475652 |
descriptor.addComponentDependency(dependency); |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
|
| 139 |
742093 |
return descriptor; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
@param |
| 144 |
|
@return |
| 145 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 146 |
742093 |
private ComponentInstantiationStrategy createComponentInstantiationStrategy(Class<?> componentClass)... |
| 147 |
|
{ |
| 148 |
742093 |
ComponentInstantiationStrategy strategy; |
| 149 |
|
|
| 150 |
|
|
| 151 |
742093 |
Singleton singleton = componentClass.getAnnotation(Singleton.class); |
| 152 |
742093 |
if (singleton != null) { |
| 153 |
648963 |
strategy = ComponentInstantiationStrategy.SINGLETON; |
| 154 |
|
} else { |
| 155 |
93130 |
InstantiationStrategy instantiationStrategy = componentClass.getAnnotation(InstantiationStrategy.class); |
| 156 |
93130 |
if (instantiationStrategy != null) { |
| 157 |
87036 |
strategy = instantiationStrategy.value(); |
| 158 |
|
} else { |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
6094 |
strategy = ComponentInstantiationStrategy.SINGLETON; |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
|
|
| 166 |
742093 |
return strategy; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
@param |
| 171 |
|
@return |
| 172 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 173 |
4075801 |
private ComponentDependency createComponentDependency(Field field)... |
| 174 |
|
{ |
| 175 |
4075801 |
ComponentDependency dependency = null; |
| 176 |
|
|
| 177 |
|
|
| 178 |
4075801 |
for (ComponentDependencyFactory factory : this.componentDependencyFactories) { |
| 179 |
4075801 |
dependency = factory.createComponentDependency(field); |
| 180 |
4075801 |
if (dependency != null) { |
| 181 |
1475652 |
break; |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
|
| 185 |
4075801 |
return dependency; |
| 186 |
|
} |
| 187 |
|
} |