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

File GenericProvider.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
19
3
1
113
61
10
0.53
6.33
3
3.33

Classes

Class Line # Actions
GenericProvider 44 19 0% 10 6
0.880%
 

Contributing tests

This file is covered by 627 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.embed;
21   
22    import java.lang.reflect.Type;
23    import java.util.List;
24    import java.util.Map;
25   
26    import javax.inject.Provider;
27   
28    import org.xwiki.component.annotation.ComponentRole;
29    import org.xwiki.component.annotation.Role;
30    import org.xwiki.component.internal.RoleHint;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.component.util.ReflectionUtils;
34   
35    /**
36    * Default provider used when the Component Manager needs to inject a {@link Provider} field but no custom Provider has
37    * been registered. The default behavior is simply to look up the Component role class when
38    * {@link javax.inject.Provider#get()} is called. This is useful for example when you wish to do "lazy injection".
39    *
40    * @param <T> the role type
41    * @version $Id: fc59ea4cd4f56b59a587a42a372e2273a0fdcddd $
42    * @since 3.3M2
43    */
 
44    public class GenericProvider<T> implements Provider<T>
45    {
46    /**
47    * @see GenericProvider#GenericProvider(ComponentManager, RoleHint)
48    */
49    protected ComponentManager componentManager;
50   
51    /**
52    * @see GenericProvider#GenericProvider(ComponentManager, RoleHint)
53    */
54    private RoleHint<T> roleHint;
55   
56    /**
57    * @param componentManager the Component Manager instance that we'll use to look up the Component Role during
58    * {@link javax.inject.Provider#get()}
59    * @param roleHint the Component Role and Hint that uniquely identify the Component we wish to provide for
60    */
 
61  14628 toggle public GenericProvider(ComponentManager componentManager, RoleHint<T> roleHint)
62    {
63  14628 this.componentManager = componentManager;
64  14628 this.roleHint = roleHint;
65    }
66   
 
67  72092 toggle @Override
68    @SuppressWarnings("unchecked")
69    public T get()
70    {
71  72107 T component;
72   
73  72105 try {
74  72104 Class<T> roleClass = this.roleHint.getRoleClass();
75   
76  72089 if (roleClass.isAssignableFrom(Provider.class)) {
77  0 try {
78  0 component =
79    this.componentManager.getInstance(this.roleHint.getRoleType(), this.roleHint.getHint());
80    } catch (ComponentLookupException e) {
81    // Inject a default Provider
82  0 component =
83    (T) new GenericProvider<Object>(this.componentManager, new RoleHint<Object>(
84    ReflectionUtils.getLastTypeGenericArgument(this.roleHint.getRoleType()),
85    this.roleHint.getHint()));
86    }
87  72087 } else if (roleClass.isAssignableFrom(List.class)) {
88  36425 component =
89    (T) this.componentManager.getInstanceList(ReflectionUtils.getLastTypeGenericArgument(this.roleHint
90    .getRoleType()));
91  35632 } else if (roleClass.isAssignableFrom(Map.class)) {
92  1 component =
93    (T) this.componentManager.getInstanceMap(ReflectionUtils.getLastTypeGenericArgument(this.roleHint
94    .getRoleType()));
95  35630 } else if (ReflectionUtils.getDirectAnnotation(ComponentRole.class, roleClass) != null
96    && ReflectionUtils.getDirectAnnotation(Role.class, roleClass) == null) {
97    // since 4.0M1, retro-compatibility (generic type used to not be taken into account)
98  0 component = getInstance(roleClass, this.roleHint.getHint());
99    } else {
100  35626 component = getInstance(this.roleHint.getRoleType(), this.roleHint.getHint());
101    }
102    } catch (Exception e) {
103  1 throw new RuntimeException("Failed to get [" + this.roleHint + "]", e);
104    }
105   
106  72114 return component;
107    }
108   
 
109  35618 toggle protected T getInstance(Type type, String hint) throws ComponentLookupException
110    {
111  35623 return this.componentManager.getInstance(type, hint);
112    }
113    }