KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > ant > DependencyCheckingTask


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle.ant;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.tools.ant.BuildException;
33
34 import classycle.Analyser;
35 import classycle.dependency.DefaultResultRenderer;
36 import classycle.dependency.DependencyChecker;
37 import classycle.dependency.ResultRenderer;
38 import classycle.util.Text;
39
40 /**
41  * Ant Task for checking class dependencies.
42  * <p>
43  * <table border="1" cellpadding="5" cellspacing="0">
44  * <tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
45  * <tr><td valign="top">includingClasses</td>
46  * <td>Comma or space separated list of wild-card patterns of
47  * fully-qualified class name which are included in the analysis.
48  * Only '*' are recognized as wild-card character.
49  * </td>
50  * <td valign="top">No. By default all classes defined in the file set
51  * are included.
52  * </td>
53  * </tr>
54  * <tr><td valign="top">excludingClasses</td>
55  * <td valign="top">Comma or space separated list of wild-card patterns of
56  * fully-qualified class name which are excluded from the analysis.
57  * Only '*' are recognized as wild-card character.
58  * </td>
59  * <td valign="top">No. By default no class defined in the file set is
60  * excluded.
61  * </td>
62  * </tr>
63  * <tr><td valign="top">mergeInnerClasses</td>
64  * <td valign="top">If <code>true</code> all class vertices are merged
65  * with the vertices of the corresponding inner classes.
66  * </td>
67  * <td valign="top">No. Default is <tt>false</tt>.</td>
68  * </tr>
69  * <tr><td valign="top">reflectionPattern</td>
70  * <td valign="top">Comma or space separated list of wild-card patterns of
71  * fully-qualified class name.
72  * Only '*' are recognized as wild-card character.
73  * <p>
74  * If in the code of a class an ordinary string constant matches
75  * one of these patterns and if this string constant
76  * has a valid syntax for a fully-qualified
77  * class name this constant will be treated as a class reference.
78  * </td>
79  * <td valign="top">No. By default ordinary string constants are not
80  * treated as class references.
81  * </td>
82  * </tr>
83  * <tr><td valign="top">definitionFile</td>
84  * <td valign="top">Path of the dependency definition file relative
85  * to the base directory.
86  * </td>
87  * <td valign="top">No. By default the dependency definition commands
88  * are embedded in the ant task.
89  * </td>
90  * </tr>
91  * <tr><td valign="top">failOnUnwantedDependencies</td>
92  * <td valign="top">If <tt>true</tt> the task will fail if an
93  * unwanted dependency is found.
94  * </td>
95  * <td valign="top">No. Default value is <tt>false</tt>.
96  * </td>
97  * </tr>
98  * <tr><td valign="top">resultRenderer</td>
99  * <td valign="top">Fully-qualified class name of a
100  * {@link ResultRenderer}.
101  * </td>
102  * <td valign="top">No. By default {@link DefaultResultRenderer} is used.
103  * </td>
104  * </tr>
105  * </table>
106  *
107  * @author Franz-Josef Elmer
108  */

109 public class DependencyCheckingTask extends ClassycleTask
110 {
111   private String JavaDoc _definitionFile;
112   private String JavaDoc _dependencyDefinition;
113   private String JavaDoc _resultRenderer;
114   private boolean _failOnUnwantedDependencies;
115   
116   public void setFailOnUnwantedDependencies(boolean failOnUnwantedDependencies)
117   {
118     _failOnUnwantedDependencies = failOnUnwantedDependencies;
119   }
120   
121   public void setDefinitionFile(String JavaDoc definitionFile)
122   {
123     _definitionFile = definitionFile;
124   }
125
126   public void setResultRenderer(String JavaDoc resultRenderer)
127   {
128     _resultRenderer = resultRenderer;
129   }
130   
131   public void addText(String JavaDoc text)
132   {
133     _dependencyDefinition = text.trim();
134   }
135   
136   public void execute() throws BuildException
137   {
138     super.execute();
139     
140     boolean ok = false;
141     try
142     {
143       Analyser analyser = new Analyser(getClassFileNames(), getPattern(),
144                                        getReflectionPattern(),
145                                        isMergeInnerClasses());
146       Map JavaDoc properties = _definitionFile == null ? getProject().getProperties()
147                                                : System.getProperties();
148       DependencyChecker dependencyChecker
149                         = new DependencyChecker(analyser,
150                                                 getDependencyDefinitions(),
151                                                 properties,
152                                                 getRenderer());
153       PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(System.out);
154       ok = dependencyChecker.check(printWriter);
155       printWriter.flush();
156     } catch (BuildException e)
157     {
158       throw e;
159     } catch (Exception JavaDoc e)
160     {
161       throw new BuildException(e);
162     }
163     if (_failOnUnwantedDependencies && ok == false)
164     {
165       throw new BuildException(
166               "Unwanted dependencies found. See output for details.");
167     }
168   }
169
170   private ResultRenderer getRenderer() throws InstantiationException JavaDoc,
171                                               IllegalAccessException JavaDoc,
172                                               ClassNotFoundException JavaDoc
173   {
174     ResultRenderer renderer = new DefaultResultRenderer();
175     if (_resultRenderer != null)
176     {
177       renderer = (ResultRenderer) Class.forName(_resultRenderer).newInstance();
178     }
179     return renderer;
180   }
181
182   private String JavaDoc getDependencyDefinitions() throws IOException JavaDoc, BuildException
183   {
184     String JavaDoc result = _dependencyDefinition;;
185     if (_definitionFile != null)
186     {
187       File JavaDoc baseDir = getOwningTarget().getProject().getBaseDir();
188       result = Text.readTextFile(new File JavaDoc(baseDir, _definitionFile));
189     }
190     if (result.length() == 0) {
191       throw new BuildException("Empty dependency definition.");
192     }
193     return result;
194   }
195 }
196
Popular Tags