1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.descriptor

File DefaultComponentDependency.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

14
47
13
1
224
126
27
0.57
3.62
13
2.08

Classes

Class Line # Actions
DefaultComponentDependency 43 47 0% 27 37
0.550%
 

Contributing tests

This file is covered by 3253 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.descriptor;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.lang.reflect.Type;
24    import java.util.Arrays;
25    import java.util.Collection;
26    import java.util.List;
27    import java.util.Map;
28    import java.util.Objects;
29   
30    import javax.inject.Provider;
31   
32    import org.apache.commons.lang3.builder.HashCodeBuilder;
33    import org.xwiki.component.util.DefaultParameterizedType;
34    import org.xwiki.component.util.ReflectionUtils;
35   
36    /**
37    * Default implementation of {@link ComponentDependency}.
38    *
39    * @version $Id: f3e67627a55dc762547a2d922fc5550d5012083c $
40    * @param <T> the type of the component role
41    * @since 1.7M1
42    */
 
43    public class DefaultComponentDependency<T> extends DefaultComponentRole<T> implements ComponentDependency<T>
44    {
45    /**
46    * @see #getName()
47    */
48    private String name;
49   
50    /**
51    * @see #getHints()
52    */
53    private String[] hints;
54   
55    /**
56    * Default constructor.
57    */
 
58  1475921 toggle public DefaultComponentDependency()
59    {
60    }
61   
62    /**
63    * Create a new DefaultComponentDependency by cloning the provided {@link ComponentDependency}.
64    *
65    * @param dependency the component dependency to clone
66    * @since 3.4M1
67    */
 
68  1475660 toggle public DefaultComponentDependency(ComponentDependency<T> dependency)
69    {
70  1475660 super(dependency);
71   
72  1475660 setName(dependency.getName());
73  1475660 if (dependency.getHints() != null) {
74  0 setHints(dependency.getHints().clone());
75    }
76    }
77   
 
78  2189725 toggle @Override
79    public String getName()
80    {
81  2189726 return this.name;
82    }
83   
 
84  1475730 toggle @Override
85    public String[] getHints()
86    {
87  1475730 return this.hints;
88    }
89   
90    /**
91    * @param name the name of the injection point (can be the name of the field for field injection or the name of the
92    * method for method injection
93    */
 
94  2951581 toggle public void setName(String name)
95    {
96  2951581 this.name = name;
97    }
98   
99    /**
100    * @param hints a list of hints used when the mapping type is a collection or map so that only component
101    * implementations matching passed hints are injected
102    */
 
103  0 toggle public void setHints(String[] hints)
104    {
105  0 this.hints = hints;
106    }
107   
108    /**
109    * {@inheritDoc}
110    *
111    * @since 3.3M1
112    */
 
113  35 toggle @Override
114    public boolean equals(Object object)
115    {
116  35 boolean result;
117   
118    // See http://www.technofundo.com/tech/java/equalhash.html for the detail of this algorithm.
119  35 if (this == object) {
120  0 result = true;
121    } else {
122  35 if (object == null || object.getClass() != getClass()) {
123  0 result = false;
124    } else {
125    // object must be Syntax at this point
126  35 result = equals((ComponentDependency) object);
127    }
128    }
129  35 return result;
130    }
131   
132    /**
133    * @param dependency the dependency to compare to
134    * @return true if the passed dependency is equals to the current instance or false otherwise
135    */
 
136  35 toggle private boolean equals(ComponentDependency dependency)
137    {
138  35 return super.equals(dependency) && Objects.equals(getName(), dependency.getName())
139    && Arrays.equals(getHints(), dependency.getHints());
140    }
141   
142    /**
143    * {@inheritDoc}
144    *
145    * @since 3.3M1
146    */
 
147  0 toggle @Override
148    public int hashCode()
149    {
150  0 HashCodeBuilder builder = new HashCodeBuilder();
151   
152  0 builder.appendSuper(super.hashCode());
153   
154  0 builder.append(getRoleType());
155  0 builder.append(getName());
156  0 builder.append(getHints());
157   
158  0 return builder.toHashCode();
159    }
160   
161    // deprecated
162   
 
163  24 toggle @Override
164    @Deprecated
165    public Class<?> getMappingType()
166    {
167  24 return ReflectionUtils.getTypeClass(getRoleType());
168    }
169   
170    /**
171    * @param mappingType the class of the type for the injection (java.lang.String, java.util.List, etc)
172    * @deprecated since 4.0M1 use {@link #setRoleType(java.lang.reflect.Type)} instead
173    */
 
174  4 toggle @Deprecated
175    public void setMappingType(Class<?> mappingType)
176    {
177  4 Type ownerType;
178  4 Type[] parameters;
179  4 if (getRoleType() instanceof ParameterizedType) {
180  0 ParameterizedType parameterizedType = (ParameterizedType) getRoleType();
181  0 ownerType = parameterizedType.getOwnerType();
182  0 parameters = parameterizedType.getActualTypeArguments();
183   
184  0 setRoleType(new DefaultParameterizedType(ownerType, mappingType, parameters));
185    } else {
186  4 setRoleType(mappingType);
187    }
188    }
189   
 
190  12 toggle @Override
191    public Class<T> getRole()
192    {
193  12 Class mapping = getMappingType();
194   
195  12 if (mapping == List.class || mapping == Collection.class || mapping == Map.class || mapping == Provider.class) {
196  4 return ReflectionUtils.getTypeClass(ReflectionUtils.getLastTypeGenericArgument(getRoleType()));
197    } else {
198  8 return mapping;
199    }
200    }
201   
 
202  0 toggle @Override
203    public void setRole(Class<T> role)
204    {
205  0 Class mapping = getMappingType();
206   
207  0 if (mapping == List.class || mapping == Collection.class || mapping == Map.class || mapping == Provider.class) {
208  0 Type ownerType;
209  0 Class<?> rawType;
210  0 if (getRoleType() instanceof ParameterizedType) {
211  0 ParameterizedType parameterizedType = (ParameterizedType) getRoleType();
212  0 ownerType = parameterizedType.getOwnerType();
213  0 rawType = (Class<?>) parameterizedType.getRawType();
214    } else {
215  0 ownerType = null;
216  0 rawType = mapping;
217    }
218   
219  0 setRoleType(new DefaultParameterizedType(ownerType, rawType, role));
220    } else {
221  0 super.setRole(role);
222    }
223    }
224    }