KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > dependency > DependencyChecker


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.dependency;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import classycle.Analyser;
31 import classycle.graph.AtomicVertex;
32
33 /**
34  * Checks a class graph for unwanted dependencies. The dependencies are
35  * described by a dependency definition file (<tt>.ddf</tt>).
36  *
37  * @author Franz-Josef Elmer
38  */

39 public class DependencyChecker
40 {
41   private final Analyser _analyser;
42   private final ResultRenderer _renderer;
43   private final DependencyProcessor _processor;
44   
45   /**
46    * Creates a new instance.
47    * Note, that the constructor does not create the graph. It only parses
48    * <tt>dependencyDefinition</tt> as a preprocessing step. The calculation
49    * of the graph is done in {@link #check(PrintWriter)}.
50    *
51    * @param analyser Analyzer instance.
52    * @param dependencyDefinition Description (as read from a .ddf file) of the
53    * dependencies to be checked.
54    * @param renderer Output renderer for unwanted dependencies found.
55    */

56   public DependencyChecker(Analyser analyser,
57                            String JavaDoc dependencyDefinition, Map JavaDoc properties,
58                            ResultRenderer renderer)
59   {
60     _analyser = analyser;
61     _renderer = renderer;
62     DependencyProperties dp = new DependencyProperties(properties);
63     _processor = new DependencyProcessor(dependencyDefinition, dp, renderer);
64   }
65   
66   /**
67    * Checks the graph and write unwanted dependencies onto the specified
68    * writer.
69    * @return <tt>true</tt> if no unwanted dependency has been found.
70    */

71   public boolean check(PrintWriter JavaDoc writer)
72   {
73     boolean ok = true;
74     AtomicVertex[] graph = _analyser.getClassGraph();
75     while (_processor.hasMoreStatements())
76     {
77       Result result = _processor.executeNextStatement(graph);
78       if (result.isOk() == false)
79       {
80         ok = false;
81       }
82       writer.print(_renderer.render(result));
83     }
84     return ok;
85   }
86   
87   /**
88    * Runs the DependencyChecker application.
89    * Exit 0 if no unwanted dependency found otherwise 1 is returned.
90    */

91   public static void main(String JavaDoc[] args)
92   {
93     DependencyCheckerCommandLine commandLine
94         = new DependencyCheckerCommandLine(args);
95     if (!commandLine.isValid()) {
96       System.out.println("Usage: java -cp classycle.jar "
97                          + "classycle.DependencyChecker "
98                          + commandLine.getUsage());
99       System.exit(1);
100     }
101
102     Analyser analyser = new Analyser(commandLine.getClassFiles(),
103                                      commandLine.getPattern(),
104                                      commandLine.getReflectionPattern(),
105                                      commandLine.isMergeInnerClasses());
106     DependencyChecker dependencyChecker
107         = new DependencyChecker(analyser,
108                                 commandLine.getDependencyDefinition(),
109                                 System.getProperties(),
110                                 commandLine.getRenderer());
111     PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(System.out);
112     boolean ok = dependencyChecker.check(printWriter);
113     printWriter.flush();
114     System.exit(ok ? 0 : 1);
115   }
116 }
117
Popular Tags