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 com.jeantessier.classreader.*; |
39 |
| import com.jeantessier.text.*; |
40 |
| |
41 |
| public class ClassMetrics extends DirectoryExplorerCommand { |
42 |
| private boolean list; |
43 |
| |
44 |
4
| protected void populateCommandLineSwitches() {
|
45 |
4
| super.populateCommandLineSwitches();
|
46 |
| |
47 |
4
| getCommandLine().addToggleSwitch("list");
|
48 |
4
| getCommandLine().addToggleSwitch("instruction-counts");
|
49 |
| } |
50 |
| |
51 |
0
| public void doProcessing() throws Exception {
|
52 |
0
| list = getCommandLine().getToggleSwitch("list");
|
53 |
| |
54 |
0
| MetricsGatherer metrics = new MetricsGatherer();
|
55 |
| |
56 |
0
| ClassfileLoader loader = new TransientClassfileLoader();
|
57 |
0
| loader.addLoadListener(getVerboseListener());
|
58 |
0
| loader.addLoadListener(new LoadListenerVisitorAdapter(metrics));
|
59 |
0
| loader.load(getCommandLine().getParameters());
|
60 |
| |
61 |
0
| getVerboseListener().print("Printing report ...");
|
62 |
| |
63 |
0
| getOut().println(metrics.getClasses().size() + " class(es)");
|
64 |
0
| if (list) {
|
65 |
0
| for (Object o : metrics.getClasses()) {
|
66 |
0
| getOut().println(" " + o);
|
67 |
| } |
68 |
| } |
69 |
| |
70 |
0
| getOut().println(metrics.getInterfaces().size() + " interface(s)");
|
71 |
0
| if (list) {
|
72 |
0
| for (Object o : metrics.getInterfaces()) {
|
73 |
0
| getOut().println(" " + o);
|
74 |
| } |
75 |
| } |
76 |
| |
77 |
0
| getOut().println();
|
78 |
0
| getOut().println(metrics.getMethods().size() + " method(s) (average " + (metrics.getMethods().size() / (metrics.getClasses().size() + (double) metrics.getInterfaces().size())) + " per class/interface)");
|
79 |
0
| getOut().println(metrics.getFields().size() + " field(s) (average " + (metrics.getFields().size() / (metrics.getClasses().size() + (double) metrics.getInterfaces().size())) + " per class/interface)");
|
80 |
0
| getOut().println();
|
81 |
| |
82 |
0
| printCFM(" synthetic element(s)", metrics.getSyntheticClasses(), metrics.getSyntheticFields(), metrics.getSyntheticMethods());
|
83 |
0
| printCFM(" deprecated element(s)", metrics.getDeprecatedClasses(), metrics.getDeprecatedFields(), metrics.getDeprecatedMethods());
|
84 |
0
| printCFMIC(" public element(s)", metrics.getPublicClasses(), metrics.getPublicFields(), metrics.getPublicMethods(), metrics.getPublicInnerClasses());
|
85 |
0
| printFMIC(" protected element(s)", metrics.getProtectedFields(), metrics.getProtectedMethods(), metrics.getProtectedInnerClasses());
|
86 |
0
| printFMIC(" private element(s)", metrics.getPrivateFields(), metrics.getPrivateMethods(), metrics.getPrivateInnerClasses());
|
87 |
0
| printCFMIC(" package element(s)", metrics.getPackageClasses(), metrics.getPackageFields(), metrics.getPackageMethods(), metrics.getPackageInnerClasses());
|
88 |
0
| printCMIC(" abstract element(s)", metrics.getAbstractClasses(), metrics.getAbstractMethods(), metrics.getAbstractInnerClasses());
|
89 |
| |
90 |
0
| printFMIC(" static element(s)", metrics.getStaticFields(), metrics.getStaticMethods(), metrics.getStaticInnerClasses());
|
91 |
0
| printCFMIC(" final element(s)", metrics.getFinalClasses(), metrics.getFinalFields(), metrics.getFinalMethods(), metrics.getFinalInnerClasses());
|
92 |
| |
93 |
0
| getOut().println(metrics.getSynchronizedMethods().size() + " synchronized method(s)");
|
94 |
0
| if (list) {
|
95 |
0
| for (Method_info method : metrics.getSynchronizedMethods()) {
|
96 |
0
| getOut().println(" " + method);
|
97 |
| } |
98 |
| } |
99 |
| |
100 |
0
| getOut().println(metrics.getNativeMethods().size() + " native method(s)");
|
101 |
0
| if (list) {
|
102 |
0
| for (Method_info method : metrics.getNativeMethods()) {
|
103 |
0
| getOut().println(" " + method);
|
104 |
| } |
105 |
| } |
106 |
| |
107 |
0
| getOut().println(metrics.getVolatileFields().size() + " volatile field(s)");
|
108 |
0
| if (list) {
|
109 |
0
| for (Field_info field : metrics.getVolatileFields()) {
|
110 |
0
| getOut().println(" " + field);
|
111 |
| } |
112 |
| } |
113 |
| |
114 |
0
| getOut().println(metrics.getTransientFields().size() + " transient field(s)");
|
115 |
0
| if (list) {
|
116 |
0
| for (Field_info field : metrics.getTransientFields()) {
|
117 |
0
| getOut().println(" " + field);
|
118 |
| } |
119 |
| } |
120 |
| |
121 |
0
| for (AttributeType attributeType : AttributeType.values()) {
|
122 |
0
| getOut().println(metrics.getAttributeCounts().get(attributeType.getAttributeName()) + " " + attributeType.getAttributeName() + " attribute(s)");
|
123 |
| } |
124 |
| |
125 |
0
| getOut().println(metrics.getCustomAttributes().size() + " custom attribute(s)");
|
126 |
0
| if (list) {
|
127 |
0
| for (Custom_attribute attribute : metrics.getCustomAttributes()) {
|
128 |
0
| getOut().println(" " + attribute);
|
129 |
| } |
130 |
| } |
131 |
| |
132 |
0
| if (getCommandLine().getToggleSwitch("instruction-counts")) {
|
133 |
0
| getOut().println();
|
134 |
0
| getOut().println("Instruction counts:");
|
135 |
0
| for (int opcode=0; opcode<256; opcode++) {
|
136 |
0
| getOut().print(" 0x");
|
137 |
0
| Hex.print(getOut(), (byte) opcode);
|
138 |
0
| getOut().println(" " + com.jeantessier.classreader.impl.Instruction.getMnemonic(opcode) + ": " + metrics.getInstructionCounts()[opcode]);
|
139 |
| } |
140 |
| } |
141 |
| } |
142 |
| |
143 |
0
| private void printCMIC(String label, Collection<Classfile> classes, Collection<Method_info> methods, Collection<InnerClass> innerClasses) throws IOException {
|
144 |
0
| getOut().println((classes.size() +
|
145 |
| methods.size() + |
146 |
| innerClasses.size()) + label); |
147 |
0
| if (list) {
|
148 |
0
| getOut().println(" " + classes.size() + " class(es)");
|
149 |
0
| for (Classfile aClass : classes) {
|
150 |
0
| getOut().println(" " + aClass);
|
151 |
| } |
152 |
| |
153 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
154 |
0
| for (Method_info method : methods) {
|
155 |
0
| getOut().println(" " + method);
|
156 |
| } |
157 |
| |
158 |
0
| getOut().println(" " + innerClasses.size() + " inner class(es)");
|
159 |
0
| for (InnerClass innerClass : innerClasses) {
|
160 |
0
| getOut().println(" " + innerClass);
|
161 |
| } |
162 |
| } else { |
163 |
0
| getOut().println(" " + classes.size() + " class(es)");
|
164 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
165 |
0
| getOut().println(" " + innerClasses.size() + " inner class(es)");
|
166 |
| } |
167 |
| } |
168 |
| |
169 |
0
| private void printCFMIC(String label, Collection<Classfile> classes, Collection<Field_info> fields, Collection<Method_info> methods, Collection<InnerClass> innerClasses) throws IOException {
|
170 |
0
| getOut().println((classes.size() +
|
171 |
| fields.size() + |
172 |
| methods.size() + |
173 |
| innerClasses.size()) + label); |
174 |
0
| if (list) {
|
175 |
0
| getOut().println(" " + classes.size() + " class(es)");
|
176 |
0
| for (Classfile aClass : classes) {
|
177 |
0
| getOut().println(" " + aClass);
|
178 |
| } |
179 |
| |
180 |
0
| getOut().println(" " + fields.size() + " field(s)");
|
181 |
0
| for (Field_info field : fields) {
|
182 |
0
| getOut().println(" " + field);
|
183 |
| } |
184 |
| |
185 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
186 |
0
| for (Method_info method : methods) {
|
187 |
0
| getOut().println(" " + method);
|
188 |
| } |
189 |
| |
190 |
0
| getOut().println(" " + innerClasses.size() + " inner class(es)");
|
191 |
0
| for (InnerClass innerClass : innerClasses) {
|
192 |
0
| getOut().println(" " + innerClass);
|
193 |
| } |
194 |
| } else { |
195 |
0
| getOut().println(" " + classes.size() + " class(es)");
|
196 |
0
| getOut().println(" " + fields.size() + " fields(s)");
|
197 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
198 |
0
| getOut().println(" " + innerClasses.size() + " inner class(es)");
|
199 |
| } |
200 |
| } |
201 |
| |
202 |
0
| private void printCFM(String label, Collection<Classfile> classes, Collection<Field_info> fields, Collection<Method_info> methods) throws IOException {
|
203 |
0
| getOut().println((classes.size() +
|
204 |
| fields.size() + |
205 |
| methods.size()) + label); |
206 |
0
| if (list) {
|
207 |
0
| getOut().println(" " + classes.size() + " class(es)");
|
208 |
0
| for (Classfile aClass : classes) {
|
209 |
0
| getOut().println(" " + aClass);
|
210 |
| } |
211 |
| |
212 |
0
| getOut().println(" " + fields.size() + " field(s)");
|
213 |
0
| for (Field_info field : fields) {
|
214 |
0
| getOut().println(" " + field);
|
215 |
| } |
216 |
| |
217 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
218 |
0
| for (Method_info method : methods) {
|
219 |
0
| getOut().println(" " + method);
|
220 |
| } |
221 |
| } else { |
222 |
0
| getOut().println(" " + classes.size() + " class(es)");
|
223 |
0
| getOut().println(" " + fields.size() + " fields(s)");
|
224 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
0
| private void printFMIC(String label, Collection<Field_info> fields, Collection<Method_info> methods, Collection<InnerClass> innerClasses) throws IOException {
|
229 |
0
| getOut().println((fields.size() +
|
230 |
| methods.size() + |
231 |
| innerClasses.size()) + label); |
232 |
0
| if (list) {
|
233 |
0
| getOut().println(" " + fields.size() + " field(s)");
|
234 |
0
| for (Field_info field : fields) {
|
235 |
0
| getOut().println(" " + field);
|
236 |
| } |
237 |
| |
238 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
239 |
0
| for (Method_info method : methods) {
|
240 |
0
| getOut().println(" " + method);
|
241 |
| } |
242 |
| |
243 |
0
| getOut().println(" " + innerClasses.size() + " inner class(es)");
|
244 |
0
| for (InnerClass innerClass : innerClasses) {
|
245 |
0
| getOut().println(" " + innerClass);
|
246 |
| } |
247 |
| } else { |
248 |
0
| getOut().println(" " + fields.size() + " fields(s)");
|
249 |
0
| getOut().println(" " + methods.size() + " method(s)");
|
250 |
0
| getOut().println(" " + innerClasses.size() + " inner class(es)");
|
251 |
| } |
252 |
| } |
253 |
| |
254 |
0
| public static void main(String[] args) throws Exception {
|
255 |
0
| new ClassMetrics().run(args);
|
256 |
| } |
257 |
| } |