1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.objects.meta

File MetaClass.java

 

Coverage histogram

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

Code metrics

6
21
8
1
139
76
12
0.57
2.62
8
1.5

Classes

Class Line # Actions
MetaClass 44 21 0% 12 8
0.771428677.1%
 

Contributing tests

No tests hitting this source file were found.

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 com.xpn.xwiki.objects.meta;
21   
22    import java.util.List;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
27    import org.xwiki.component.manager.ComponentLookupException;
28   
29    import com.xpn.xwiki.XWikiContext;
30    import com.xpn.xwiki.internal.objects.classes.PropertyClassProvider;
31    import com.xpn.xwiki.objects.BaseCollection;
32    import com.xpn.xwiki.objects.PropertyInterface;
33    import com.xpn.xwiki.objects.classes.BaseClass;
34    import com.xpn.xwiki.objects.classes.PropertyClass;
35    import com.xpn.xwiki.web.Utils;
36   
37    /**
38    * A pseudo XClass whose fields are meta properties. In other words, each field of this XClass defines a type of
39    * property that can be added to a standard XClass. This class is being used to lookup XClass property types. New code
40    * should lookup {@link PropertyClassProvider} implementations instead using the component manager.
41    *
42    * @version $Id: 68ad62cd694a6ac1732bb542ed658093fafa5f4f $
43    */
 
44    public class MetaClass extends BaseClass
45    {
46    /**
47    * The prefix prepended to the property name when setting or retrieving a property meta class.
48    */
49    private static final String PROPERTY_NAME_PREFIX = "meta";
50   
51    /**
52    * Logging helper object.
53    */
54    private static final Logger LOGGER = LoggerFactory.getLogger(MetaClass.class);
55   
56    /**
57    * A cached instance of this class that can be used to quickly lookup XClass property types.
58    */
59    private static MetaClass metaClass;
60   
61    /**
62    * Creates a new instance that has a property for each available property type.
63    */
 
64  8 toggle public MetaClass()
65    {
66  8 try {
67  8 List<PropertyClassProvider> providers =
68    Utils.getContextComponentManager().getInstanceList(PropertyClassProvider.class);
69  8 for (PropertyClassProvider provider : providers) {
70  128 PropertyInterface property = provider.getDefinition();
71  128 safeput(property.getName(), property);
72    }
73    } catch (ComponentLookupException e) {
74  0 LOGGER.error("Failed to initialize the meta class.", e);
75    }
76    }
77   
 
78  128 toggle @Override
79    public void safeput(String name, PropertyInterface property)
80    {
81  128 addField(PROPERTY_NAME_PREFIX + name, property);
82  128 if (property instanceof PropertyClass) {
83  0 ((PropertyClass) property).setObject(this);
84  0 ((PropertyClass) property).setName(name);
85    }
86    }
87   
 
88  3793 toggle @Override
89    public PropertyInterface safeget(String name)
90    {
91  3793 return super.safeget(PROPERTY_NAME_PREFIX + name);
92    }
93   
 
94  3792 toggle @Override
95    public PropertyInterface get(String name)
96    {
97  3792 PropertyInterface property = safeget(name);
98  3792 if (property == null) {
99    // In previous versions the property name was the full Java class name of the property class implementation.
100    // Extract the actual property name (the hint used to lookup the property class provider) by removing the
101    // Java package prefix and the Class suffix.
102  1 property = safeget(StringUtils.removeEnd(StringUtils.substringAfterLast(name, "."), "Class"));
103    }
104  3792 return property;
105    }
106   
 
107  0 toggle @Override
108    public void put(String name, PropertyInterface property)
109    {
110  0 safeput(name, property);
111    }
112   
113    /**
114    * @return a cached instance of this class that can be used to quickly lookup XClass property types
115    */
 
116  955 toggle public static MetaClass getMetaClass()
117    {
118  955 if (metaClass == null) {
119  8 metaClass = new MetaClass();
120    }
121  955 return metaClass;
122    }
123   
124    /**
125    * Sets the cached instance of this class.
126    *
127    * @param metaClass the cached instance
128    */
 
129  60 toggle public static void setMetaClass(MetaClass metaClass)
130    {
131  60 MetaClass.metaClass = metaClass;
132    }
133   
 
134  0 toggle @Override
135    public BaseCollection newObject(XWikiContext context)
136    {
137  0 return new BaseClass();
138    }
139    }