Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
22   157   14   3.67
2   78   0.64   6
6     2.33  
1    
6.2% of code in this file is excluded from these metrics.
 
  PackageObjectFactory       Line # 32 96.7% 0.96666664
14.01 22 14 14 0.64
 
  ( 10 of 458)
 
1    ////////////////////////////////////////////////////////////////////////////////
2    // checkstyle: Checks Java source code for adherence to a set of rules.
3    // Copyright (C) 2001-2005 Oliver Burn
4    //
5    // This library is free software; you can redistribute it and/or
6    // modify it under the terms of the GNU Lesser General Public
7    // License as published by the Free Software Foundation; either
8    // version 2.1 of the License, or (at your option) any later version.
9    //
10    // This library 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 library; if not, write to the Free Software
17    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18    ////////////////////////////////////////////////////////////////////////////////
19    package com.puppycrawl.tools.checkstyle;
20   
21    import java.util.ArrayList;
22    import java.util.List;
23   
24    import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
25   
26    /**
27    * A factory for creating objects from package names and names.
28    * @author Rick Giles
29    * @author lkuehne
30    * @version $Revision$
31    */
 
32    class PackageObjectFactory implements ModuleFactory
33    {
34    /** a list of package names to prepend to class names */
35    private final List mPackages = new ArrayList();
36   
37    /**
38    * Creates a new <code>PackageObjectFactory</code> instance.
39    */
 
40  467 toggle PackageObjectFactory()
41    {
42    }
43   
44    /**
45    * Helper for testing.
46    * @return the package names that have been added
47    */
 
48  2 toggle String[] getPackages()
49    {
50  2 return (String[]) mPackages.toArray(new String[mPackages.size()]);
51    }
52   
53    /**
54    * Registers a package name to use for shortName resolution.
55    * @param aPackageName the package name
56    */
 
57  7345 toggle void addPackage(String aPackageName)
58    {
59  7345 mPackages.add(aPackageName);
60    }
61   
62    /**
63    * Creates a new instance of a class from a given name. If the name is
64    * a classname, creates an instance of the named class. Otherwise, creates
65    * an instance of a classname obtained by concatenating the given
66    * to a package name from a given list of package names.
67    * @param aName the name of a class.
68    * @return the <code>Object</code>
69    * @throws CheckstyleException if an error occurs.
70    */
 
71  937 toggle private Object doMakeObject(String aName)
72    throws CheckstyleException
73    {
74    //try aName first
75  937 try {
76  937 return createObject(aName);
77    }
78    catch (final CheckstyleException ex) {
79  4 ; // keep looking
80    }
81   
82    //now try packages
83  4 for (int i = 0; i < mPackages.size(); i++) {
84  1 final String packageName = (String) mPackages.get(i);
85  1 final String className = packageName + aName;
86  1 try {
87  1 return createObject(className);
88    }
89    catch (final CheckstyleException ex) {
90  0 ; // keep looking
91    }
92    }
93   
94  3 throw new CheckstyleException("Unable to instantiate " + aName);
95    }
96   
97    /**
98    * Creates a new instance of a named class.
99    * @param aClassName the name of the class to instantiate.
100    * @return the <code>Object</code> created by mLoader.
101    * @throws CheckstyleException if an error occurs.
102    */
 
103  938 toggle private Object createObject(String aClassName)
104    throws CheckstyleException
105    {
106  938 try {
107  938 final ClassLoader loader = Thread.currentThread()
108    .getContextClassLoader();
109  938 final Class clazz = Class.forName(aClassName, true, loader);
110  934 return clazz.newInstance();
111    }
112    catch (final ClassNotFoundException e) {
113  4 throw new CheckstyleException(
114    "Unable to find class for " + aClassName, e);
115    }
116    catch (final InstantiationException e) {
117    ///CLOVER:OFF
118    throw new CheckstyleException(
119    "Unable to instantiate " + aClassName, e);
120    ///CLOVER:ON
121    }
122    catch (final IllegalAccessException e) {
123    ///CLOVER:OFF
124    throw new CheckstyleException(
125    "Unable to instantiate " + aClassName, e);
126    ///CLOVER:ON
127    }
128    }
129   
130    /**
131    * Creates a new instance of a class from a given name, or that name
132    * concatenated with &quot;Check&quot;. If the name is
133    * a classname, creates an instance of the named class. Otherwise, creates
134    * an instance of a classname obtained by concatenating the given name
135    * to a package name from a given list of package names.
136    * @param aName the name of a class.
137    * @return the <code>Object</code> created by aLoader.
138    * @throws CheckstyleException if an error occurs.
139    */
 
140  935 toggle public Object createModule(String aName)
141    throws CheckstyleException
142    {
143  935 try {
144  935 return doMakeObject(aName);
145    }
146    catch (final CheckstyleException ex) {
147    //try again with suffix "Check"
148  2 try {
149  2 return doMakeObject(aName + "Check");
150    }
151    catch (final CheckstyleException ex2) {
152  1 throw new CheckstyleException(
153    "Unable to instantiate " + aName, ex2);
154    }
155    }
156    }
157    }