1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.dependencyfinder.cli; |
34 |
| |
35 |
| import java.util.*; |
36 |
| import java.io.*; |
37 |
| |
38 |
| import org.apache.log4j.*; |
39 |
| |
40 |
| import com.jeantessier.commandline.*; |
41 |
| import com.jeantessier.dependency.*; |
42 |
| |
43 |
| public class ClassCohesion extends DependencyGraphCommand { |
44 |
| private static final String DEFAULT_ENCODING = "utf-8"; |
45 |
| private static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd"; |
46 |
| private static final String DEFAULT_INDENT_TEXT = " "; |
47 |
| |
48 |
11
| protected void populateCommandLineSwitches() {
|
49 |
11
| super.populateCommandLineSwitches();
|
50 |
11
| populateCommandLineSwitchesForXMLOutput(DEFAULT_ENCODING, DEFAULT_DTD_PREFIX, DEFAULT_INDENT_TEXT);
|
51 |
| |
52 |
11
| getCommandLine().addToggleSwitch("csv");
|
53 |
11
| getCommandLine().addToggleSwitch("txt");
|
54 |
11
| getCommandLine().addToggleSwitch("xml");
|
55 |
11
| getCommandLine().addToggleSwitch("list");
|
56 |
| } |
57 |
| |
58 |
10
| protected Collection<CommandLineException> parseCommandLine(String[] args) {
|
59 |
10
| Collection<CommandLineException> exceptions = super.parseCommandLine(args);
|
60 |
10
| int modeSwitch = 0;
|
61 |
| |
62 |
10
| if (getCommandLine().getToggleSwitch("csv")) {
|
63 |
2
| modeSwitch++;
|
64 |
| } |
65 |
10
| if (getCommandLine().getToggleSwitch("txt")) {
|
66 |
2
| modeSwitch++;
|
67 |
| } |
68 |
10
| if (getCommandLine().getToggleSwitch("xml")) {
|
69 |
1
| modeSwitch++;
|
70 |
| } |
71 |
10
| if (modeSwitch != 1) {
|
72 |
7
| exceptions.add(new CommandLineException("Must have one and only one of -csv, -txt, or -xml"));
|
73 |
| } |
74 |
| |
75 |
10
| return exceptions;
|
76 |
| } |
77 |
| |
78 |
0
| public void doProcessing() throws Exception {
|
79 |
0
| LCOM4Gatherer gatherer = new LCOM4Gatherer();
|
80 |
| |
81 |
0
| Logger.getLogger(OOMetrics.class).debug("Reading classes and computing metrics as we go ...");
|
82 |
0
| getVerboseListener().print("Reading classes and computing metrics as we go ...");
|
83 |
0
| gatherer.traverseNodes(loadGraph().getPackages().values());
|
84 |
| |
85 |
0
| Logger.getLogger(OOMetrics.class).debug("Printing results ...");
|
86 |
0
| getVerboseListener().print("Printing results ...");
|
87 |
| |
88 |
0
| if (getCommandLine().isPresent("csv")) {
|
89 |
0
| printCSVFiles(gatherer.getResults());
|
90 |
0
| } else if (getCommandLine().isPresent("txt")) {
|
91 |
0
| printTextFile(gatherer.getResults());
|
92 |
0
| } else if (getCommandLine().isPresent("xml")) {
|
93 |
0
| printXMLFile(gatherer.getResults());
|
94 |
| } |
95 |
| |
96 |
0
| Logger.getLogger(OOMetrics.class).debug("Done.");
|
97 |
| } |
98 |
| |
99 |
0
| private void printCSVFiles(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException {
|
100 |
0
| getOut().println("class, LCOM4");
|
101 |
0
| for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) {
|
102 |
0
| getOut().println(entry.getKey().getName() + ", " + entry.getValue().size());
|
103 |
| } |
104 |
| } |
105 |
| |
106 |
0
| private void printTextFile(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException {
|
107 |
0
| String indentText = getCommandLine().getSingleSwitch("indent-text");
|
108 |
| |
109 |
0
| for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) {
|
110 |
0
| getOut().println(entry.getKey().getName() + ": " + entry.getValue().size());
|
111 |
0
| if (entry.getValue().size() > 1 && getCommandLine().isPresent("list")) {
|
112 |
0
| getOut().println(indentText + "--------");
|
113 |
0
| for (Collection<FeatureNode> component : entry.getValue()) {
|
114 |
0
| for (FeatureNode feature : component) {
|
115 |
0
| getOut().println(indentText + feature.getName().substring(feature.getClassNode().getName().length() + 1));
|
116 |
| } |
117 |
0
| getOut().println(indentText + "--------");
|
118 |
| } |
119 |
| } |
120 |
| } |
121 |
| } |
122 |
| |
123 |
0
| private void printXMLFile(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException {
|
124 |
0
| String indentText = getCommandLine().getSingleSwitch("indent-text");
|
125 |
| |
126 |
0
| getOut().println("<classes>");
|
127 |
0
| for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) {
|
128 |
0
| if (entry.getValue().size() > 1) {
|
129 |
0
| getOut().println(indentText + "<class name=\"" + entry.getKey().getName() + "\" lcom4=\"" + entry.getValue().size() + "\">");
|
130 |
0
| for (Collection<FeatureNode> component : entry.getValue()) {
|
131 |
0
| getOut().println(indentText + indentText + "<component>");
|
132 |
0
| for (FeatureNode feature : component) {
|
133 |
0
| getOut().println(indentText + indentText + indentText + "<feature name=\"" + feature.getName() + "\"/>");
|
134 |
| } |
135 |
0
| getOut().println(indentText + indentText + "</component>");
|
136 |
| } |
137 |
0
| getOut().println(indentText + "</class>");
|
138 |
| } else { |
139 |
0
| getOut().println(indentText + "<class name=\"" + entry.getKey().getName() + "\" lcom4=\"" + entry.getValue().size() + "\"/>");
|
140 |
| } |
141 |
| } |
142 |
0
| getOut().println("</classes>");
|
143 |
| } |
144 |
| |
145 |
0
| public static void main(String[] args) throws Exception {
|
146 |
0
| new ClassCohesion().run(args);
|
147 |
| } |
148 |
| } |